Docker container Mysql connection problem

wo cao pit father



db container running...

docker run -d --name db dbrepos:version


Not only to ensure normal access to the db container, but also to configure
etc/mysql/my.cnf

127.0.0.0 to 0.0.0.0

and need to

log in
mysql -uroot -p



Remove anonymous users
delete from user where user='';




The root user needs to configure remote access

⚠️123456 is the password you want to configure  

grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;

update user set host='%' where user='root' and host='localhost';




Then run the web, which is the container that connects to the db.


[mapport] represents the port you want to map to the host master. For example, 8080:8080
can also choose -P to map randomly.

[webname] represents your web container alias.

--link represents Establish a separate channel communication with the db container.

run -d -p  [mapport] --name [webname]   --link db:db webname:version



The steps are like this..



Also note that if the data cannot be accessed there could be multiple reasons.

Check the running container status. Is it up
docker ps -a    


View the details of a single container, such as IP, mapped port information.

Docker inspect running container id





In addition, some silly reasons may also be caused,

such as database passwords, JDBC JAR packages, etc.

Check them one by one.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326443217&siteId=291194637