# docker pull mysql:5.7
root@ubuntu:/opt/gb_docker_compose# docker run --rm --name=mysql mysql:5.7
2022-04-02 08:56:23+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started.
2022-04-02 08:56:23+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2022-04-02 08:56:23+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started.
2022-04-02 08:56:24+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
You need to specify one of the following:
- MYSQL_ROOT_PASSWORD
- MYSQL_ALLOW_EMPTY_PASSWORD
- MYSQL_RANDOM_ROOT_PASSWORD
提示得配置个密码
docker run --rm --name=mysql -e MYSQL_ROOT_PASSWORD=123 mysql:5.7
跑开了,但是比较坑的是终止不了程序,肯定处理型号量了,咋办。
再开个终端,stop掉好了。
docker run -p 13306:3306 --rm --name=mysql -d -e MYSQL_ROOT_PASSWORD=123 mysql:5.7
3306端口已经用了,映射的是13306端口。
这次学聪明了,直接加"-d",让后台运行。。
navicat测试一下,没有问题。
跑起来就是空数据库
# 主配置文件
docker container cp mysql:/etc/mysql ./etc_mysql
# 配置目录
docker container cp mysql:/var/log/mysql ./var_log_mysql
# 样式配置文件
docker container cp mysql:/var/lib/mysql ./var_lib_mysql
感觉已经有数据路,拷出来不好使。
# docker run --rm -d --name=mysql mysql:5.7 /bin/sh -c "while true; do echo 'liuyuelong YYDS'; sleep 1; done"
# docker logs -tf mysql
2022-04-02T09:25:28.889633925Z liuyuelong YYDS
2022-04-02T09:25:29.890302299Z liuyuelong YYDS
2022-04-02T09:25:30.892371933Z liuyuelong YYDS
# docker exec -it mysql /bin/bash
# 主配置文件
docker container cp mysql:/etc/mysql ./etc_mysql
# 配置目录
docker container cp mysql:/var/log/mysql ./var_log_mysql
# 样式配置文件
docker container cp mysql:/var/lib/mysql ./var_lib_mysql
这次感觉干净多了,做个压缩包流备份
# tar -czvf docker_mysql_5.7_src_files.tgz ./
./
./etc_mysql/
./etc_mysql/mysql.cnf
./etc_mysql/my.cnf
./etc_mysql/conf.d/
./etc_mysql/conf.d/mysqldump.cnf
./etc_mysql/conf.d/mysql.cnf
./etc_mysql/conf.d/docker.cnf
./etc_mysql/mysql.conf.d/
./etc_mysql/mysql.conf.d/mysqld.cnf
./etc_mysql/my.cnf.fallback
./var_log_mysql/
./var_log_mysql/error.log
./var_lib_mysql/
# docker run \
--rm \
-d \
-p 13306:3306 \
--name=mysql \
-e MYSQL_ROOT_PASSWORD=123 \
-v $(pwd)/etc_mysql:/etc/mysql \
-v $(pwd)/var_log_mysql:/var/log/mysql \
-v $(pwd)/var_lib_mysql:/var/lib/mysql \
mysql:5.7
好了,这样每次启动时,数据应该就可以保留了。不会因为重建容器导致数据丢失了。
# docker exec -it mysql mysql -uroot -p123
mysql> CREATE DATABASE TEST;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| TEST |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> exit
Bye
# docker stop mysql
# docker run \
--rm \
-d \
-p 13306:3306 \
--name=mysql \
-e MYSQL_ROOT_PASSWORD=123 \
-v $(pwd)/etc_mysql:/etc/mysql \
-v $(pwd)/var_log_mysql:/var/log/mysql \
-v $(pwd)/var_lib_mysql:/var/lib/mysql \
mysql:5.7
# docker exec -it mysql mysql -uroot -p123
mysql> show databases;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| TEST |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
mysql> exit
Bye
牛逼,数据没有丢,没有问题。