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

Attempts were made using server IP and local IP 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,where is the network bridge( 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
  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