"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Connect to a Docker MySQL Container from Localhost?

How to Connect to a Docker MySQL Container from Localhost?

Published on 2024-11-18
Browse:329

How to Connect to a Docker MySQL Container from Localhost?

Connecting to Docker MySQL Container from Localhost

To interact with a MySQL instance running within a Docker container directly from your host machine, follow these steps:

Using docker-compose up

Since you have published port 3306 on your Docker host, you can connect to it from the host itself:

$> mysql -h 127.0.0.1 -P 3306 -u root

Using docker-compose run

If you are not using the docker-compose up command, include the --service-ports option to consider the port mapping section:

$> docker-compose run --service-ports db

Additional Considerations

  • By default, the MySQL client may try to connect using a Unix socket when using "localhost." To avoid this, specify the IP address (127.0.0.1) explicitly:
$> mysql -h 127.0.0.1 -P 3306 -u root
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3