Home >Database >Mysql Tutorial >How to Connect to a Docker MySQL Container from Localhost?

How to Connect to a Docker MySQL Container from Localhost?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-09 17:33:021011browse

How to Connect to a Docker MySQL Container from Localhost?

Connecting to Docker MySQL Container from Localhost

To interact with a MySQL instance running within a Docker container directly from your host machine, follow these steps:

Using docker-compose up

Since you have published port 3306 on your Docker host, you can connect to it from the host itself:

$> mysql -h 127.0.0.1 -P 3306 -u root

Using docker-compose run

If you are not using the docker-compose up command, include the --service-ports option to consider the port mapping section:

$> docker-compose run --service-ports db

Additional Considerations

  • By default, the MySQL client may try to connect using a Unix socket when using "localhost." To avoid this, 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 Docker MySQL Container from Localhost?. 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