When I try to run mysql command through bash, I get this error.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")
This is my docker-compose file
db:
image: mariadb:10.0.34
volumes:
- ./latest_dump.sql:/docker-entrypoint-initdb.d/latest_dump.sql
restart: always
ports:
- "3307:3306"
environment:
MYSQL_ROOT_PASSWORD: *******
How can I solve this issue?
From the error message, I can only assume you're using the mysql client from your host and trying to connect it to localhost. The mysql client has a special case where it converts a hostname of localhost to the default Unix socket path instead, hence the error you're seeing. Swap localhost for 127.0.0.1 and it'll probably work.
In the future, these sorts of questions/requests would be more appropriately posted to the Docker Community Forums, the Docker Community Slack, or Stack Overflow.
I encountered the same problem, and I confirm, it works with host explicitly set with 127.0.0.1
mysql -u ${mariadb_username} -p${mariadb_password} -h 127.0.0.1
Thanks @tianon
Most helpful comment
From the error message, I can only assume you're using the
mysqlclient from your host and trying to connect it tolocalhost. Themysqlclient has a special case where it converts a hostname oflocalhostto the default Unix socket path instead, hence the error you're seeing. Swaplocalhostfor127.0.0.1and it'll probably work.In the future, these sorts of questions/requests would be more appropriately posted to the Docker Community Forums, the Docker Community Slack, or Stack Overflow.