Home > Database > Mysql Tutorial > body text

How to Connect to a Docker MySQL Container from Localhost?

Patricia Arquette
Release: 2024-11-09 17:33:02
Original
890 people have browsed it

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

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

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

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!

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