Home > Database > Mysql Tutorial > 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
Release: 2024-11-11 07:34:02
Original
862 people have browsed it

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
Copy after login

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
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template