Docker Community Forums

Share and learn in the Docker community.

How to log in to MySQL container?

I’m using LinuxMint 20.2 and I started a MySQL container

sudo docker run --name my-sql-container -p 3309:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest

That seems to be working

sudo docker ps -a -f name=my-sql-container        
CONTAINER ID   IMAGE          COMMAND                  CREATED      STATUS    PORTS     NAMES
601e12ag0c65   mysql:latest   "docker-entrypoint.s…"   17 days ago   Created             my-sql-container

How can I log in to that container from my host? If I try

sudo mysql -h localhost -P 3309 -u root

I will be logged in to my existing local DB

Do you want to log in to the container or just connect to the mysql server?

That was long time ago when I worked with MySQL but if I remember well, for MySQL “localhost” means “use the local unix socket”. Also you don’t need sudo. Try with an IP address:

mysql -h -P 3309 -u root

If it doesn’t work then use the mysql client inside the container to check the user and where they can connect from. When you first install the MySQL server the initial permissions are very restrictive

docker exec -it my-sql-container mysql -h localhost -P 3309 -u root