docker run -p 13306:3306 --name mysql --restart=always --privileged=true \
-v /data/docker/mysql/log:/var/log/mysql \
-v /data/docker/mysql/data:/var/lib/mysql \
-v /data/docker/mysql/conf:/etc/mysql/conf.d \
-v /etc/localtime:/etc/localtime:ro \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:5.7
注释:
-- name mysql:指定docker容器名叫mysql,当然你可以改成其他的
--restart=always:总是跟随docker启动
--privileged=true:获取宿主机root权限
-v dir_1:dir_2:dir_1指的是宿主机的目录,可以不存在,docker会自动创建,dir_2位容器里面的目录,必须存在,否则容器无法启动
-e MYSQL_ROOT_PASSWORD=123456:指定mysql环境变量,root用户的密码为123456
[root@localhost mysql_data_backup]# docker exec -it mysql /bin/bash
bash-4.2# mysql -uroot -p123456
CREATE DATABASE IF NOT EXISTS kahnDB DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
use kahnDB;
create table student(num int(10) not null unique primary key,name varchar(20) not null,gender varchar(20) not null,birthday datetime,address varchar(50));
insert into student (num,name,gender,birthday,address) VALUES (1,'serena','woman','2000-12-31','shanghai');
insert into student (num,name,gender,birthday,address) VALUES (2,'kahn','nan','2000-01-29','huaian');
#!/usr/bin/env bash
docker_name="mysql"
backup_dir="/data/docker/mysql/mysql_data_backup"
db_name="kahnDB"
db_password="123456"
xtime=`date +%Y-%m-%d_%H:%M:%S_%5N`
docker exec -i ${docker_name} mysqldump -uroot -p${db_password} ${db_name} > ${backup_dir}/${db_name}_${xtime}.sql
点个赞吧老弟