You have a MySQL container running in Docker and want to establish a connection to it from your MacBook's command line mysql client without using Docker commands.
If you're using docker-compose up, connect to the container using 127.0.0.1:3306, as the host port 3306 is published.
When using docker-compose run, the port mapping section in the docker-compose.yml file is not considered. To enable it, add the --service-ports option:
docker-compose run --service-ports db
By default, the mysql client attempts to use a unix socket when connecting to localhost. Therefore, specify the IP address 127.0.0.1 explicitly:
mysql -h 127.0.0.1 -P 3306 -u root
The above is the detailed content of How to Connect to a MySQL Container from Your Host Machine's Command Line?. For more information, please follow other related articles on the PHP Chinese website!