Docker Container: Accessing Host MySQL

Fikih Firmansyah
2 min readMar 9, 2023

--

Image by Valdas Miskinis from Pixabay

Docker has revolutionized the way applications are developed and deployed. It provides a platform-independent containerization environment that can help developers to build, package, and distribute their applications easily. One of the challenges that developers face is accessing the host’s MySQL database from a Docker container. In this article, we will explore how to access the host’s MySQL database from a Docker container.

Prerequisites To follow along with this article, you will need:

  • Docker installed on your machine
  • A MySQL server running on your host machine
  • Basic knowledge of Docker and MySQL

Connecting to Host’s MySQL from Docker Container To connect to the host’s MySQL server from a Docker container, we need to follow these steps:

Step 1: Make sure your docker container is on the network bridge with the host. you can check it through this guide https://stackoverflow.com/questions/43904562/docker-how-to-find-the-network-my-container-is-in.

Step 2: Log in to MySQL as ROOT on the host.

Step 3: Use mysql database and select * from user to show all users.

Here in the Host column, change localhost to%.

If safer if you create another user and database and access it.

Step 4: If still not working, open bind address mysql to accept from all connection.

edit here /etc/mysql/mysql.conf.d/mysqld.cnf and change bind-address to 0.0.0.0

Conclusion In this article, we have seen how to access the host’s MySQL database from a Docker container. By following the steps outlined above, you can connect to your host’s MySQL server and perform any necessary operations. Docker provides a powerful platform for developing and deploying applications, and being able to access the host’s MySQL database from within a container is an essential feature that can simplify development and deployment workflows.

It should work, if it hasn’t, please comment below, I’ll get back to you as soon as I can. Good luck!

--

--

Fikih Firmansyah
Fikih Firmansyah

Written by Fikih Firmansyah

Back End Developer at Universitas Sumatera Utara || Google Developer Groups Medan

No responses yet