0

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)

Community
  • 1
  • 1
Blueblazer172
  • 588
  • 2
  • 15
  • 44

2 Answers2

4

this work for me:

  1. sudo service mysql stop
  2. sudo usermod -d /var/lib/mysql/ mysql
  3. sudo service mysql start
Rafael
  • 7,605
  • 13
  • 31
  • 46
Jay
  • 90
  • 10
2

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 (&)
Marc Alff
  • 8,227
  • 33
  • 59
  • but if i normally start my webserver i still have to runt the script from above... can there be something wrong with my vagrant box ? – Blueblazer172 Nov 21 '16 at 08:35