You have a Docker MySQL container running, and you want to connect to it from the MySQL client on your host machine without using another Docker container as a tunnel. Here's how you can achieve this:
Using docker-compose up
By publishing port 3306 on your Docker host, you can connect to the MySQL container using 127.0.0.1:3306 from the host machine.
Using docker-compose run
If you're using docker-compose run, you need to include the --service-ports option to ensure the port mapping is considered. Use the following command:
docker-compose run --service-ports db
Important Notes
Example command to connect to MySQL using the host IP:
mysql -h 127.0.0.1 -P 3306 -u root
The above is the detailed content of How to Connect to a Docker MySQL Container Directly from the Host Machine?. For more information, please follow other related articles on the PHP Chinese website!