Docker container accesses the host's MySQL operation

Docker container accesses the host's MySQL operation

background:

There is a flask project that provides an interface, which is built and run using a docker container. MySQL runs on the host machine, and you need to allow flask to connect to the host machine's mysql in the container.

Using the ifconfig command, you can see that there is a docker0 and eth0. In the docker container, you can connect to the host's MySQL through the IP address of eth0 plus the port number (3306); in addition, nginx can access the container through the IP address of docker0 plus the port number specified when building the container.

Supplement: The mysql container of docker runs normally, but the host machine cannot connect to the database. Here is the solution

Docker runs mysql container normally, but host Navicat cannot connect

I executed docker ps on docker and the display was as follows:

This means that my mysql is normal, but I can't connect to my database using sqlyog and Navicat. Later I found the problem. Although I succeeded in creating a mysql container (docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.6.35), there was a prompt that I neglected:

It is prompted here that the network is unavailable, so the host cannot connect to the database

Solution: Now enter this command vim /usr/lib/sysctl.d/00-system.conf (if vim doesn't work, just enter vi):

This will appear immediately:

Add this sentence at the end: net.ipv4.ip_forward=1 (the added command is as follows: press the i key first, then you can operate) and then save and exit (after writing the command net.ipv4.ip_forward=1, press the esc key, then Ctrl+: and finally enter wq! to save and exit), then restart the network service: systemctl restart network First stop the mysql container just now docker stop docker, then delete the container docker rm mysql as follows:

Now you can recreate the mysql container and see the effect:

Is there no warning about the network being unavailable? I connected using sqlyog, and it was the same when I connected using Navicat:

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Docker container custom hosts network access operation
  • Docker port mapping and external inaccessibility issues
  • How to enable remote access in Docker
  • Solution to the problem that Docker container cannot access Jupyter
  • 404 error occurs when accessing the homepage of tomcat started in Docker mode
  • Docker image access to local elasticsearch port operation
  • Solution to docker suddenly not being accessible from the external network

<<:  Some lesser-known sorting methods in MySQL

>>:  js array entries() Get iteration method

Recommend

How to Clear Disk Space on CentOS 6 or CentOS 7

Following are the quick commands to clear disk sp...

Nine advanced methods for deduplicating JS arrays (proven and effective)

Preface The general methods are not listed here, ...

WeChat applet realizes taking photos and selecting pictures from albums

This article shares the specific code for WeChat ...

VUE implements token login verification

This article example shares the specific code of ...

How to install and configure SSH service in Ubuntu 18.04

Install ssh tool 1. Open the terminal and type th...

Div adaptive height automatically fills the remaining height

Scenario 1: Html: <div class="outer"...

Nodejs error handling process record

This article takes the connection error ECONNREFU...

The front-end must know how to lazy load images (three methods)

Table of contents 1. What is lazy loading? 2. Imp...

Use pictures to realize personalized underline of hyperlinks

Don't be surprised if you see some kind of und...

JavaScript manual implementation of instanceof method

1. Usage of instanceof instanceof operator is use...

Using NTP for Time Synchronization in Ubuntu

NTP is a TCP/IP protocol for synchronizing time o...

Detailed explanation of the lock structure in MySQL

Mysql supports 3 types of lock structures Table-l...

How to create a basic image of the Python runtime environment using Docker

1. Preparation 1.1 Download the Python installati...