1、拉取镜像
docker pull mysql
docker images
2、拉取镜像创建对应的文件夹
/home/mysql/conf
/home/data/data
/home/logs/logs
3、写配置文件
vi /home/mysql/conf/my.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
user = mysql
bind-address = 0.0.0.0
#skip-grant-tables
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
[client]
port=3306
4、创建容器并启动
docker run \
--privileged=true \
-p 3306:3306 \
--name mysql \
-v /home/mysql/conf/my.cnf:/etc/mysql/my.cnf \
-v /home/mysql/logs:/var/log/mysql \
-v /home/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=zh123456 \
-d mysql
4、进入容器设置远程访问
docker exec -it mysql /bin/bash
mysql -u root -p
use mysql;
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'zh123456';
5、其他操作
- WARNING: IPv4 forwarding is disabled. Networking will not work.错误结决
echo "net.ipv4.ip_forward=1" >>/usr/lib/sysctl.d/00-system.conf
systemctl restart network && systemctl restart docker
#配置文件添加添加
skip-grant-tables
#进入容器设置密码
flush privileges;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'zh123456';