简体   繁体   中英

Cannot connect to mysql from within a docker container

Bulding a mysql based image as follows ( Dockerfile ):

FROM mysql:5.7

COPY somescripts* /docker-entrypoint-initdb.d/

and then exec-ing into it:

docker run --env="MYSQL_ROOT_PASSWORD=mypassword" -it theimagejustbuilt bash

but ...

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

Solution 1. Specify "127.0.0.1" as the host instead of localhost, ie mysql -h 127.0.0.1 -u root instead of mysql. Note that if you omit the host (mysql -u root) the MySQL client will implicitly use localhost.

Solution 2. In /etc/mysql/my.cnf you should see this near the top of the file:

[client]
port          = 3306
socket        = /var/run/mysqld/mysqld.sock
Change socket to the location of your MemSQL socket file. By default, this is /var/lib/memsql/data/memsql.sock.

Turns out I was accidentaly overriding the entrypoint via the following command:

docker run --env="MYSQL_ROOT_PASSWORD=mypassword" -it theimagejustbuilt bash

the bash keyword at the end is not needed since it overrides the default entrypoint and connection to mysql service is therefore not possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM