docker pull mysql:5.7
docker run -p 3306:3306 --name mysql \ -v /usr/local/docker/mysql/conf:/etc/mysql \ -v /usr/local/docker/mysql/logs:/var/log/mysql \ -v /usr/local/docker/mysql/data:/var/lib/mysql \ -e MYSQL_ROOT_PASSWORD=123456 \ -d mysql:5.7
如果你的容器运行正常,但是无法访问到MySQL,一般有以下几个可能的原因:
防火墙阻拦
# 开放端口: $ systemctl status firewalld $ firewall-cmd --zone=public --add-port=3306/tcp -permanent $ firewall-cmd --reload
# 关闭防火墙: $ sudo systemctl stop firewalld
需要进入docker本地客户端设置远程访问账号
$ sudo docker exec -it mysql bash $ mysql -uroot -p123456
mysql> grant all privileges on *.* to root@'%' identified by "password";
mysql> flush privileges;