Blueblazer172 Blueblazer172 - 13 days ago 5
MySQL Question

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in Vagrant

I have a problem with my mysql-server in vagrant. Everytime i restart my vagrant box with

vagrant reload
or
vagrant up
i get this confusing error.

My vagrant box is this one here:
ubuntu/trusty64
from
Vagrant-Boxes

This error has been answered already several times here and at askubuntu.
but i want to know why only this code works for me:

su - mysql -s /bin/sh -c "/usr/bin/mysqld_safe > /dev/null 2>&1 &"


the code is from the file
mysql*
at
/etc/init.d/mysql
.


can anyone explain what the code means?



all this answeres did not help me for explanation from here:


mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Answer

The error means the connection to the server failed, and the root cause is that the MySQL server is not running, most likely because it was not started.

You need to make sure the machine startup scripts do start MySQL, in the proper order (the server before clients connections to it), and be sure to wait for the startup to actually complete.

The following script

su - mysql -s /bin/sh -c "/usr/bin/mysqld_safe > /dev/null 2>&1 &"

does start the MySQL server.

What it does is:

  1. Log in as the mysql user (su - mysql)
  2. Execute a shell (-s /bin/sh)
  3. Execute a command in this shell (-c)
  4. The command itself is /usr/bin/mysqld_safe > /dev/null 2>&1 &

This command:

  1. Starts a MySQL server (/usr/bin/mysqld_safe)
  2. Redirect the standard output to nowhere (> /dev/null)
  3. Redirect the standard error to nowhere as well (2>&1)
  4. And execute the command in the background (&)
Comments