Can’t connect to local MySQL server through socket…error.

I recently moved my /home folder to its own partition, but in doing so, I broke MySQL. The full error I got was:
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

To fix this you need to create the file and make sure that MySQL has access to it. (All commands need to be run as root)

Create the directory (if it doesn’t already exist).

sudo mkdir /var/run/mysqld/

Create the file by “touching” it.

sudo touch /var/run/mysqld/mysqld.sock

Set the ownership of the mysqld.sock file and folder to mysql.

sudo chown -R mysql /var/run/mysqld/

You can then start MySQL and breath easier.

33 thoughts on “Can’t connect to local MySQL server through socket…error.

  1. i’ve followed your instruction, then it came out like this

    root@razman-laptop:~# mysql -u root -p
    Enter password:
    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (111)

  2. I have same problem…
    Same answers again and again.
    Again another unsolved problem. Noone seems to investigate what the heck is going on.
    It seems the OS have to be reinstalled.

    Ubuntu for Human DOING!

    • This is the problem with free stuff… You have to self service yourself waisting 3 – 4 days reading and posting to figure out why a file might just disapear alone. This thing just happened to me and that’s why i ended here.

      Cheers everybody, may the Varchaaar be with u all !

  3. The provided solution works when I logged in as root but when I logged in as another user I face the same error. Dont know what the problem is.. :(

  4. I have tried creating the file with owner mysql,
    I have tried stopping and restarting mysql – using the XAMPP command /opt/lampp/lampp startmysql
    The CD collection in XAMPP works fine with mysql.
    I am trying to install gallery3 with the command

    php installer/index.php -u mozillanerd -p #GAL#GAL -d mozillanerd -x moz
    Oops! Something went wrong during the installation:
    
    ==> Unable to connect to the database.
    Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
    
    For help you can try:
      * The Gallery 3 FAQ   - http://codex.gallery2.org/Gallery3:FAQ
      * The Gallery Forums - http://gallery.menalto.com/forum
    
    ** INSTALLATION FAILED **

    nothing seems to work.

  5. hello, I stumbled upon the same problem… tried all the conventional solutions of creating mysqld.sock and changing the ownership to mysql user, but that was not the problem…

    On closer look in /var/log/syslog i found following error…

    mysqld[29562]: 100722 0:58:31 [ERROR] /usr/sbin/mysqld: unknown option ‘–skip-federated’
    mysqld_safe[29565]: ended
    /etc/init.d/mysql[29733]: 0 processes alive and ‘/usr/bin/mysqladmin –defaults-file=/etc/mysql/debian.cnf ping’ resulted in
    /etc/init.d/mysql[29733]: ^G/usr/bin/mysqladmin: connect to server at ‘localhost’ failed
    /etc/init.d/mysql[29733]: error: ‘Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)’
    /etc/init.d/mysql[29733]: Check that mysqld is running and that the socket: ‘/var/run/mysqld/mysqld.sock’ exists!

    I was setting up mysql for Ruby On Rails for which I had to install some client mysql libraries as explained at http://www.bin-co.com/blog/2009/04/error-connection-mysql-ruby-rails-linux-solution/

    ‘sudo apt-get install libmysqlclient15-dev libmysqlclient15off libmysql-ruby’

    but above installations made some changes to mysql conf file and probably added ‘–skip-federated’ to my.cnf

    Removing –skip-federated solved my problem.

    Cheers!
    Z.

  6. Pingback: MySQL service won’t start

  7. Pingback: mysql service offline – mysql.sock location

  8. What worked for me on ubuntu

    sudo apt-get purge mysql-client mysql-server

    sudo apt-get install mysql-client mysql-server

    when uninstalling it allows you to keet databases and users, it works after the installation just as before it crashed for an uncertain reason.

    • Yep, I had that mysqld.sock problem too and that’s the only way to get mysql back without loosing any data. But I did nothing that trigged the problem. I suspect the default template server in Virtualmin to be the “source” of the problem as I reached the limits. Previously any new file I tried to ftp on my server where empty at the arrival because of the lack of space; not sure if it’s related.

  9. Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

    this happened to me when I needed to reset the root password. It borked the permissions for mysql.

    I had to insert a mysql-init file into the start up. then stop, restart and then I was able to log in again (thank god).

    I suspect that if you look at the permissions on user: mysql and or other users you could avoid the re-install. I just hesitate to give up and have the re-install potentially blow out custom configs. Tho hopefully you’ve got those in all the correct places in the first place.

    On the other hand, mysql is so arcane, I’d probably be the first to do it.

    Thanks for the instructions that saved my day btw, when I saw the database seemingly bite the dust, I was seriously worried I was going to be rebuilding from the ground up.

  10. Pingback: MySQL service won’t start - Admins Goodies

  11. I had the same problem on an Ubuntu 11 virtual machine running on vmware server 2.0. It turned out that I had broken my /etc/network/interfaces file. Once I got the loopback portion of the file corrected, MySQL came back fine.

    For me, it was making sure that the following lines were in the /etc/network/interfaces file:

    auto lo
    iface lo inet loopback

    When you connect to MySQL via the local machine using # mysql -uusername -ppassword it tries to connect to 127.0.0.1 (local loopback) However, if you’ve botched your interfaces file as I had, your mysql gets screwed. I tried all of the uninstalls mentioned above but that didn’t work for me. I even made sure that I went through the entire filesystem and deleted every file and directory with “mysql” in it’s name and then reinstalled. No luck. But when I got my interfaces file corrected, everything was fine.

    see # man interfaces for a little help on the file.

  12. I’ve noticed a lot of people coming across this problem and I’ve seen many unresolved questions floating around. Many times this occurs after a system reboot where mysql/mysqld is not started once the reboot is complete. A fix I came across:

    # chkconfig –list mysqld
    mysqld 0 : off 1 : off 2 : off 3 : off 4 : off 5 : off 6 : off

    # chkconfig mysqld on

    # chkconfig –list mysqld
    mysqld 0 : off 1 : off 2 : on 3 : on 4 : on 5 : on 6 : off

    # /sbin/reboot

    ——
    Wait for reboot to complete

    Then, to check that mysql is working properly
    ——

    # mysql -u root -p

    OR

    # /etc/init.d/mysqld status

    I hope this helps someone.

  13. If you are using MySql 5.5 ,Then do the following
    1. mv /etc/my.cnf /etc/my.cnf.bkp
    2. /scripts/mysqlup –force
    3. /etc/init.d/mysql restart

    Ok

  14. or you can try this

    cd /var/lib/mysql
    touch mysql.sock
    chown mysql:mysql mysql.sock
    chmod 1777 mysql.sock

    now make a sym link into /tmp

    ln -s /var/lib/mysql/mysql.sock /tmp
    then
    chmod 1777 /tmp

    now
    /scripts/mysqlup –force

    now just restart mysql
    /etc/rc.d/init.d/mysql restart

  15. Pingback: debian mysql server always down | web technical support

  16. Hi

    it because my HD was out of space!

    check with df -h if you have a space.

    what happen is that mysql tried to create the socket file, but there is no space so it dosent worked !

  17. I have follow instruction, than i have have give this error…
    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysql/mysql.sock’ (111)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>