Connect to Docker MySQL Container from Localhost without Docker Commands
You have a Docker container running a MySQL instance and want to connect to it from your local MacBook's command line, without using Docker commands. Here's how you can achieve this:
Using docker-compose up
Start the container with port mapping enabled:
docker-compose up
From your host, you can connect to the MySQL instance using:
mysql -h 127.0.0.1 -P 3306 -u root
Using docker-compose run
When using docker-compose run, you need to explicitly enable port mapping using the --service-ports option:
docker-compose run --service-ports db
Connect to the MySQL instance as before:
mysql -h 127.0.0.1 -P 3306 -u root
Additional Note
Remember to use 127.0.0.1 as the host address, instead of localhost, as the MySQL client by default tries to connect via a Unix socket when connecting to localhost.
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