Home > Database > Mysql Tutorial > How Do I Connect to a MySQL Docker Container from My Host Machine?

How Do I Connect to a MySQL Docker Container from My Host Machine?

Linda Hamilton
Release: 2024-11-27 17:27:11
Original
224 people have browsed it

How Do I Connect to a MySQL Docker Container from My Host Machine?

Connecting to MySQL in Docker Container from Host

When attempting to connect to a MySQL instance running within a Docker container from the host machine, you may encounter the error:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Copy after login

To resolve this issue and connect successfully, follow these steps:

  1. Specify the host address, port, and protocol when connecting from the host:
mysql -h localhost -P <port> --protocol=tcp -u root
Copy after login

Replace with the port you have forwarded from the Docker container (e.g., 12345).

  1. MySQL in Docker containers runs on TCP, not sockets. Using --protocol=tcp specifies TCP connection.
  2. Ensure that the bind-address in the MySQL configuration within the Docker container is set to '0.0.0.0' to allow connections from outside the container.

With these modifications, you should be able to successfully connect to the MySQL instance in the Docker container from your host machine. Remember to replace localhost in the connection command with the IP address or hostname of the host machine.

The above is the detailed content of How Do I Connect to a MySQL Docker Container from My 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