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

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

Linda Hamilton
Release: 2024-11-30 06:18:18
Original
880 people have browsed it

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

Connecting to MySQL in a Docker Container from the Host

You desire to establish a connection to a MySQL instance running within a Docker container from your host machine. Despite efforts, your attempts have been unsuccessful.

Docker Configuration

Your Dockerfile configures:

  • Ubuntu 14.04.3 base image
  • MySQL-server installation
  • Modification of my.cnf to disable binding to localhost
  • Exposure of port 3306
  • MySQL startup and log monitoring upon container execution

Host Connectivity Issues

Despite a successful container startup and MySQL accessibility within the container, you encounter an error when attempting to connect from the host:

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

Solution: Specify Host, Port, and Protocol

To establish a connection from the host to the MySQL container, you must specify the following parameters in the mysql command:

  • Host: localhost
  • Port: Docker-forwarded port (12345 in your case)
  • Protocol: tcp (since the MySQL socket is unavailable in the container)

The corrected command should be:

mysql -h localhost -P 12345 --protocol=tcp -u root
Copy after login

The above is the detailed content of How Can 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