0

As shown below, when I create a MySQL 8.0 Docker container and immediately try to connect to it with mysql -u root -ppass, I get an error that there is no socket, but after waiting for a few minutes, I can reconnect and it succeeds. Is this because some initialization process was not completed and the first and second connection failed? If so, can I check the some kind of log to see if MySQL has finished the initialization process?

$ # WSL 2
$ docker -v
Docker version 20.10.12, build e91ed57
$ docker run -d --rm --name mysql_test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=pass mysql:8.0
$ docker exec -it mysql_test bash
# mysql -u root -ppass
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
# mysql -u root -ppass
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
# mysql -u root -ppass
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
...

By the way, the cause of the error is that there is no socket to connect to the MySQL server, so I tried to touch /var/run/mysqld/mysqld.sock, but only got the following error, and I had to wait for several minutes to connect. mysql start, mysql restart also had no effect.

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

user48423
  • 39
  • 3
  • 3
    It takes a while for the database to start up and be ready for connections. `docker run` returns as soon as the container has started, but that doesn't mean that it's ready to use. – Hans Kilian Feb 28 '22 at 08:27
  • Oh I see. If so, is there any way to check that the MySQL initialization is finished in a log file? Can I it using `less` or something? – user48423 Feb 28 '22 at 08:41
  • 1
    You can see the initialization messages using `docker logs mysql_test` – Hans Kilian Feb 28 '22 at 08:44
  • 1
    The usual approach is to either use `nc` or the command-line `mysql` tool to try to connect to the database until you succeed; there is a reasonably-widely-used `wait-for` script that packages this up. The linked question has more examples. – David Maze Feb 28 '22 at 11:52

0 Answers0