mysql資料庫備份與恢復
阿新 • • 發佈:2020-12-31
mysql資料庫備份與恢復
資料庫常用備份方案
資料庫備份方案:
- 全量備份
- 增量備份
- 差異備份
備份方案 | 特點 |
---|---|
全量備份 | 全量備份就是指對某一個時間點上的所有資料或應用進行的一個完全拷貝。 資料恢復快。 備份時間長 |
增量備份 | 增量備份是指在一次全備份或上一次增量備份後,以後每次的備份只需備份與前一次相比增加和者被修改的檔案。這就意味著,第一次增量備份的物件是進行全備後所產生的增加和修改的檔案;第二次增量備份的物件是進行第一次增量備份後所產生的增加和修改的檔案,如此類推。 沒有重複的備份資料 備份時間短 恢復資料時必須按一定的順序進行 |
差異備份 | 備份上一次的完全備份後發生變化的所有檔案。 |
mysql備份工具mysqldump
//新建一個庫school,並新建一個表student,插入資料
mysql> create database school; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> use school Database changed mysql> create table student(id int not null primary key auto_increment,name varchar(50),age tinyint); Query OK, 0 rows affected (0.04 sec) mysql> insert student(name,age) values('haruki',20),('akira',18),('neneka',21); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from student; +----+--------+------+ | id | name | age | +----+--------+------+ | 1 | haruki | 20 | | 2 | akira | 18 | | 3 | neneka | 21 | +----+--------+------+ 3 rows in set (0.00 sec)
// 全量備份資料庫
[root@cst ~]# mysqldump -uroot -p1234 --all-databases >all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
//刪除school後恢復
[root@cst ~]# mysql -uroot -p1234 -e 'drop database school;' mysql: [Warning] Using a password on the command line interface can be insecure. [root@cst ~]# mysql -uroot -p1234 -e 'show databases;' mysql: [Warning] Using a password on the command line interface can be insecure. +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ [root@cst ~]# mysql -uroot -p1234 < all.sql mysql: [Warning] Using a password on the command line interface can be insecure. [root@cst ~]# mysql -uroot -p1234 -e 'show databases;' mysql: [Warning] Using a password on the command line interface can be insecure. +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | | sys | +--------------------+ 恢復成功
mysql備份工具mysqldump
//語法:
mysqldump [OPTIONS] database [tables ...]
mysqldump [OPTIONS] --all-databases [OPTIONS]
mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
//常用的OPTIONS:
-uUSERNAME //指定資料庫使用者名稱
-hHOST //指定伺服器主機,請使用ip地址
-pPASSWORD //指定資料庫使用者的密碼
-P# //指定資料庫監聽的埠,這裡的#需用實際的埠號代替,如-P3307
備份例項
MariaDB [cst]> show databases;
+--------------------+
| Database |
+--------------------+
| cst |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)
MariaDB [cst]> show tables;
+---------------+
| Tables_in_cst |
+---------------+
| student |
| student2 |
| student3 |
+---------------+
3 rows in set (0.000 sec)
//備份整個資料庫(全備)
[root@cst ~]# cd /opt/
[root@cst opt]# mkdir mydata
[root@cst opt]# ls
mydata
[root@cst opt]# cd mydata/
[root@cst mydata]# mysqldump -uroot -p6666 --all-databases > all.sql
[root@cst mydata]# ls
all.sql
//備份cst庫
[root@cst mydata]# mysqldump -uroot -p6666 --databases cst > data_cst.sql
[root@cst mydata]# ls
all.sql data_cst.sql
//備份cst庫中的student和student3表
[root@cst mydata]# mysqldump -uroot -p6666 cst student student3 > data_tables.sql
[root@cst mydata]# ls
all.sql data_cst.sql data_tables.sql
恢復例項
//模擬誤刪cst庫
MariaDB [(none)]> drop database cst;
Query OK, 3 rows affected (0.007 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.000 sec)
//恢復cst庫
[root@cst mydata]# mysql -uroot -p6666 < data_cst.sql
[root@cst mydata]# mysql -uroot -p6666 -e 'show databases;'
+--------------------+
| Database |
+--------------------+
| cst |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
//模擬誤刪除cst庫中的student和student3表
MariaDB [cst]> drop table student;
Query OK, 0 rows affected (0.002 sec)
MariaDB [cst]> drop table student3;
Query OK, 0 rows affected (0.002 sec)
MariaDB [cst]> show tables;
+---------------+
| Tables_in_cst |
+---------------+
| student2 |
+---------------+
1 row in set (0.000 sec)
//恢復刪除的表
MariaDB [cst]> source data_tables.sql;
MariaDB [cst]> show tables;
+---------------+
| Tables_in_cst |
+---------------+
| student |
| student2 |
| student3 |
+---------------+
3 rows in set (0.000 sec)
//刪除所有庫
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema | //該庫無法刪除
+--------------------+
1 row in set (0.000 sec)
//恢復所有庫
[root@cst mydata]# mysql -uroot -p6666 < all.sql
[root@cst mydata]# mysql -uroot -p6666 -e 'show databases;'
+--------------------+
| Database |
+--------------------+
| cst |
| information_schema |
| mysql |
+--------------------+
#一個系統庫無法恢復,但不影響正常使用。
差異備份與恢復
mysql差異備份
//開啟MySQL伺服器的二進位制日誌功能
[root@cst ~]# vim /etc/my.cnf
[mysqld]
server-id=1
log-bin=mysql_bin
[root@cst ~]# systemctl restart mariadb.service
//對資料庫進行完全備份
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| cst |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.000 sec)
3 rows in set (0.000 sec)
MariaDB [(none)]> use cst;
Database changed
MariaDB [cst]> show tables;
+---------------+
| Tables_in_cst |
+---------------+
| student |
| student2 |
| student3 |
+---------------+
3 rows in set (0.000 sec)
MariaDB [cst]> select * from student3;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | mana | 20 |
| 2 | kiruya | 15 |
| 3 | akira | 24 |
+----+--------+------+
3 rows in set (0.000 sec)
//完全備份
[root@cst mydata]# mysqldump -uroot --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > all.sql
[root@cst mydata]# ls
all.sql
//增加新內容
MariaDB [cst]> insert into student3(name,age) values('genji',20),('hanzo',124);
Query OK, 2 rows affected (0.001 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [cst]> select * from student3;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | mana | 20 |
| 2 | kiruya | 15 |
| 3 | akira | 24 |
| 4 | genji | 20 |
| 5 | hanzo | 124 |
+----+--------+------+
5 rows in set (0.000 sec)
MariaDB [cst]> update student3 set age = 24 where id = 5;
Query OK, 1 row affected (0.033 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [cst]> select * from student3;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | mana | 20 |
| 2 | kiruya | 15 |
| 3 | akira | 24 |
| 4 | genji | 20 |
| 5 | hanzo | 24 |
+----+--------+------+
5 rows in set (0.000 sec)
5 rows in set (0.000 sec)
mysql差異備份恢復
//模擬刪除cst庫
MariaDB [(none)]> drop database cst;
Query OK, 3 rows affected (0.186 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.000 sec)
//重新整理建立新的二進位制日誌
[root@cst mydata]# cd /var/lib/mysql/
[root@cst mysql]# ls
[root@cst mysql]# ls
aria_log.00000001 ibdata1 mysql_bin.000003
aria_log_control ibtmp1 mysql_bin.index
ib_buffer_pool multi-master.info mysql_upgrade_info
ib_logfile0 mysql performance_schema
ib_logfile1 mysql.sock
//重新整理建立新的二進位制日誌
[root@cst mysql]# mysqladmin -uroot flush-logs
[root@cst mysql]# ls
aria_log.00000001 ibdata1 mysql_bin.000003
aria_log_control ibtmp1 mysql_bin.000004
ib_buffer_pool multi-master.info mysql_bin.index
ib_logfile0 mysql mysql_upgrade_info
ib_logfile1 mysql.sock performance_schema
//恢復完全備份
[root@cst mysql]# mysql -uroot < /opt/mydata/all.sql
[root@cst mysql]# mysql -uroot -e 'show databases;'
+--------------------+
| Database |
+--------------------+
| cst |
| information_schema |
| mysql |
+--------------------+
[root@cst mysql]# mysql -uroot -e 'show tables from cst;'
+---------------+
| Tables_in_cst |
+---------------+
| student |
| student2 |
| student3 |
+---------------+
[root@cst mysql]# mysql -uroot -e 'select * from cst.student3;'
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | mana | 20 |
| 2 | kirue | 15 |
| 3 | akira | 24 |
+----+-------+------+
//恢復差異備份
//選擇刷新出來的前一個日誌檔案,找到最後一次操作的位置
MariaDB [cst]> show binlog events in 'mysql_bin.000003';
*************************** 16. row ***************************
Log_name: mysql_bin.000003
Pos: 946
Event_type: Query
Server_id: 1
End_log_pos: 1029
Info: drop database cst
*************************** 17. row ***************************
Log_name: mysql_bin.000003
Pos: 1029
Event_type: Rotate
Server_id: 1
End_log_pos: 1076
Info: mysql_bin.000004;pos=4
17 rows in set (0.000 sec)
//使用mysqlbinlog恢復差異備份
[root@cst mysql]# mysqlbinlog --stop-position=946 /var/lib/mysql/mysql_bi
[root@cst mysql]# mysql -uroot -e 'select * from cst.student3'
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | mana | 20 |
| 2 | kiruya | 15 |
| 3 | akira | 24 |
| 4 | genji | 20 |
| 5 | hanzo | 24 |
+----+--------+------+