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.
The above is the detailed content of How Can I Connect to a Docker MySQL Container from My Local Machine Without Using Docker Commands?. For more information, please follow other related articles on the PHP Chinese website!