Solved

MYSQL error :Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) (localhost))

Posted on 2011-03-25
13
811 Views
Last Modified: 2012-05-11
Hello,
  I have a Linux system that have MYSQL install. However, when I tried to acess an application that used mysql I get the following error:

(Can't contact the database server: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) (localhost))

Here is the setting snippet

# The MySQL server
[mysqld]
port            = 3306
 basedir = /usr
# datadir = /var/lib/mysql
datadir = /export/mysql/var
socket = /tmp/mysql.sock


# The following options will be passed to all MySQL clients
[client]
#password       = your_password
port            = 3306
socket =/tmp/mysql.sock


usbratst:/tmp # ls -al mysql.sock
srwxrwxrwx 1 mysql mysql 0 Mar 24 23:50 mysql.sock



Thanks
0
Comment
Question by:SiemensSEN
  • 4
  • 4
  • 4
  • +1
13 Comments
 
LVL 12

Expert Comment

by:upanwar
ID: 35215500
Make sure that mysql service is running.

#/etc/init.d/mysqld status

or

# ps -ef | grep -i mysql
0
 
LVL 8

Expert Comment

by:pilson66
ID: 35215515
In your config, socket located in /tmp/mysql.sock

You are trying to connect to /var/lib/mysql/mysql.sock

Try to connect to /tmp/mysql.sock
0
 
LVL 12

Expert Comment

by:upanwar
ID: 35215831
Since in your my.cnf you are calling your socket file from  /tmp/mysql.sock and it is available there but if still you are not able to start your mysql service then please run the below given command.

# ln -s /tmp/mysql.sock /var/lib/mysql

and then start the mysql service.

/etc/init.d/mysqld start
0
 

Author Comment

by:SiemensSEN
ID: 35231663
Hello,
  Thanks. Here is the result I get from running the various comamnd above


@usbratst:/etc/init.d> ps -ef |grep mysql

root      4088     1  0 Mar27 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/export/mysql/var --pid-file=/export/mysql/var/usbratst.pid
mysql     4437  4088  0 Mar27 ?        00:00:09 /usr/sbin/mysqld --basedir=/usr --datadir=/export/mysql/var --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/export/mysql/var/mysqld.log --pid-file=/export/mysql/var/usbratst.pid --socket=/tmp/mysql.sock --port=3306
pb054799 10510 10356  0 08:46 pts/0    00:00:00 grep mysql


-----------------
@usbratst:/etc/init.d> /etc/init.d/mysql stop
MySQL server PID file could not be found!  


@usbratst:/etc/init.d> /usr/sbin/mysqld status

110328  8:45:12 [Warning] Can't create test file /export/mysql/var/usbratst.lower-test
110328  8:45:12 [Warning] Can't create test file /export/mysql/var/usbratst.lower-test
/usr/sbin/mysqld: File './mysql-bin.index' not found (Errcode: 13)
110328  8:45:12 Aborting

110328  8:45:12 [Note] /usr/sbin/mysqld: Shutdown complete

-----


MYSQL.CNF file
# The MySQL server
[mysqld]
port            = 3306
 basedir = /usr
# datadir = /var/lib/mysql
datadir = /export/mysql/var
socket = /tmp/mysql.sock


# The following options will be passed to all MySQL clients
[client]
#password       = your_password
port            = 3306
socket =/tmp/mysql.sock



I cannot find the MYSQL sock file ..

Thanks
0
 
LVL 8

Expert Comment

by:pilson66
ID: 35231714
--socket=/tmp/mysql.sock
socket = /tmp/mysql.sock

socket placed in /tmp/mysql.sock
0
 

Author Comment

by:SiemensSEN
ID: 35231753
Thanks. I look at the /tmp dir and It does not contain the sock file...

0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 12

Expert Comment

by:upanwar
ID: 35231771
Do:

ls -la /tmp/mysql.sock

If file is there then you can create a symbolic link with below given command.

# ln -s /tmp/mysql.sock /var/lib/mysql

and then start the mysql service.

/etc/init.d/mysqld start

It should work.
0
 
LVL 8

Expert Comment

by:pilson66
ID: 35231772
"sudo ls -l | grep mysql"
0
 
LVL 8

Expert Comment

by:pilson66
ID: 35231779
Sorry
"sudo ls -l /tmp/ | grep mysql"
0
 
LVL 12

Expert Comment

by:upanwar
ID: 35231782
what about:

ls -la /var/lib/mysql/mysql.sock
0
 

Author Comment

by:SiemensSEN
ID: 35231851

usbratst:~ # ls -la /var/lib/mysql/mysql.socklrwxrwxrwx 1 root mysql 15 Mar 25 11:38 /var/lib/mysql/mysql.sock -> /tmp/mysql.sock

usbratst:~ # ls -al /tmptotal 68
drwxrwxrwt 12 root root 4096 Mar 28 09:04 .
drwxr-xr-x 24 root root 4096 Mar 27 18:55 ..
drwxrwxrwt  2 root root 4096 Mar 27 20:10 .ICE-unix
-r--r--r--  1 root root   11 Mar 27 20:10 .X0-lock
drwxrwxrwt  2 root root 4096 Mar 27 20:10 .X11-unix
drwx------  2 root root 4096 Mar  9 00:11 .esd-0
drwx------  2 gdm  gdm  4096 Mar 27 20:10 .esd-106
drwx------  2 gdm  gdm  4096 Mar 27 20:10 orbit-gdm
drwx------  2 root root 4096 Mar  9 00:11 orbit-root
drwx------  2 root root 4096 Mar  9 00:11 pulse-AKSCpmG5ddvH
drwx------  2 gdm  gdm  4096 Mar 27 20:10 pulse-PKdhtXMmr18n
-rw-------  1 root root  512 Mar 27 18:55 random-seed.HT3Jpq160d
-rw-------  1 root root  512 Mar 26 21:16 random-seed.Y820xWvpNI
-rw-------  1 root root  512 Mar  9 00:59 random-seed.g4Uclw7xIf
drwxr-xr-x  2 root root 4096 Mar  9 00:11 unique
drwx------  2 root root 4096 Mar  8 22:03 virtual-root.CRZAQT
-rw-r--r--  1 root root 1710 Mar  8 17:56 xwlog

----
usbratst:~ # ls -al /var/lib/mysqltotal 28788
drwxr-xr-x  5 mysql mysql     4096 Mar 25 11:38 .
drwxr-xr-x 44 root  root      4096 Mar 24 16:31 ..
-rw-r--r--  1 mysql mysql        0 Mar  9 23:15 .run-mysql_upgrade
-rw-r--r--  1 root  root       340 Mar 23 01:14 RPM_UPGRADE_HISTORY
-rw-r--r--  1 mysql mysql      340 Mar 23 01:14 RPM_UPGRADE_MARKER-LAST
-rw-rw----  1 mysql mysql  5242880 Mar 24 16:33 ib_logfile0
-rw-rw----  1 mysql mysql  5242880 Mar 21 02:32 ib_logfile1
-rw-rw----  1 mysql mysql 18874368 Mar 24 16:33 ibdata1
drwx--x--x  2 mysql mysql     4096 Mar 23 01:14 mysql
-rw-rw----  1 mysql mysql      126 Mar 23 17:41 mysql-bin.000001
-rw-rw----  1 mysql mysql      126 Mar 23 17:51 mysql-bin.000002
-rw-rw----  1 mysql mysql      126 Mar 23 18:21 mysql-bin.000003
-rw-rw----  1 mysql mysql      150 Mar 23 19:45 mysql-bin.000004
-rw-rw----  1 mysql mysql      126 Mar 24 15:59 mysql-bin.000005
-rw-rw----  1 mysql mysql      126 Mar 24 16:33 mysql-bin.000006
-rw-rw----  1 mysql mysql      114 Mar 24 16:32 mysql-bin.index
lrwxrwxrwx  1 root  mysql       15 Mar 25 11:38 mysql.sock -> /tmp/mysql.sock
-rw-rw----  1 mysql mysql        0 Mar 24 19:45 mysqld.log
-rw-rw----  1 mysql mysql      864 Mar 21 19:45 mysqld.log-20110321.bz2
-rw-rw----  1 mysql mysql     1365 Mar 23 19:45 mysqld.log-20110323.bz2
-rw-rw----  1 mysql mysql      911 Mar 24 19:45 mysqld.log-20110324.bz2
drwx------  2 mysql mysql     4096 Mar 23 01:14 performance_schema
drwxr-xr-x  2 mysql mysql     4096 Mar 23 01:14 test
0
 

Author Comment

by:SiemensSEN
ID: 35231869
usbratst:~ # ls -l /tmp/ |grep mysql
usbratst:~ #
0
 
LVL 8

Accepted Solution

by:
wolfgang_93 earned 125 total points
ID: 35235710
I came into this thread and I think things have gone in the wrong direction because you followed
a suggestion that in my opinion is confusing and did not address your issue appropriately:

ln -s /tmp/mysql.sock /var/lib/mysql

(This command sets up a symbolic link from one place to another and is at best
confusing which is obvious from what I saw happen following its implementation)

So first of all, I suggest that you get rid of this link -- if it is not already gone:
-  Shut down your MySQL server
-  Issue the command ls -ld /tmp/mysql.sock
   to make sure it is gone. If not, issue the command:
      rm /tmp/mysql.sock

Next you need to decide whether you want to/are allowed to modify the application
and modify it to point to /tmp/mysql.sock instead.

Personally I think it is better for /var/lib/mysql/mysql.sock to be used as a socket
location (assuming you actually have a /var/lib/mysql directory set up) because /tmp
is a temporary directory that can be scrubbed anytime
by the operating system and render your MySQL server non-functional.

Issue this command to ensure you have a /var/lib/mysql directory set up with
appropriate permission for the server to use:
   ls -ld /var/lib/mysql

If so, then you can define a socket file at that location by putting lines
like this in your my.cnf file:
   socket = /var/lib/mysql/mysql.sock

Start your server and it should start using the new socket.
Your application should then be able to connect to the MySQL at the new
socket (assuming it is correct in other respects such as MySQL id and password
used by the application).
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now