mysql5.7 on docker
Linux
- 使用mysql 5.7 docker镜像
- my.cnf文件放置在宿主机/etc/mysql/下
- 数据存储在宿主机 /data/mysql下
- 端口对外端口使用50000
#拉取镜像
docker pull mysql:5.7
#mkdir /etc/mysql/
cat > /etc/mysql/my.cnf <<EOF
[mysqld]
#skip-name-resolve
port = 3306
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
skip-character-set-client-handshake=1
default-storage-engine=INNODB
max_allowed_packet = 500M
explicit_defaults_for_timestamp=1
long_query_time = 10
EOF
mkdir -p /data/mysql
docker run --name mysql5.7 -v /etc/mysql:/etc/mysql/conf.d -v /data/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d --restart=always -p 50000:3306 mysql:5.7
#mysql5.7是指新mysql docker的名称
#my-secret-pw是指mysql的root密码
#mysql:5.7是指docker镜像的名称
#端口映射-p 50000:3306
进入已启动的mysql docker
docker exec -it mysql5.7 bash
查看mysql日志
docker logs mysql5.7
备份数据库 mysql5.7是容器的名字
mkdir -p /data/backup/
docker exec mysql5.7 sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /data/backup/all-databases.sql
Windows
# 拉取镜像
docker pull mysql:5.7
# my.cnf文件(D:\Tools\DockerImage\mysql\one5000\config)
[mysqld]
#skip-name-resolve
port = 3306
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
skip-character-set-client-handshake=1
default-storage-engine=INNODB
max_allowed_packet = 500M
explicit_defaults_for_timestamp=1
long_query_time = 10
# 运行容器
docker run -p 50000:3306 --name mysqlone50000 -v d:/Tools/DockerImage/mysql/one5000/config:/etc/mysql/conf.d -v d:/Tools/DockerImage/mysql/one5000/data:/var/lib/mysql -v d:/Tools/DockerImage/mysql/one5000/log:/var/log/mysql -e MYSQL_ROOT_PASSWORD=123456 -d --restart=always mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --default-time-zone="+8:00"