MySQL 备份与还原

关于 Docker 启动 MySQL 参考:Docker 启动 MySQL

查看数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 1. 进入容器
------------------------docker exec -it mysql01 bash
# 2. 连接 mysql
root@65cde980ee00:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 108
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

# 3. 显示所有数据库
mysql> show databases;
+------------------------+
| Database |
+------------------------+
| information_schema |
| jcloud_yunding_db_push |
| mysql |
| order_bff_cmdb |
| performance_schema |
| sys |
+------------------------+
6 rows in set (0.00 sec)

# 4. 使用数据库
mysql> use order_bff_cmdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>
# 5. 显示当前数据库的所有表名
mysql> show tables;
+--------------------------+
| Tables_in_order_bff_cmdb |
+--------------------------+
| api_user |
| input_cfg |
| mapping_cfg |
| output_cfg |
| task_cfg |
+--------------------------+
5 rows in set (0.00 sec)

# 6. 断开 mysql 连接
mysql> exit
Bye
# 7. 退出容器
root@65cde980ee00:/# exit
exit
------------------------

备份 所有数据库

语法:root@65cde980ee00:/# mysqldump -hhost -Pport -uusername -ppassword --all-databases > /备份路径/备份文件

1
2
3
4
5
6
7
8
9
------------------------docker exec -it mysql01 bash
root@65cde980ee00:/# mysqldump -uroot -p123456 --all-databases > /home/all_db.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure. #===> 只是告诉你 密码不应该暴露在命令行
root@65cde980ee00:/#
root@65cde980ee00:/# mysqldump -uroot -p --all-databases > /home/all_db.sql #===> 会把原有的删除,重新建立一份新的文件
Enter password: #===> 输入密码
root@65cde980ee00:/#
root@65cde980ee00:/# mysqldump -h127.0.0.1 -P3306 -uroot -p --all-databases > /home/all_db.sql #===> 指定ip:port【大写:-P端口号,小写:-p密码】
Enter password:

备份 指定数据库

语法:root@65cde980ee00:/# mysqldump -hhost -Pport -uusername -ppassword --databases db1 [db2] [db3] ... > /备份路径/备份文件

1
2
3
4
5
6
7
#===> 备份一个数据库,有“CREATE DATABASE”
root@65cde980ee00:/# mysqldump -h127.0.0.1 -P3306 -uroot -p --databases order_bff_cmdb > /home/order_bff_cmdb.sql
Enter password:

#===> 备份多个数据库“CREATE DATABASE”
root@65cde980ee00:/# mysqldump -h127.0.0.1 -P3306 -uroot -p --databases order_bff_cmdb jcloud_yunding_db_push > /home/order_bff_cmdb+jcloud_yunding_db_push.sql
Enter password:

备份 指定数据库 的所有表

语法:root@65cde980ee00:/# mysqldump -hhost -Pport -uusername -ppassword db > /备份路径/备份文件

1
2
3
#===> 备份数据库 order_bff_cmdb 的所有表
root@65cde980ee00:/# mysqldump -h127.0.0.1 -P3306 -uroot -p order_bff_cmdb > /home/order_bff_cmdb.tables.sql
Enter password:

备份 指定数据库 的指定表

语法:root@65cde980ee00:/# mysqldump -hhost -Pport -uusername -ppassword db table1 [table2] [table3] ... > /备份路径/备份文件

1
2
3
4
5
6
7
#===> 备份数据库 order_bff_cmdb 的 api_user 表
root@65cde980ee00:/# mysqldump -h127.0.0.1 -P3306 -uroot -p order_bff_cmdb api_user > /home/order_bff_cmdb.api_user.sql
Enter password:

#===> 备份数据库 order_bff_cmdb 的 api_user input_cfg 表
root@65cde980ee00:/# mysqldump -h127.0.0.1 -P3306 -uroot -p order_bff_cmdb api_user input_cfg > /home/order_bff_cmdb.api_user+input_cfg.sql
Enter password:

备份问题:gtid,statistics

语法:mysqldump -hhost -Pport -uusername -ppassword --databases db1 --set-gtid-purged=OFF --column-statistics=0 > /root/db1.sql

1、GTID:可使用--set-gtid-purged=OFF
2、mysqldump: Couldn’t execute … Unknown table ‘column_statistics’ in information_schema (1109):可使用--column-statistics=0

错误问题:

1
2
3
4
5
root@f938e9ba4247:/# mysqldump -hhost -uusername -ppassword --databases db1 > /root/db1.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database.
If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.
mysqldump: Couldn't execute 'SELECT COLUMN_NAME,JSON_EXTRACT(HISTOGRAM, '$."number-of-buckets-specified"') FROM information_schema.COLUMN_STATISTICS WHERE SCHEMA_NAME = 'order_bff_cmdb' AND TABLE_NAME = 'api_user';': Unknown table 'column_statistics' in information_schema (1109)

关于 docker cp

  • 从容器拿出来
    docker cp mysql01:/home . #===> 容器 mysql02:/home 目录 拷贝到当前目录(/app/docker/mysql/)

  • 放进容器
    docker cp home mysql02:/home #===> 把当前 home 目录 拷贝到 mysql02:/home 下面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Connecting to 192.168.1.1:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Mon Dec 7 18:42:18 2020 from 192.168.1.8
------------------------
# 把容器目录拷贝到宿主机下面
# 把 容器mysql01 中的 /home 目录 拷贝到 /app/docker/mysql/ 下面
------------------------cd /app/docker/mysql/
------------------------ls
new_db_bak old_db_bak
------------------------
------------------------docker ps -a | grep mysql
65cde980ee00 mysql "docker-entrypoint.s…" 27 hours ago Up 27 hours 0.0.0.0:3306->3306/tcp, 33060/tcp mysql01
------------------------docker cp mysql01:/home . #===> 容器 mysql02:/home 目录 拷贝到当前目录(/app/docker/mysql/)
------------------------ls
home new_db_bak old_db_bak
------------------------
------------------------ll home/
总用量 4168
-rw-r--r--. 1 root root 4024037 12月 7 18:41 all_db.sql
-rw-r--r--. 1 root root 3820 12月 7 18:51 order_bff_cmdb.api_user+input_cfg.sql
-rw-r--r--. 1 root root 2548 12月 7 18:49 order_bff_cmdb.api_user.sql
-rw-r--r--. 1 root root 213286 12月 7 18:44 order_bff_cmdb+jcloud_yunding_db_push.sql
-rw-r--r--. 1 root root 7797 12月 7 18:42 order_bff_cmdb.sql
-rw-r--r--. 1 root root 7595 12月 7 18:49 order_bff_cmdb.tables.sql
------------------------
------------------------



# 启动容器 mysql02
------------------------docker run --name mysql02 -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
fd4dc026523b2e4e169ef45292a52927a6f15f9c4ac6c09fba5d6da77ff2e34e
------------------------
------------------------
------------------------docker exec -it mysql02 ls -lh /home
total 0
------------------------
------------------------docker cp home mysql02:/home #===> 把当前 home 目录 拷贝到 mysql02:/home 下面
------------------------
------------------------docker exec -it mysql02 ls -lh /home
total 4.0K
drwxr-xr-x. 2 root root 4.0K Dec 8 02:51 home
------------------------docker exec -it mysql02 ls -lh /home/home #===> 查看文件
total 4.1M
-rw-r--r--. 1 root root 3.9M Dec 8 02:41 all_db.sql
-rw-r--r--. 1 root root 209K Dec 8 02:44 order_bff_cmdb+jcloud_yunding_db_push.sql
-rw-r--r--. 1 root root 3.8K Dec 8 02:51 order_bff_cmdb.api_user+input_cfg.sql
-rw-r--r--. 1 root root 2.5K Dec 8 02:49 order_bff_cmdb.api_user.sql
-rw-r--r--. 1 root root 7.7K Dec 8 02:42 order_bff_cmdb.sql
-rw-r--r--. 1 root root 7.5K Dec 8 02:49 order_bff_cmdb.tables.sql
------------------------

还原

语法:mysql -uroot -p < /home/home/order_bff_cmdb.sql #===> 导入整个数据库
语法:mysql -uroot -p order_bff_cmdb < /home/home/order_bff_cmdb.api_user.sql #===> 导入表,需要指定数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 1. 进入容器
------------------------docker exec -it mysql02 bash

# 2. 执行备份数据库 sql
root@fd4dc026523b:/# mysql -uroot -p < /home/home/order_bff_cmdb.sql
Enter password: #===> 输入密码
root@fd4dc026523b:/#
root@fd4dc026523b:/# mysql -uroot -p123456 #===> 连接 mysql
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases; #===> 查看所有数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| order_bff_cmdb | #===> 创建了数据库
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)

mysql> exit #===> 断开连接
Bye
root@fd4dc026523b:/#
# 3. 执行备份的表,需要指定数据库
root@fd4dc026523b:/# mysql -uroot -p order_bff_cmdb < /home/home/order_bff_cmdb.api_user.sql
Enter password:
root@fd4dc026523b:/#
root@fd4dc026523b:/# exit #===> 退出容器
exit
------------------------