1、创建工作目录
mkdir mysql
2、使用docker-compose.yml 管理
cat mysql/docker-compose.yml
version: ‘3’
services:
mysql:
image: mysql:5.7.22
container_name: mysql
volumes:
– /data/mysql/db:/var/lib/mysql/
– /data/mysql/sock/mysql.sock:/var/run/mysql.sock
– /data/mysql/conf:/etc/mysql/conf.d
ports:
– 3306:3306
command: –character-set-server=utf8 –collation-server=utf8_general_ci
environment:
MYSQL_ROOT_PASSWORD: 1qaz2wsx
MYSQL_USER: test
MYSQL_PASSWORD: 1qaz2wsx
TZ: Asia/Shanghai
3、最后docker-compose up -d 即可
4、mysql数据库登陆
docker exec -it 42ca64902e6a mysql -uroot -p’1qaz2wsx’
5、mysql数据库备份脚本
docker exec -i mysql bash <<‘EOF’
mkdir /backup/mysql/$(date +%Y%m%d)
# 备份指定数据库
mysqldump -uroot -p1qaz2wsx discuz > /backup/mysql/$(date +%Y%m%d)/discuz_$(date +%Y%m%d_%H%M%S).sql
# 备份所有数据库
mysqldump –no-defaults –events –all-databases -uroot -p1qaz2wsx > /backup/mysql/$(date +%Y%m%d)/all_$(date +%Y%m%d_%H%M%S).sql
exit
EOF
mkdir /backup/mysql/$(date +%Y%m%d)
docker cp mysql:/backup/mysql/$(date +%Y%m%d) /backup/mysql/