Problem:
You want to connect to a MySQL instance running in a Docker container from your host machine, but you're encountering an error related to the socket connection.
Your Dockerfile includes the following changes:
Successful Command Sequence:
Connecting from Within the Container:
Once inside the container, you can successfully connect to MySQL using: mysql -u root
Unsuccessful Attempt from Host Machine:
When attempting to connect from the host using: mysql -P 12345 -uroot, you receive an error indicating the inability to connect to MySQL through the socket.
To connect to the MySQL instance in the Docker container from the host machine, use the following command:
mysql -h localhost -P 3306 --protocol=tcp -u root
Replace 3306 with the port number you have forwarded from the Docker container (in this case, 12345).
Explanation:
Since MySQL is running within the Docker container, the socket connection is not available. By setting "--protocol=tcp" in the mysql command, you can specify that the connection should be made through TCP instead.
The above is the detailed content of How to Connect to a MySQL Docker Container from Your Host Machine?. For more information, please follow other related articles on the PHP Chinese website!