Skip to content


Can’t connect to local MySQL server through socket /tmp/mysql.sock

A frequent error message received when using the mysql command line utility is: Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ While this error message can be frustrating, the solution is simple.

When connecting to a MySQL server located on the local system, the mysql client connects thorugh a local file called a socket instead of connecting to the localhost loopback address 127.0.0.1. For the mysql client, the default location of this socket file is /tmp/mysql.sock. However, for a variety of reasons, many MySQL installations place this socket file somewhere else like /var/lib/mysql/mysql.sock.

While it is possible to make this work by specifying the socket file directly in the mysql client command

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

it is painful to type this in every time. If you must do so this way (because you don’t have permissions to the file in the solution below), you could create an alias in your shell to make this work (like alias mysql=”mysql –socket=/var/lib/mysql/mysql.sock” depending on your shell).

To make your life easier, you can make a simple change to the MySQL configuration file /etc/my.cnf that will permanently set the socket file used by the mysql client. After making a backup copy of /etc/my.cnf, open it in your favorite editor. The file is divided into sections such as

[mysqld]
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock

[mysql.server]
user=mysql
basedir=/usr/local/mysql

If there is not currently a section called [client], add one at the bottom of the file and copy the socket= line under the [mysqld] section such as:

[client]
socket=/var/lib/mysql/mysql.sock

If there is already a [client] section in the my.cnf file, add or edit the socket line as appropriate. You won’t need to restart your server or any other processes. Subsequent uses of the mysql client will use the proper socket file.

Extrapolating this to my case, i moved the MySQL database to another hard-drive, so the client would still connect to the old .sock file, so i defined the CLIENT section in /etc/my.cnf.

Also, i had to change in php.ini the default mysql_connect socket, to point to the new location.

In case you’re using MailScanner, it has to be restarted as well, because it will still try to use the old mysql socket.

Posted in Tutorials - cPanel.

3 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Bulshiiiiiiiiittttt!!!! :D

    When the application and the mysql database reside on the same machine, you can forget about connecting through network sockets (which is the equivalent of connecting to IP: 127.0.0.1) and you should instead try connecting through a local file called mysql.sock.

    The place of the file (mysql.sock) depends on the mysql build/linux distro.

    Regarding that file two things should be taken into account:
    1. Know exactly where that file resides.
    2. Make sure the rights on that bastard (I mean mysql.sock) are properly set. You should have a user/group “mysql” with rights on that file.

    Now… if you can’t connect to the database you can quickly check its status and (also important) the parameters.

    You can do that with a command like:
    ps -ef | grep mysqld

    if you get no result, then the server wasn’t able to start.

    If you get results, then you should have something like:
    /usr/libexec/mysql/mysqld –basedir=/usr –datadir=/var/lib/mysql –user=mysql –pid-file=/var/run/mysql/mysql.pid –skip-external-locking –port=3306 –socket=/var/run/mysql/mysql.sock

    Option –port=3306 makes sure you can access the database from remote machines. Ofcourse you need firewall access and also most probably an username/password. Also in the cnf file you should comment out “bind-address = 127.0.0.1″ which will make mysqld listen on localhost ONLY.

    Option –socket=/var/run/mysql/mysql.sock tells you where is the socket located, and also tells you that this file connection mode is available.

    In order

    P.S. After you set up the password on the database you can’t connect to it without adding 2 parameters to the mysql client.

    So, instead of using: mysql with no parameters try:
    mysql -u root -p
    then type the password.

  2. aia e bullshit pentru noobs in situatia in care nu se foloseste cpanel or stuff :)

  3. Programare Sociala this is a cPanel tut, so you r tha bullshiter… this is correct..

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.