以5.7版本为例
1 配置mysql配置文件
编辑/etc/my.cnf,添加以下内容:
[mysqld] skip-host-cache skip-name-resolve datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock secure-file-priv=/var/lib/mysql-files user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
2 创建数据目录
mkdir -p /var/lib/mysql
3 拉取docker镜像
docker pull mysql/mysql-server:5.7
4 启动docker
docker run --name=mysql -p 3306:3306 --mount type=bind,src=/etc/my.cnf,dst=/etc/my.cnf --mount type=bind,src=/var/lib/mysql,dst=/var/lib/mysql -d mysql/mysql-server:5.7
5 授权其它用户访问
grant all privileges on *.* to 'root'@'%' identified by 'root'; flush privileges;