下载镜像:
docker pull mysql:5.7.29
注意事项:
- docker/mysql 创建本地目录(必须空的,不能有其他不必要的目录),或已有的 MySQL DataDir;
- 创建 /Volumes/Study/data/docker/mysql-conf 目录,其下有 my.cnf;
- copy mysql 安装目录下的 share 到 mysql-conf 下;
- 创建 tmp 目录 到 mysql-conf 下;
- 请确保 mysql-conf 下的文件为 777 权限(因为容器内以 mysql 用户启动,没权限读写不行);
创建容器:
shell
docker create --name mysql-db \
-v /Volumes/Study/data/docker/mysql/data:/var/lib/mysql \
-v /Volumes/Study/data/docker/mysql:/etc/mysql/conf.d \
-v /Volumes/Study/data/docker/mysql/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf \
-e MYSQL_ROOT_PASSWORD=123456 \
-p 3306:3306 mysql:5.7.29
my.cnf 内容:(mysql-conf 在创建容器的时候挂载到:/etc/mysql/conf.d为前提)
[client]
port=3306
socket=/var/lib/mysql/mysqld.sock
[mysql]
default-character-set=utf8
socket=/var/lib/mysql/mysqld.sock
[mysqld_safe]
socket=/var/lib/mysql/mysqld.sock
nice=0
[mysqld]
user=mysql
tmpdir=/etc/mysql/conf.d/tmp
#Path to the database root
datadir=/var/lib/mysql
pid-file=/var/lib/mysql/mysqld.pid
log-error=/var/lib/mysql/mysql.err
#此处为language配置项
language=/etc/mysql/conf.d/share/english
socket=/var/lib/mysql/mysqld.sock
# ... 其它配置
log-bin = mysql-bin
log-bin-index = mysql-bin.index