Home  >  Article  >  Database  >  How to Connect to a Docker MySQL Container Directly from the Host Machine?

How to Connect to a Docker MySQL Container Directly from the Host Machine?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-11 07:34:02790browse

How to Connect to a Docker MySQL Container Directly from the Host Machine?

Connecting to Docker MySQL Container from Host Machine Directly

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

  • Ensure that port 3306 is open and available on your host machine.
  • Use 127.0.0.1 instead of localhost when connecting to the MySQL instance, as the default behavior is to connect using a Unix socket.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn