MySQL Socket errors

This is an error that many people who run PHP and MySQL are familiar with:

ERROR 2002: Can’t connect to local MySQL sever through socket

‘/var/lib/mysql/mysql.sock’ (2)

Luckily, this is relatively easy to solve in most cases. Here’s what to do.

·First of all, we need to decide where the MySQL socket file should be. For this text we will assume that you would like the socket file to be placed in the MySQL default location for a Redhat system which is /var/lib/mysql/mysql.sock.

·Second, is MySQL running? This is easily overlooked, but check and make sure that MySQL is running with a command such as:

[[email protected] ~]# ps aux | grep mysql

root67220.00.146561132 ?S08:200:00 /bin/sh /usr/bin/mysqld_safe –datadir=/var/lib/mysql –socket=/var/lib/mysql/mysql.sock –log-error=/var/log/mysqld.log –pid-file=/var/run/mysqld/mysqld.pid

mysql67640.12.6 128328 16904 ?Sl08:200:01 /usr/libexec/mysqld –basedir=/usr –datadir=/var/lib/mysql –user=mysql –pid-file=/var/run/mysqld/mysqld.pid –skip-external-locking –port=3306 –socket=/var/lib/mysql/mysql.sock

root70510.00.14044676 pts/0R+08:400:00 grep mysql

Good, it’s running so we can move on. If MySQL is not running, try to start it with “/etc/init.d/mysqld start”

·Alright, so MySQL is humming along, but it is pretty useless if we cannnot connect to it. The easiest way to see what MySQL thinks it should be using for socket file is by running the following command:

[[email protected] ~]# /usr/libexec/mysqld –print-defaults

/usr/libexec/mysqld would have been started with the following arguments:

–port=3306 —socket=/var/lib/mysql/mysqld.sock –skip-locking –key_buffer=16K

–max_allowed_packet=1M –table_cache=4 –sort_buffer_size=64K –read_buffer_size=256K

–read_rnd_buffer_size=256K –net_buffer_length=2K –thread_stack=64K –server-id=1

–port=3306 –socket=/var/lib/mysql/mysql99999.sock –skip-locking –key_buffer=16K

–max_allowed_packet=1M –table_cache=4 –sort_buffer_size=64K –read_buffer_size=256K

–read_rnd_buffer_size=256K –net_buffer_length=2K –thread_stack=64K –server-id=1

[[email protected]  ~]#

I’ve highlighted the part that you should look at to find what you need to know. It looks like I must have had a bad keystroke when editing my “/etc/my.cnf” the other day.

·Here’s an excerpt from my “/etc/my.cnf”. Make sure that when you edit this file that you update the correct socket specification. You will notice that there two of them, one of them is for the MySQL client, the second is for the MySQL daemon. Update the one from the “[mysqld]” section.

——-cut———

[client]

#password= your_password

port= 3306

socket= /var/lib/mysql/mysql.sock

# Here follows entries for some specific programs

# The MySQL server

[mysqld]

port= 3306

socket= /var/lib/mysql/mysqld.sock

skip-locking

key_buffer = 16K

max_allowed_packet = 1M

——-cut———

Change it to:

——-cut———

[client]

#password= your_password

port= 3306

socket= /var/lib/mysql/mysql.sock

# Here follows entries for some specific programs

# The MySQL server

[mysqld]

port= 3306

socket= /var/lib/mysql/mysql.sock

skip-locking

key_buffer = 16K

max_allowed_packet = 1M

——-cut———

·Okay, now that this is updated, we should restart MySQL.

[[email protected] ~]# /etc/init.d/mysqld restart

Stopping MySQL:[OK]

Starting MySQL:[OK]

[[email protected] ~]#

·Okay, is everything working now? If you were using the command line mysql client, give it a try. If you were using php, try your webpage again. If you are still experiencing problems with php not being able to find the socket, then you should check where php thinks the socket file lives.

[[email protected] ~]# php -i | grep mysql.default_socket

mysql.default_socket => no value => no value

[[email protected] ~]# php -i | grep php.ini

Configuration File (php.ini) Path => /etc

Loaded Configuration File => /etc/php.ini

It appears that php does not have a socket location set, meaning that it will use what MySQL tells it is the default (in this case being /var/lib/mysql/mysql.sock), but to make sure lets specify a location.

·Notice that in the last step, we also wanted to find out where php’s php.ini configuration file is located and we know that it is in “/etc/php.ini”. Let’s fire up our favorite text editor and fix this.

——-cut———

; compile-time value defined MYSQL_PORT (in that order).Win32 will only look

; at MYSQL_PORT.

mysql.default_port =

; Default socket name for local MySQL connects.If empty, uses the built-in

; MySQL defaults.

mysql.default_socket =

; Default host for mysql_connect() (doesn’t apply in safe mode).

mysql.default_host =

; Default user for mysql_connect() (doesn’t apply in safe mode).

mysql.default_user =

——-cut———

Changes to:

——-cut———

; compile-time value defined MYSQL_PORT (in that order).Win32 will only look

; at MYSQL_PORT.

mysql.default_port =

; Default socket name for local MySQL connects.If empty, uses the built-in

; MySQL defaults.

mysql.default_socket = “/var/lib/mysql/mysql.sock”

; Default host for mysql_connect() (doesn’t apply in safe mode).

mysql.default_host =

; Default user for mysql_connect() (doesn’t apply in safe mode).

mysql.default_user =

——-cut———

Now both the command line MySQL client as well as php should be able to connect to MySQL just fine!