I'm using below command to run the mysql docker container.
docker container run -it --name mysql-test -e MYSQL_ROOT_PASSWORD=secret mysql bash
Then I tried to connect to mysql inside docker container using below command.
mysql -uroot -psecret
It gives me below error.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
If I do below then sometimes it works but not all time.
touch /var/run/mysqld/mysqld.sock
Then I tried to run the container in detach mode but it also gave me the same error.
docker container run -d -p 3306:3306 --name mysql-test -e MYSQL_ROOT_PASSWORD=secret mysql
docker exec -it mysql-test bash
mysql -uroot -p
What am I doing wrong here?
If you happen to get “Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)” errors in logging in to mysql-server while working on docker containers:
check the path of socket file in /etc/mysql/my.cnf
check if mysqld.sock and mysqld.pid file is present in /var/run/mysqld/ directory or not.
If not, create these files as:
touch /var/run/mysqld/mysqld.sock
touch /var/run/mysqld/mysqld.pid
chown -R mysql:mysql /var/run/mysqld/mysqld.sock
chown -R mysql:mysql /var/run/mysqld/mysqld.sock
chmod -R 644 /var/run/mysqld/mysqld.sock
Now restart the mysql-server as:
service mysql-server restart
Now, trying logging again in to mysql-server.
This Connect to mysql in a docker container from the host may help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With