• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 963
  • Last Modified:

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

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
SiemensSEN
Asked:
SiemensSEN
  • 4
  • 4
  • 4
  • +1
1 Solution
 
upanwarCommented:
Make sure that mysql service is running.

#/etc/init.d/mysqld status

or

# ps -ef | grep -i mysql
0
 
pilson66Commented:
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
 
upanwarCommented:
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
SiemensSENAuthor Commented:
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
 
pilson66Commented:
--socket=/tmp/mysql.sock
socket = /tmp/mysql.sock

socket placed in /tmp/mysql.sock
0
 
SiemensSENAuthor Commented:
Thanks. I look at the /tmp dir and It does not contain the sock file...

0
 
upanwarCommented:
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
 
pilson66Commented:
"sudo ls -l | grep mysql"
0
 
pilson66Commented:
Sorry
"sudo ls -l /tmp/ | grep mysql"
0
 
upanwarCommented:
what about:

ls -la /var/lib/mysql/mysql.sock
0
 
SiemensSENAuthor Commented:

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
 
SiemensSENAuthor Commented:
usbratst:~ # ls -l /tmp/ |grep mysql
usbratst:~ #
0
 
wolfgang_93Commented:
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 4
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now