Solve: 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 through a local file called a socket instead of connecting to the localhost loopback address 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



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:


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.


About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.

51 Responses to “Solve: Can’t connect to local MySQL server through socket /tmp/mysql.sock”

  1. November 19, 2008 at 4:47 pm, Anonymous said:

    Should I be able to connect to MySQL over my LAN? I am running MAMP on Tiger, and can connect using MAMP’s built in tools, but preferred the look of Sequel Pro, which I can use to connect with MySQL on the local machine using LocalHost, root user, and password, port 8889 and Socket: /Applications/MAMP/tmp/mysql/mysql.sock. However when I try to connect to that Mac from another on the LAN, which works to MAMP fine via It says Can’t connect to local MySQL server through socket ‘/Applications/MAMP/tmp/mysql/mysql.sock’ (2). I’m confused. MAMP’s using port 8888, and MySQL’s using port 8889. How can I get pass both in the URL? Or am I all wrong here? Any suggestions?


  2. January 06, 2009 at 7:30 am, arun said:

    I found the following in the MySQL documentation:

    1. Log in as the root user. (“Start” => “Switch User”)
    2. Open a terminal window (the icon that looks like a monitor behind a clamshell).
    3. Type “cd /bin” enter (no quotes). :: usually not nessecary ::
    4. Type “mysql_install_db –user=mysql

    This is from:


  3. January 29, 2009 at 7:29 am, obaid said:

    can this type of error be generated by something like:
    /*as you can see the resource parameter of mysql_fetch_assoc()*/ is missing
    do help please…!!!


  4. February 15, 2009 at 10:21 pm, gogster said:

    I am on client side. I can’t change serwer settings. Can send php scripts only! It would be usefull if it will be my own serwer, but it isnt.. So your’s advice is useless..

    The reason fo client is bad serwer name in “mysql_connect” function. For example database is:
    so you should use it properly like this:


    insted of not properly like this:



    • February 15, 2009 at 10:23 pm, gogster said:

      of course in this problem command:


      not work


  5. May 13, 2009 at 3:25 pm, David C. Rankin, J.D.,P.E. said:

    Excellent nugget of information:

    It came in real handy when the geniouses at opensuse broke an update of mysql to mysql-5.1.34-29.1 by putting the mysql.sock and in /var/run/mysql instead of /var/lib/mysql as normal. Great quick fix was found here. In addition to /etc/my.cnf, the init script at /usr/sbin/rcmysql needed to be fixed as well.



    • June 30, 2009 at 12:37 am, Steven Sheffey said:

      Hi guys, I to have this problem and I’m new to mySQL and PHP. Where are you guys locating all these directories such as: /var/lib/mysql or /usr/local/mysql/data ?


  6. June 30, 2009 at 2:25 pm, JEME said:

    Good day qmchenry,

    I try with de line:
    mysql –socket=/var/lib/mysql/mysql.sock …

    but now the terminal list this error:

    ERROR 1045 (28000): Access denied for user ‘root’@'localhost’ (using password: YES)

    would you can help me


  7. September 25, 2009 at 12:22 am, NikoJr said:

    Man u have saved my life hehehe… very useful.

    Thanku very much


  8. October 17, 2009 at 4:55 pm, Anonymous said:

    Wouldn’t the link be removed at server reboot and the problem appear again??

    I had the same problem but it is because my.cnf was not in the default locations

    Default options are read from the following files in the given order:
    /etc/my.cnf ~/.my.cnf /usr/local/mysql/etc/my.cnf

    So even when I added the [client] section I got the same error, so I did a link

    #ln -s /my/cnf/file_location/my.cnf /etc/my.cnf

    And it worked great.


  9. January 11, 2010 at 1:07 am, Anonymous said:

    i am getting this message

    Warning: mysql_pconnect() [function.mysql-pconnect]: Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2) in /home/content/07/5479507/html/kb/install/install_step3.php on line 61
    Could not connect to the server: Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)

    what should i do?


  10. January 15, 2010 at 10:24 pm, Anonymous said:

    Please make sure that your disk having enough space..!


    • January 28, 2010 at 4:26 pm, Anonymous said:

      Another way to give the path for socket file is this:

      This did wonders for me and I am really very thankful to this:

      Hope this will help many others as it did for me.


  11. February 15, 2010 at 10:00 am, Albert said:

    I had the same problem and that was the solution.
    Thanks a lot!


  12. February 22, 2010 at 2:04 am, Anonymous said:

    Excellent explanation. But I still have a question. I have a different scenario. My mySQL database is on a different server with a hosting company and I am connecting from my client in my home. I am getting the same error. Is there something that can be done from my end to fix this error? Thanks anyways.


    • February 22, 2010 at 2:14 am, Anonymous said:

      Continuing to my earlier post I am not receiving the above error while connecting to the database but when I am running first ever query in my web page. Can somebody suggest another explanation?


  13. March 15, 2010 at 4:14 am, Anonymous said:

    Yes…this solution looks very simple. Now, where the heck is the /etc/my.cnf file?! I can’t find it anywhere. I’m so frustrated right now. I have a hard time when I spend sooo much time just trying to get software working so I can actually get some work done!
    Thanks for the help.


  14. April 08, 2010 at 2:04 am, Benjamin See said:

    I would like to point out that, when performing tasks in an automated environment, such as setting up your mysql during kickstart installation, it important to note that if you execute mysql-related commands immediately after you start the mysql service, it is likely that your commands failed due to the mysql service “still in the progress” of starting up.

    And the error message that you get would be similar that mysql.sock is not found.

    The solution is simple. Allow for some delay between starting your mysql service, and performing your first mysql tasks, so that when the first mysql command is issued, the service is started and running properly.


    • June 02, 2010 at 1:35 am, Pat said:

      Just want to add my humble thank you to the list. I was updating our php version in preparation for another software update when things went south. Could not figure out for the life of me why apache could not connect to mysql. Manual connection …. no problem…. apache -> php -> mysql …. problem

      mysql.sock …. running amok. symbolic link solved the problem!

      What a relief!



  15. June 08, 2010 at 7:06 am, muzie said:

    thanks brother!


  16. August 29, 2010 at 6:28 pm, Marco said:

    Usually there’s a hickup in your my.ini that will get you this message at the first mysql server restart after upgrading or changing your configuration.
    Locate the .err file in your mysql/datadir and see what option is deprecated this time.


  17. September 21, 2010 at 1:43 am, Deepak Frazer said:

    it worked for me on CentOS. mysql Server version: 5.0.77
    Thanks dude


  18. October 14, 2010 at 7:46 pm, Heri Anto マスター said:

    pkill mysqld
    su mysql
    chown -R mysql.mysql /var/lib/mysql/
    mysqld_safe &


  19. October 14, 2010 at 7:47 pm, Heri Anto マスター said:

    try this work for me

    pkill mysqld
    su mysql
    chown -R mysql.mysql /var/lib/mysql/
    mysqld_safe &


  20. November 10, 2010 at 8:12 am, cave_person_2 said:

    Good lord, can SOMEONE say how to do it as if, and I’m just giving you an example, as if not everyone was computerically inclined? I think I must be one of the last 4 in thje USA and we’re all sorry. I really have no idea what you’re talking about, what these things mean and where all these icons with clamshells could be found, etc? So theres another user for my system? Where? And then again, please explain it as if I weren’t a techie too if you can. I would be forever greatful….


    • March 10, 2011 at 7:11 pm, computerically inclined? said:

      Calm down, maybe you are on the wrong message board. This is a technical message board. Sounds like you are looking for the Fisher Price Computer Users Group.


      • December 31, 2012 at 9:54 pm, Kathryn Brewer said:

        that was mean. The user has a legitimate concern. Can we be a little less techy and help the person without making them feel like they are 5? Meanie.


  21. December 09, 2010 at 4:55 pm, Arik_harel said:

    For xampp this command worke for me:
    ln -s /opt/lampp/var/mysql/mysql.sock /var/run/mysqld/mysqld.sock


  22. December 17, 2010 at 11:51 am, CJ said:

    Hello. I’m a real newbie to all this so would be grateful for answers in words of one syllable and sorry if this is obvious to everyone else, but most comments are going over my head. I think if I can get past this bit, then I should be OK to do everything I want to do, I’m just stuck here, trying to get a connection.

    I’m getting the ‘Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)’ error message.

    I’ve set up my first MySQL database and its hosted on a remote web server so I don’t have access to config files. I’m on the client side, trying to connect so I can create some dynamic pages for my website.

    My web hosting company are being less than helpful in helping me to sort this out. What sort of questions should I be asking them and then if they say I’m using the wrong socket, what php script would I need to specify (I would need it in full)?

    Sorry, this must be really basic stuff for everyone else but it’s causing me a lot of grief and if I can just get through this I can actually do some work….

    Thanks in advance for any suggestions.



  23. December 21, 2010 at 11:22 pm, Anonym said:

    BTW also this occurs when your site under ddos attack.
    In this situation mysqld will load high cpu usage too.


  24. January 07, 2011 at 10:15 am, Ajimol said:

    Thanks a worked for me too.


  25. February 16, 2011 at 5:52 pm, Yahya Nikandish said:


  26. June 06, 2011 at 10:54 am, Gopinathkarangula said:

    Hi to all for me this problem occurred twice, so i just thought and understood the problem, when ever you are changing some config file related to mysql, better stop the service, edit you config, and start the service, then mysqld problem you will never face..


  27. July 25, 2011 at 3:12 pm, Chandan Singh Pachwal said:

    try runing it as
    /data/(folder in which mysql is installed)/mysql/bin/mysql


  28. July 25, 2011 at 3:12 pm, Chandan Singh Pachwal said:

    try runing it as
    /data/(folder in which mysql is installed)/mysql/bin/mysql


  29. September 24, 2011 at 2:53 am, rana said:

    When I upload my php and MySQL website of web hosting sites then i am getting this message.

    Warning: mysql_pconnect() [function.mysql-pconnect]: Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2) in /srv/disk2/833826/www/ on line 9

    Fatal error: Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2) in /srv/disk2/833826/www/ on line 9

    what should i do?


  30. December 02, 2011 at 6:55 am, Roy M J said:

    Hi i am having the same error, How should my connnection code look like? Currently its

    But the error says

    Can’t connect to local MySQL server through socket ‘/tmp/mysql5.sock’ (2)

    How should i link the socket to this code?



  31. January 02, 2012 at 11:57 am, susu said:

    fantastic illustration ,, but i couldn’t change the ‘my.cnf’ cz it’s read only.. how can i change it’s permission???
    thx alot ;D


  32. March 04, 2012 at 7:02 am, ashraf said:

    i faced the same problem before
    it was on godaddy server
    i just replaced (localhost) with the DBHOST given by godaddy and it worked fine.just visit db info and find database will be a long name..
    i hope this helps someone.


  33. April 25, 2012 at 4:52 am, TQuang said:

    Yes, that right. I finding that.


  34. May 07, 2012 at 10:23 am, amritpal said:

    did not works???


  35. August 15, 2012 at 8:24 pm, Bolt Ram said:

    I don’t know what flavor(s) you guys are using, but on CEntOS 6.2/MySQL 5.5.21 this fix (thanks, by the way) did NOT work until I restarted mysql. If you’re using the command line (via SSH, for example), you may need to use the following command to start her back up:

    # /etc/init.d/msyql start


  36. August 20, 2012 at 6:53 pm, Wolf said:

    I am trying to set up MySQL for roundcube on Mac OS X 10.8 and want to create a database with the command (current path: /usr/local/mysql-5.5.27-osx10.6-x86_64/bin/)
    ./mysqladmin create roundcube
    which gives the error above. However, the solution doesn’t work for me because there is no socket file anywhere and not even a /var/lib/mysql folder. In order to edit my.cnf I also had to create it first. (I copied the file /usr/local/mysql/support-files/my-huge.cnf to /etc/my.cnf)
    Then I tried to start the daemon by hand, so I ran
    which gives the following output:

    120820 14:10:18 [Warning] Can’t create test file /usr/local/mysql-5.5.27-osx10.6-x86_64/data/.lower-test
    120820 14:10:18 [Warning] Can’t create test file /usr/local/mysql-5.5.27-osx10.6-x86_64/data/.lower-test
    ./mysqld: File ‘./mysql-bin.index’ not found (Errcode: 13)
    120820 14:10:18 [ERROR] Aborting

    Somewhere else it is said this has to do with database privileges. But I haven’t created any database yet!???
    I would be really happy if anyone could help.



  37. November 08, 2012 at 7:54 am, Sidheeque said:

    I renamed mysql username

    after that i got one error 0 rows effected

    after that i am not able to log in MySQL database with old and new user name

    Please help me


  38. November 10, 2012 at 8:44 am, edd said:

    It’s work for me. Since I moved to the new Dedicated server It’s been more than a week I’m looking the solution of this kind of error.

    Thank a lot Quinn McHenry


  39. January 23, 2013 at 1:24 am, Harihar Pai said:

    Do this it will work mysql -uadmin -h10.13.133.238 -P3307 -p where capital P is for port and p is for password. -u is user and -h is ip of host


  40. March 21, 2013 at 2:39 pm, Liza said:

    Thanks a lot
    You saved my life !!!


  41. June 07, 2013 at 1:19 pm, chris said:

    i can not get into immortalnight


  42. June 12, 2013 at 4:00 am, ajay said:

    but i cant edit in “my.cnf” file please help me

    thanks in advance


  43. June 13, 2013 at 11:30 pm, Apoorv said:

    I am building a quiz as an intern for a website hosted on yahoo, and my quiz is giving the same error. I cannot change the mysql config file on yahoo servers, so what should I do?


  44. March 31, 2014 at 9:38 am, DelDeldago said:

    Thanks dude – you are my hero :)


Leave a Reply