Top
NSD DBA DAY06
- 案例1:完全備份與恢復
- 案例2:增量備份與恢復
- 案例3:差異備份與恢復
- 案例4:binlog日志
1 案例1:完全備份與恢復
1.1 問題
- 練習物理備份與恢復
- 練習mysqldump備份與恢復
1.2 方案
在數據庫服務器192.168.88.50 練習數據的備份與恢復
1.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:練習物理備份與恢復
冷備份,需停止數據庫服務 適合線下服務器。
備份數據
- [root@mysql50 ~]# systemctl stop mysqld
- [root@mysql50 ~]# mkdir /bakdir 創建備份目錄
- [root@mysql50 ~]# cp -r /var/lib/mysql /bakdir/mysql.bak 拷貝數據源文件
- [root@mysql50 ~]# cd /var/lib/mysql
- [root@mysql50 mysql]# tar -zcvf /bakdir/mysql.tar.gz ./* 打包壓縮數據源文件
- [root@mysql50 mysql]# ls /bakdir/ 查看備份文件
- mysql.bak mysql.tar.gz
刪除數據
- [root@mysql50 ~]# rm -rf /var/lib/mysql/*
恢復數據
- [root@mysql50 ~]# tar -xf /bakdir/mysql.tar.gz -C /var/lib/mysql/
- [root@mysql50 ~]# systemctl start mysqld
- [root@mysql50 ~]# mysql -uroot -pNSD2023...a
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | GAMEDB |
- | db1 |
- | home |
- | information_schema |
- | mysql |
- | performance_schema |
- | studb |
- | sys |
- | tarena |
- | 學生庫 |
- +--------------------+
- 10 rows in set (0.00 sec)
也可使用cp拷貝的備份文件恢復數據
- [root@mysql50 ~]# systemctl stop mysqld
- [root@mysql50 ~]# rm -rf /var/lib/mysql/*
- [root@mysql50 ~]# cp -r /bakdir/mysql.bak/* /var/lib/mysql/
- [root@mysql50 ~]# chown -R mysql:mysql /var/lib/mysql
- [root@mysql50 ~]# systemctl start mysqld
- [root@mysql50 ~]# mysql -uroot -pNSD2023...a
步驟二:練習mysqldump備份與恢復
熱備份,備份和恢復數據庫服務必須是運行的
- //備份1張表
- [root@mysql50 ~]# mysqldump -uroot -pNSD2023...a tarena salary > /bakdir/tarena_salary.sql
- mysqldump: [Warning] Using a password on the command line interface can be insecure.
- //備份多張表
- [root@mysql50 ~]# mysqldump -uroot -pNSD2023...a tarena employees departments > /bakdir/tarena_employees_deparments.sql
- mysqldump: [Warning] Using a password on the command line interface can be insecure.
- //備份1個庫
- [root@mysql50 ~]# mysqldump -uroot -pNSD2023...a -B tarena > /bakdir/tarena.sql
- mysqldump: [Warning] Using a password on the command line interface can be insecure.
- //備份多個庫
- [root@mysql50 ~]# mysqldump -uroot -pNSD2023...a -B studb db1 > /bakdir/studb_db1.sql
- mysqldump: [Warning] Using a password on the command line interface can be insecure.
- //備份所有庫
- [root@mysql50 ~]# mysqldump -uroot -pNSD2023...a -A > /bakdir/allbak.sql
- mysqldump: [Warning] Using a password on the command line interface can be insecure.
- [root@mysql50 ~]#
恢復數據(覆蓋恢復數據)
- [root@mysql50 ~]# mysql -uroot -pNSD2023...a
- mysql> drop database tarena; //刪除庫
- Query OK, 6 rows affected (0.57 sec)
- mysql> exit
- Bye
- [root@mysql50 ~]# mysql -uroot -pNSD2023...a < /bakdir/tarena.sql //恢復數據
- mysql: [Warning] Using a password on the command line interface can be insecure.
- [root@mysql50 ~]# mysql -uroot -pNSD2023...a //登陸
- mysql> use tarena; //進庫
- mysql> show tables; //看表
- +------------------+
- | Tables_in_tarena |
- +------------------+
- | departments |
- | employees |
- | salary |
- | stu4 |
- | user |
- | wage_grade |
- +------------------+
- 6 rows in set (0.00 sec)
- mysql> delete from salary; //刪除表記錄
- Query OK, 8055 rows affected (0.11 sec)
- mysql> exit
- Bye
- [root@mysql50 ~]#
- //使用備份文件恢復數據
- [root@mysql50 ~]# mysql -uroot -pNSD2023...a tarena < /bakdir/tarena_salary.sql
- mysql: [Warning] Using a password on the command line interface can be insecure.
- [root@mysql50 ~]# mysql -uroot -pNSD2023...a //登陸服務
- mysql> select count(*) from tarena.salary; //查看行數
- +----------+
- | count(*) |
- +----------+
- | 8055 |
- +----------+
- 1 row in set (0.00 sec)
分析:
Mysqldump 備份和恢復數據時會鎖表,鎖表期間無法對表做寫訪問,mysqldump適合備份數據量比較小的數據或在數據庫服務器訪問量少的時候備份。
2 案例2:增量備份與恢復
2.1 問題
- 練習數據增量備份
- 練習數據增量恢復
2.2 方案
2.3 準備2臺數據庫服務器,如表-1所示
?
增量備份:備份上次備份后,新產生的數據。
PERCONA Xtrabackup是一款強大的在線熱備份工具,備份過程中不鎖庫表,適合生產環境。支持完全備份與恢復、增量備份與恢復、差異備份與恢復。
在192.168.88.50主機完成備份與恢復的練習 。
2.4 步驟
實現此案例需要按照如下步驟進行。
步驟一:練習數據增量備份
安裝軟件(在mysql50 、MySQL51 兩臺主機都要安裝)
- //把軟件拷貝到虛擬機里
- [openeuler@server1 ~]$ scp /linux-soft/s3/percona-xtrabackup-8.0.26-18-Linux-x86_64.glibc2.12-minimal.tar.gz root@192.168.88.50:/root/
- //安裝依賴
- [root@host50 ~]# yum -y install perl-DBD-MySQL
- //解壓源碼
- [root@host50 ~ ]# tar -xf percona-xtrabackup-8.0.26-18-Linux-x86_64.glibc2.12-minimal.tar.gz
- //移動并改名
- [root@host50 ~ ]# mv percona-xtrabackup-8.0.26-18-Linux-x86_64.glibc2.12-minimal /usr/local/percona
- //把命令添加到系統環境變量
- [root@host50 ~ ]# vim /etc/bashrc
- export PATH=/usr/local/percona/bin:$PATH 添加在文件末尾
- :wq
- [root@host50 ~ ]# source /etc/bashrc
- //查看幫助信息
- [root@host50 ~ ]# man xtrabackup (按q 退出)
增量備份(在mysql50主機 完成增量備份練習)
對數據做增量備份前,必須先有一次備份,也就是首次備份,通常是備份所有數據;比如每周周一完全備份,周二到周日增量備份。
周一完全備份(備份所有數據)
- [root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=NSD2023...a --backup --target-dir=/fullbak --datadir=/var/lib/mysql
- ……
- ……
- 230530 18:18:48 [00] ...done
- xtrabackup: Transaction log of lsn (24822878) to (24822898) was copied.
- 230530 18:18:50 completed OK!
- [root@mysql50 ~]#
- //插入新數據 (可以插入多行)
- mysql> insert into tarena.salary(date,employee_id,basic,bonus)values("20230610",18,25000,8000);
周二增量備份(備份周一備份后新產生的數據)
- [root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=NSD2023...a --backup --target-dir=/new2 --incremental-basedir=/fullbak --datadir=/var/lib/mysql
- ……
- ……
- 230530 18:33:52 [00] ...done
- xtrabackup: Transaction log of lsn (24827173) to (24827183) was copied.
- 230530 18:33:53 completed OK!
- [root@mysql50 ~]#
- //插入新數據 (可以插入多行)
- mysql> insert into tarena.salary(date,employee_id,basic,bonus)values("20230710",18,25000,8000);
周三增量備份(備份周二備份后新產生的數據)
- [root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=NSD2023...a --backup --target-dir=/new3 --incremental-basedir=/new2 --datadir=/var/lib/mysql
- ……
- ……
- 230530 18:46:17 [00] ...done
- xtrabackup: Transaction log of lsn (24832526) to (24832536) was copied.
- 230530 18:46:18 completed OK!
- [root@mysql50 ~]#
- //插入新數據 (可以插入多行)
- mysql> insert into tarena.salary(date,employee_id,basic,bonus)values("20230710",18,25000,8000);
周四增量備份(備份周三備份后新產生的數據)
- [root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=NSD2023...a --backup --target-dir=/new4 --incremental-basedir=/new3 --datadir=/var/lib/mysql
- ……
- ……
- 230530 18:53:41 [00] ...done
- xtrabackup: Transaction log of lsn (24837561) to (24837571) was copied.
- 230530 18:53:42 completed OK!
- [root@mysql50 ~]#
- //插入新數據 (可以插入多行)
- mysql> insert into tarena.salary(date,employee_id,basic,bonus)values("20230710",18,25000,8000);
周五增量備份(備份周四備份后新產生的數據)
- [root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=NSD2023...a --backup --target-dir=/new5 --incremental-basedir=/new4 --datadir=/var/lib/mysql
- ……
- ……
- 230530 18:58:50 [00] ...done
- xtrabackup: Transaction log of lsn (24841645) to (24841655) was copied.
- 230530 18:58:51 completed OK!
- [root@mysql50 ~]#
- //插入新數據 (可以插入多行)
- mysql> insert into tarena.salary(date,employee_id,basic,bonus)values("20230710",18,25000,8000);
周六增量備份(備份周五備份后新產生的數據)
- [root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=NSD2023...a --backup --target-dir=/new6 --incremental-basedir=/new5 --datadir=/var/lib/mysql
- ……
- ……
- 230530 19:00:55 [00] ...done
- xtrabackup: Transaction log of lsn (24848404) to (24848414) was copied.
- 230530 19:00:56 completed OK!
- [root@mysql50 ~]#
- //插入新數據 (可以插入多行)
- mysql> insert into tarena.salary(date,employee_id,basic,bonus)values("20230710",18,25000,8000);
周日增量備份(備份周六備份后新產生的數據)
- [root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=NSD2023...a --backup --target-dir=/new7 --incremental-basedir=/new6 --datadir=/var/lib/mysql
- ……
- ……
- 230530 19:00:55 [00] ...done
- xtrabackup: Transaction log of lsn (24848404) to (24848414) was copied.
- 230530 19:00:56 completed OK!
- [root@mysql50 ~]#
步驟二:練習數據增量恢復
增量恢復數據步驟:
- 準備恢復數據
- 合并數據
- 清空數據庫目錄
- 拷貝數據
- 修改數據庫目錄所有者/組用戶為mysql
- 重啟數據庫服務
具體操作如下:
MySQL51 拷貝 MySQL50 的備份文件到 本機的根目錄下
- [root@mysql50 ~]# scp –r root@192.168.88.50:/fullbak /
- [root@mysql50 ~]# scp –r root@192.168.88.50:/new2 /
- [root@mysql50 ~]# scp –r root@192.168.88.50:/new3 /
- [root@mysql50 ~]# scp –r root@192.168.88.50:/new4 /
- [root@mysql50 ~]# scp –r root@192.168.88.50:/new5 /
- [root@mysql50 ~]# scp –r root@192.168.88.50:/new6 /
- [root@mysql50 ~]# scp –r root@192.168.88.50:/new7 /
在MySQL51主機使用備份文件恢復數據
1)、準備恢復數據
- [root@mysql51 ~]# xtrabackup --prepare --apply-log-only --target-dir=/fullbak
- ……
- ……
- Log background threads are being closed...
- Shutdown completed; log sequence number 24822898
- Number of pools: 1
- 230531 14:32:14 completed OK!
- [root@mysql51 ~]#
2)、合并數據
- //將周二的增量數據拷貝到周一備份目錄里,合并后周一的目錄里存放的是周一 + 周二 的數據
- [root@mysql51 ~]# xtrabackup --prepare --apply-log-only --target-dir=/fullbak --incremental-dir=/new2
- ……
- ……
- 230531 14:40:05 [00] Copying /new2/binlog.index to ./binlog.index
- 230531 14:40:05 [00] ...done
- 230531 14:40:05 completed OK!
- [root@mysql51 ~]#
- //將周三的增量數據拷貝到周一備份目錄里,合并后周一的目錄里存放的是周一 + 周二 + 周三 的數據
- [root@mysql51 ~]# xtrabackup --prepare --apply-log-only --target-dir=/fullbak --incremental-dir=/new3
- ……
- ……
- 230531 15:00:37 [00] Copying /new3/binlog.index to ./binlog.index
- 230531 15:00:37 [00] ...done
- 230531 15:00:37 completed OK!
- [root@mysql51 ~]#
- //將周四的增量數據拷貝到周一備份目錄里,合并后周一的目錄里存放的是周一 + 周二 + 周三 +周四的數據
- [root@mysql51 ~]# xtrabackup --prepare --apply-log-only --target-dir=/fullbak --incremental-dir=/new4
- ……
- ……
- 230531 15:00:37 [00] Copying /new4/binlog.index to ./binlog.index
- 230531 15:00:37 [00] ...done
- 230531 15:00:37 completed OK!
- [root@mysql51 ~]#
- //將周五的增量數據拷貝到周一備份目錄里,合并后周一的目錄里存放的是周一 + 周二 + 周三 +周四+周五的數據
- [root@mysql51 ~]# xtrabackup --prepare --apply-log-only --target-dir=/fullbak --incremental-dir=/new5
- ……
- ……
- 230531 15:00:37 [00] Copying /new5/binlog.index to ./binlog.index
- 230531 15:00:37 [00] ...done
- 230531 15:00:37 completed OK!
- [root@mysql51 ~]#
- //將周六的增量數據拷貝到周一備份目錄里,合并后周一的目錄里存放的是周一 + 周二 + 周三 +周四+周五+周六的數據
- [root@mysql51 ~]# xtrabackup --prepare --apply-log-only --target-dir=/fullbak --incremental-dir=/new6
- ……
- ……
- 230531 15:00:37 [00] Copying /new6/binlog.index to ./binlog.index
- 230531 15:00:37 [00] ...done
- 230531 15:00:37 completed OK!
- [root@mysql51 ~]#
- //將周日的增量數據拷貝到周一備份目錄里,合并后周一的目錄里存放的是周一 + 周二 + 周三 +周四+周五+周六+周日的數據
- [root@mysql51 ~]# xtrabackup --prepare --apply-log-only --target-dir=/fullbak --incremental-dir=/new7
- ……
- ……
- 230531 15:00:37 [00] Copying /new7/binlog.index to ./binlog.index
- 230531 15:00:37 [00] ...done
- 230531 15:00:37 completed OK!
- [root@mysql51 ~]#
- [root@mysql51 ~]# rm -rf /var/lib/mysql/*
- [root@mysql51 ~]# xtrabackup --copy-back --target-dir=/fullbak
- [root@mysql51 ~]# chown -R mysql:mysql /var/lib/mysql
6)重啟數據庫服務
- [root@mysql51 ~]# systemctl restart mysqld
7)連接服務查看數據
- [root@mysql51 ~]# mysql -uroot -pNSD2023...a
- mysql> select count(*) from tarena.salary where date=20230710;
- +----------+
- | count(*) |
- +----------+
- | 75 |
- +----------+
- 1 row in set (0.01 sec)
- mysql> select count(*) from tarena.salary where not date=20230710;
- +----------+
- | count(*) |
- +----------+
- | 8067 |
- +----------+
- 1 row in set (0.00 sec)
3 案例3:差異備份與恢復
3.1 問題
- 練習差異備份
- 練習差異恢復
3.2 方案
差異備份:備份完全備份后,新產生的數據。
PERCONA Xtrabackup是一款強大的在線熱備份工具,備份過程中不鎖庫表,適合生產環境。支持差異備份與恢復。
在192.168.88.50主機完成差異備份
3.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:練習差異備份
差異備份
對數據做差異備份前,必須先有一次備份,也就是首次備份,通常是備份所有數據;比如每周周一完全備份,周二到周日差異備份。
- //周一完全備份
- [root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=NSD2023...a --backup --target-dir=/allbak --datadir=/var/lib/mysql
- ……
- ……
- 230531 17:10:02 [00] Writing /allbak/xtrabackup_info
- 230531 17:10:02 [00] ...done
- xtrabackup: Transaction log of lsn (24881353) to (24881373) was copied.
- 230531 17:10:03 completed OK!
- [root@mysql50 ~]#
- //插入新數據 (可以插入多行)
- mysql> insert into tarena.salary(date,employee_id,basic,bonus)values("20230810",18,25000,8000);
周二差異備份,備份周一備份后新產生的數據
- [root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=NSD2023...a --backup --target-dir=/dir2 --incremental-basedir=/allbak --datadir=/var/lib/mysql
- ……
- ……
- 230531 17:23:56 [00] Writing /dir2/xtrabackup_info
- 230531 17:23:56 [00] ...done
- xtrabackup: Transaction log of lsn (24886741) to (24886751) was copied.
- 230531 17:23:58 completed OK!
- [root@mysql50 ~]#
- //插入新數據 (可以插入多行)
- mysql> insert into tarena.salary(date,employee_id,basic,bonus)values("20230810",18,25000,8000);
周三差異備份,備份周一備份后新產生的數據
- [root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=NSD2023...a --backup --target-dir=/dir3 --incremental-basedir=/allbak --datadir=/var/lib/mysql
- ……
- ……
- 230531 17:27:10 [00] Writing /dir3/xtrabackup_info
- 230531 17:27:10 [00] ...done
- xtrabackup: Transaction log of lsn (24892043) to (24892063) was copied.
- 230531 17:27:11 completed OK!
- [root@mysql50 ~]#
- //插入新數據 (可以插入多行)
- mysql> insert into tarena.salary(date,employee_id,basic,bonus)values("20230810",18,25000,8000);
周四差異備份,備份周一備份后新產生的數據
- [root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=NSD2023...a --backup --target-dir=/dir4 --incremental-basedir=/allbak --datadir=/var/lib/mysql
- ……
- ……
- 230531 17:31:00 [00] Writing /dir4/xtrabackup_info
- 230531 17:31:00 [00] ...done
- xtrabackup: Transaction log of lsn (24900560) to (24900580) was copied.
- 230531 17:31:01 completed OK!
- [root@mysql50 ~]#
- //插入新數據 (可以插入多行)
- mysql> insert into tarena.salary(date,employee_id,basic,bonus)values("20230810",18,25000,8000);
周五差異備份,備份周一備份后新產生的數據
- [root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=NSD2023...a --backup --target-dir=/dir5 --incremental-basedir=/allbak --datadir=/var/lib/mysql
- ……
- ……
- 230531 17:32:38 [00] Writing /dir5/xtrabackup_info
- 230531 17:32:38 [00] ...done
- xtrabackup: Transaction log of lsn (24906902) to (24906912) was copied.
- 230531 17:32:39 completed OK!
- [root@mysql50 ~]#
- //插入新數據 (可以插入多行)
- mysql> insert into tarena.salary(date,employee_id,basic,bonus)values("20230810",18,25000,8000);
周六差異備份,備份周一備份后新產生的數據
- [root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=NSD2023...a --backup --target-dir=/dir6 --incremental-basedir=/allbak --datadir=/var/lib/mysql
- ……
- ……
- 230531 17:41:01 [00] Writing /dir6/xtrabackup_info
- 230531 17:41:01 [00] ...done
- xtrabackup: Transaction log of lsn (24914729) to (24914739) was copied.
- 230531 17:41:02 completed OK!
- [root@mysql50 ~]#
- //插入新數據 (可以插入多行)
- mysql> insert into tarena.salary(date,employee_id,basic,bonus)values("20230810",18,25000,8000);
周日差異,備份備份周一備份后新產生的數據
- [root@mysql50 ~]# xtrabackup --host=127.0.0.1 --user=root --password=NSD2023...a --backup --target-dir=/dir7 --incremental-basedir=/allbak --datadir=/var/lib/mysql
- ……
- ……
- 230531 17:43:16 [00] Writing /dir7/xtrabackup_info
- 230531 17:43:16 [00] ...done
- xtrabackup: Transaction log of lsn (24920772) to (24920782) was copied.
- 230531 17:43:17 completed OK!
- [root@mysql50 ~]#
步驟二:練習差異恢復
差異恢復數據步驟:
- 準備恢復數據
- 合并數據
- 清空數據庫目錄
- 拷貝數據
- 修改數據庫目錄所有者/組用戶為mysql
- 重啟數據庫服務
具體操作如下:
MySQL51 拷貝 MySQL50 的備份文件到 本機的根目錄下
- [root@mysql51 ~]# scp –r root@192.168.88.50:/allbak /
- [root@mysql51 ~]# scp –r root@192.168.88.50:/dir7 /
在MySQL51主機使用備份文件恢復數據
1)、準備恢復數據
- [root@mysql51 ~]# xtrabackup --prepare --apply-log-only --target-dir=/allbak
- ……
- ……
- Log background threads are being closed...
- Shutdown completed; log sequence number 24881373
- Number of pools: 1
- 230531 17:59:06 completed OK!
- [root@mysql51 ~]#
2)、合并數據
- //將周日的差異備份與周一的完全備份合并,因為周日的差異備份包擴周二+周日的所有數據
- [root@mysql51 ~]# xtrabackup --prepare --apply-log-only --target-dir=/allbak --incremental-dir=/dir7
- ……
- ……
- 230531 18:05:08 [00] Copying /dir7/binlog.000029 to ./binlog.000029
- 230531 18:05:08 [00] ...done
- 230531 18:05:08 [00] Copying /dir7/binlog.index to ./binlog.index
- 230531 18:05:08 [00] ...done
- 230531 18:05:08 completed OK!
- [root@mysql51 ~]#
- [root@mysql51 ~]# rm -rf /var/lib/mysql/*
- [root@mysql51 ~]# xtrabackup --copy-back --target-dir=/allbak
- [root@mysql51 ~]# chown -R mysql:mysql /var/lib/mysql
6)重啟數據庫服務
- [root@mysql51 ~]# systemctl restart mysqld
7)連接服務查看數據
- [root@mysql51 ~]# mysql -uroot -pNSD2023...a
- mysql> select count(*) from tarena.salary where date=20230810;
- +----------+
- | count(*) |
- +----------+
- | 75 |
- +----------+
- 1 row in set (0.01 sec)
- mysql> select count(*) from tarena.salary where not date=20230810;
- +----------+
- | count(*) |
- +----------+
- | 8067 |
- +----------+
- 1 row in set (0.00 sec)
4 案例4:binlog日志
4.1 問題
- 查看正在使用的binlog日志文件
- 自定義日志目錄和日志名
- 手動創建新的日志文件
- 練習日志相關命令的使用
- 使用日志恢復數據
4.2 方案
binlog日志介紹:
- 也稱做 二進制日志
- MySQL服務日志文件的一種
- 保存除查詢之外的所有SQL命令
- 可用于數據的備份和恢復
- 配置mysql主從同步的必要條件
- 準備新的數據庫服務器如表-1,做binlog日志的練習
?
4.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:查看正在使用的binlog日志文件
在新創建的數據庫服務器做如下操作:
- [root@mysql52 ~]# yum -y install mysql-server mysql 安裝軟件
- [root@mysql52 ~]# systemctl start mysqld 啟動服務
- [root@mysql52 ~]# mysql 連接服務
- mysql> show master status; 查看日志文件
- +----------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +----------------+----------+--------------+------------------+-------------------+
- | binlog.000001 | 156 | | | |
- +----------------+----------+--------------+------------------+-------------------+
- 1 row in set (0.00 sec)
- 執行查詢命令
- mysql> select count(*) from mysql.user;
- +----------+
- | count(*) |
- +----------+
- | 4 |
- +----------+
- 1 row in set (0.00 sec)
- mysql> show master status; 執行查詢命令 日志偏移量不變
- +----------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +----------------+----------+--------------+------------------+-------------------+
- | binlog.000001 | 156 | | | |
- +----------------+----------+--------------+------------------+-------------------+
- 1 row in set (0.00 sec)
- 執行建庫、建表命令
- mysql> create database db1;
- Query OK, 1 row affected (0.07 sec)
- mysql> create table db1.user(name char(10));
- Query OK, 0 rows affected (0.52 sec)
- mysql> show master status; 執行寫命令 日志偏移量改變
- +----------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +----------------+----------+--------------+------------------+-------------------+
- | binlog.000001 | 535 | | | |
- +----------------+----------+--------------+------------------+-------------------+
- 1 row in set (0.00 sec)
- mysql> insert into db1.user values("jim"); 插入記錄
- Query OK, 1 row affected (0.10 sec)
- mysql> show master status; 執行寫命令 日志偏移量改變
- +----------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +----------------+----------+--------------+------------------+-------------------+
- | binlog.000001 | 809 | | | |
- +----------------+----------+--------------+------------------+-------------------+
- 1 row in set (0.00 sec)
- mysql>
步驟二:自定義日志目錄和日志名
日志文件默認保存在/var/lib/mysql目錄下,默認日志名binlog
- [root@mysql52 ~]# vim /etc/my.cnf.d/mysql-server.cnf
- [mysqld]
- log-bin=/mylog/mysql52 //定義日志目錄和日志文件名(手動添加)
- :wq
- [root@mysql52 ~]# mkdir /mylog 創建目錄
- [root@mysql52 ~]# chown mysql /mylog 修改目錄所有者mysql用戶
- [root@mysql52 ~]# setenforce 0 關閉selinux
- [root@mysql52 ~]# systemctl restart mysqld 重啟服務
- [root@mysql52 ~]# ls /mylog/ 查看日志目錄
- mysql52.000001 mysql52.index
- [root@mysql52 ~]# mysql 登陸服務
- Mysql> show master status ; 查看日志信息
- +----------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +----------------+----------+--------------+------------------+-------------------+
- | mysql52.000001 | 156 | | | |
- +----------------+----------+--------------+------------------+-------------------+
步驟三:手動創建新的日志文件
默認日志文件容量大于1G時會自動創建新的日志文件,在日志文件沒寫滿時,執行的所有寫命令都會保存到當前使用的日志文件里。
- //刷新前查看
- mysql> show master status;
- +----------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +----------------+----------+--------------+------------------+-------------------+
- | mysql52.000001 | 156 | | | |
- +----------------+----------+--------------+------------------+-------------------+
- 1 row in set (0.00 sec)
- mysql> flush logs; //刷新日志
- Query OK, 0 rows affected (0.22 sec)
- mysql> flush logs; //刷新日志
- Query OK, 0 rows affected (0.16 sec)
- mysql> show master status; //刷新一次創建一個新日志
- +----------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +----------------+----------+--------------+------------------+-------------------+
- | mysql52.000003 | 156 | | | |
- +----------------+----------+--------------+------------------+-------------------+
- 1 row in set (0.00 sec)
- //只要服務重啟就會創建新日志
- [root@mysql52 ~]# systemctl restart mysqld
- [root@mysql52 ~]# mysql 連接服務
- Mysql> show master status; 查看日志
- +----------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +----------------+----------+--------------+------------------+-------------------+
- | mysql52.000004 | 156 | | | |
- +----------------+----------+--------------+------------------+-------------------+
- [root@mysql52 ~]#
- //完全備份后創建新的日志文件,創建的日志個數和備份庫的個數一致
- [root@mysql52 ~]# mysqldump --flush-logs mysql user > user.sql
- [root@mysql52 ~]# mysql -e 'show master status'
- +----------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +----------------+----------+--------------+------------------+-------------------+
- | mysql52.000005 | 156 | | | |
- +----------------+----------+--------------+------------------+-------------------+
- [root@mysql52 ~]# mysqldump --flush-logs -B mysql db1 > db_2.sql
- [root@mysql52 ~]# mysql -e 'show master status'
- +----------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +----------------+----------+--------------+------------------+-------------------+
- | mysql52.000007 | 156 | | | |
- +----------------+----------+--------------+------------------+-------------------+
- [root@mysql52 ~]#
步驟四:練習日志相關命令的使用
MySQL服務提供了管理日志的專屬命令,具體練習如下:
- //查看已有的日志文件
- mysql> show binary logs;
- 日志文件名 日志大小(字節) 加密(no/yes)
- +----------------+-----------+-----------+
- | Log_name | File_size | Encrypted |
- +----------------+-----------+-----------+
- | mysql52.000001 | 201 | No |
- | mysql52.000002 | 201 | No |
- | mysql52.000003 | 179 | No |
- | mysql52.000004 | 201 | No |
- | mysql52.000005 | 201 | No |
- | mysql52.000006 | 201 | No |
- | mysql52.000007 | 156 | No |
- +----------------+-----------+-----------+
- 7 rows in set (0.00 sec)
- //查看正在使用的日志
- mysql> show master status;
- +----------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +----------------+----------+--------------+------------------+-------------------+
- | mysql52.000007 | 156 | | | |
- +----------------+----------+--------------+------------------+-------------------+
- 1 row in set (0.00 sec)
- //插入記錄
- mysql> insert into db1.user values("yaya");
- Query OK, 1 row affected (0.04 sec)
- //查看日志文件內容
- mysql> show binlog events in "mysql52.000007";
- Log_name: 日志文件名。
- Pos: 命令在日志文件中的起始位置。
- Event_type: 事件類型,例如 Query、Table_map、Write_rows 等。
- Server_id: 服務器 ID。
- End_log_pos:命令在文件中的結束位置,以字節為單位。
- Info:執行命令信息。
- +----------------+-----+----------------+-----------+-------------+--------------------------------------+
- | Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
- +----------------+-----+----------------+-----------+-------------+--------------------------------------+
- | mysql52.000007 | 4 | Format_desc | 1 | 125 | Server ver: 8.0.26, Binlog ver: 4 |
- | mysql52.000007 | 125 | Previous_gtids | 1 | 156 | |
- | mysql52.000007 | 156 | Anonymous_Gtid | 1 | 235 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
- | mysql52.000007 | 235 | Query | 1 | 306 | BEGIN |
- | mysql52.000007 | 306 | Table_map | 1 | 359 | table_id: 108 (db1.user) |
- | mysql52.000007 | 359 | Write_rows | 1 | 400 | table_id: 108 flags: STMT_END_F |
- | mysql52.000007 | 400 | Xid | 1 | 431 | COMMIT /* xid=649 */ |
- +----------------+-----+----------------+-----------+-------------+--------------------------------------+
- 7 rows in set (0.00 sec)
- //刪除日志文件名之前的所有日志文件
- mysql> purge master logs to "mysql52.000004";
- Query OK, 0 rows affected (0.10 sec)
- //查看已有的日志文件
- mysql> show binary logs;
- +----------------+-----------+-----------+
- | Log_name | File_size | Encrypted |
- +----------------+-----------+-----------+
- | mysql52.000004 | 201 | No |
- | mysql52.000005 | 201 | No |
- | mysql52.000006 | 201 | No |
- | mysql52.000007 | 431 | No |
- +----------------+-----------+-----------+
- 4 rows in set (0.00 sec)
- //刪除所有日志文件,并重新創建日志文件
- mysql> reset master;
- Query OK, 0 rows affected (0.14 sec)
- //查看已有的日志文件 ,僅有第1個文件了
- mysql> show binary logs;
- +----------------+-----------+-----------+
- | Log_name | File_size | Encrypted |
- +----------------+-----------+-----------+
- | mysql52.000001 | 156 | No |
- +----------------+-----------+-----------+
- 1 row in set (0.00 sec)
步驟五:使用日志恢復數據
把查看到的文件內容管道給連接mysql服務的命令執行
恢復數據命令:
mysqlbinlog /目錄/文件名 | mysql –uroot -p密碼
1)在mysql52主機執行如下操:
- //重置日志
- mysql> reset master;
- Query OK, 0 rows affected (0.09 sec)
- //查看日志
- mysql> show master status;
- +----------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +----------------+----------+--------------+------------------+-------------------+
- | mysql52.000001 | 156 | | | |
- +----------------+----------+--------------+------------------+-------------------+
- 1 row in set (0.00 sec)
- //建庫、
- mysql> create database gamedb;
- Query OK, 1 row affected (0.07 sec)
- //建表
- mysql> create table gamedb.t1(name char(10),class char(3));
- Query OK, 0 rows affected (0.55 sec)
- //插入記錄
- mysql> insert into gamedb.t1 values ("yaya","nsd");
- Query OK, 1 row affected (0.08 sec)
- mysql> insert into gamedb.t1 values ("yaya","nsd");
- Query OK, 1 row affected (0.04 sec)
- mysql> insert into gamedb.t1 values ("yaya","nsd");
- Query OK, 1 row affected (0.08 sec)
- //查看表記錄
- mysql> select * from gamedb.t1;
- +------+-------+
- | name | class |
- +------+-------+
- | yaya | nsd |
- | yaya | nsd |
- | yaya | nsd |
- +------+-------+
- 3 rows in set (0.00 sec)
- mysql> exit
- //把日志文件拷貝給恢復數據的服務器,比如 mysql50
- [root@mysql52 ~]# scp /mylog/mysql52.000001 root@192.168.88.50:/root/
- The authenticity of host '192.168.88.50 (192.168.88.50)' can't be established.
- ECDSA key fingerprint is SHA256:t7J3okFd0o+9zTmFCIetvDl6mxGCmc43VoD6C65zico.
- Are you sure you want to continue connecting (yes/no/[fingerprint])? Yes 同意
- Warning: Permanently added '192.168.88.50' (ECDSA) to the list of known hosts.
- root@192.168.88.50's password: mysql50的密碼
- mysql52.000001 100% 1410 1.6MB/s 00:00
- [root@mysql52 ~]#
2)在MySQL50 使用日志恢復數據
- //查看日志
- [root@mysql50 ~]# ls /root/mysql52.000001
- /root/mysql52.000001
- //執行日志恢復數據
- [root@mysql50 ~]# mysqlbinlog /root/mysql52.000001 | mysql -uroot -pNSD2023...a
- mysql: [Warning] Using a password on the command line interface can be insecure.
- //連接服務查看數據
- [root@mysql50 ~]# mysql -uroot -pNSD2023...a -e 'select * from gamedb.t1'
- mysql: [Warning] Using a password on the command line interface can be insecure.
- +------+-------+
- | name | class |
- +------+-------+
- | yaya | nsd |
- | yaya | nsd |
- | yaya | nsd |
- +------+-------+
- [root@mysql50 ~]#