Database connection failed,Failed to connect to MySQL

Viewed 169

How can the database connect to the local non container MySQL of the host computer when the project is deployed using Docker?

Deploy using https://bt.cn

Attempts were made using server IP and local IP 127.0.0.1 localhost, but they were unable to connect to a non container MySQL database

2 Answers

After trying to use the host network, the container no longer has mapped port 9080:80, and the container cannot start automatic deletion. After using the original method again

docker run -d -p 9080:80 -v answer-data:/data --name answer apache/answer:latest
  1. http://localhost:9080/install Step 2: Fill in the database;
  2. When connecting to a non container MySQL database, use the database address 172.17.0.2:3306,where 172.17.0.2 is the network bridge(172.17.0.0/16) IP address assigned to the Answer container;
  3. The access permission for non container databases is set to everyone. For security reasons, I have set it to the specified IP address, which is 172.17.0.2
  4. Now, the Docker deployed Answer has successfully connected to the non container MySQL database

Using the host network is an easy way to solve it.

docker run --network host -d -p 9080:80 -v answer-data:/data --name answer apache/answer:1.3.0

Using the host network that can access the host Mysql at 127.0.0.1:3306