数据库备份,数据库为school,素材如下
1.创建student和score表
CREATE TABLE student (
id INT(10) NOT NULL UNIQUE PRIMARY KEY ,
name VARCHAR(20) NOT NULL ,
sex VARCHAR(4) ,
birth YEAR,
department VARCHAR(20) ,
address VARCHAR(50)
);
创建score表。SQL代码如下:
CREATE TABLE score (
id INT(10) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT ,
stu_id INT(10) NOT NULL ,
c_name VARCHAR(20) ,
grade INT(10)
);
2.为student表和score表增加记录
向student表插入记录的INSERT语句如下:
INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');
INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');
INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');
INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');
INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');
INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');
向score表插入记录的INSERT语句如下:
INSERT INTO score VALUES(NULL,901, '计算机',98);
INSERT INTO score VALUES(NULL,901, '英语', 80);
INSERT INTO score VALUES(NULL,902, '计算机',65);
INSERT INTO score VALUES(NULL,902, '中文',88);
INSERT INTO score VALUES(NULL,903, '中文',95);
INSERT INTO score VALUES(NULL,904, '计算机',70);
INSERT INTO score VALUES(NULL,904, '英语',92);
INSERT INTO score VALUES(NULL,905, '英语',94);
INSERT INTO score VALUES(NULL,906, '计算机',90);
INSERT INTO score VALUES(NULL,906, '英语',85);
3.备份数据库school到/backup目录
4.备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库
5.直接将MySQL数据库压缩备份
6.备份MySQL数据库某个(些)表。此例备份student表
7.同时备份多个MySQL数据库(其他数据库素材自行准备)
8.仅仅备份数据库结构
9.备份服务器上所有数据库
10.还原MySQL数据库
11.还原压缩的MySQL数据库
12.使用xtrabackup 备份数据库
13.在另外的数据库服务器上还原xtrabackup 备份
14.使用mydumper备份数据库
15.使用mydumper恢复数据库文章来源:https://www.toymoban.com/news/detail-600385.html
1、创建student和score表
mysql5.7 [(none)]>create database school;
Query OK, 0 rows affected (0.00 sec)
mysql5.7 [(none)]>show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql5.7 [school]>create table student(
-> id int(10) not null unique primary key,
-> name varchar(20) not null,
-> sex varchar(4),
-> birth year,
-> department varchar(20),
-> address varchar(50)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql5.7 [school]>create table score(
-> id int(10) not null unique primary key auto_increment,
-> stu_id int(10) not null,
-> c_name varchar(20),
-> grade int(10)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql5.7 [school]>
文章来源地址https://www.toymoban.com/news/detail-600385.html
2、为student表和score表增加记录
向student表插入记录的INSERT语句如下:
mysql5.7 [school]>INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');
Query OK, 1 row affected (0.00 sec)
mysql5.7 [school]>INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');
Query OK, 1 row affected (0.00 sec)
mysql5.7 [school]>INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');
Query OK, 1 row affected (0.00 sec)
mysql5.7 [school]>INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');
Query OK, 1 row affected (0.00 sec)
mysql5.7 [school]>INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');
Query OK, 1 row affected (0.00 sec)
mysql5.7 [school]>INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');
Query OK, 1 row affected (0.00 sec)
向score表插入记录的INSERT语句如下:
mysql5.7 [school]>INSERT INTO score VALUES(NULL,901, '计算机',98);
Query OK, 1 row affected (0.00 sec)
mysql5.7 [school]>INSERT INTO score VALUES(NULL,902, '英语',80);
Query OK, 1 row affected (0.00 sec)
mysql5.7 [school]>INSERT INTO score VALUES(NULL,902, '计算机',65);
Query OK, 1 row affected (0.00 sec)
mysql5.7 [school]>INSERT INTO score VALUES(NULL,902, '中文',88);
Query OK, 1 row affected (0.00 sec)
mysql5.7 [school]>INSERT INTO score VALUES(NULL,903, '中文',95);
Query OK, 1 row affected (0.00 sec)
mysql5.7 [school]>INSERT INTO score VALUES(NULL,904, '计算机',70);
Query OK, 1 row affected (0.00 sec)
mysql5.7 [school]>INSERT INTO score VALUES(NULL,904, '英语',92);
Query OK, 1 row affected (0.00 sec)
mysql5.7 [school]>INSERT INTO score VALUES(NULL,905, '英语',94);
Query OK, 1 row affected (0.00 sec)
mysql5.7 [school]>INSERT INTO score VALUES(NULL,906, '计算机',90);
Query OK, 1 row affected (0.00 sec)
mysql5.7 [school]>INSERT INTO score VALUES(NULL,906, '英语',85);
Query OK, 1 row affected (0.01 sec)
查看表的信息:
mysql5.7 [school]>select * from student;
+-----+-----------+------+-------+--------------+--------------------+
| id | name | sex | birth | department | address |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+-----------+------+-------+--------------+--------------------+
6 rows in set (0.00 sec)
mysql5.7 [school]>select * from score;
+----+--------+-----------+-------+
| id | stu_id | c_name | grade |
+----+--------+-----------+-------+
| 1 | 901 | 计算机 | 98 |
| 2 | 902 | 英语 | 80 |
| 3 | 902 | 计算机 | 65 |
| 4 | 902 | 中文 | 88 |
| 5 | 903 | 中文 | 95 |
| 6 | 904 | 计算机 | 70 |
| 7 | 904 | 英语 | 92 |
| 8 | 905 | 英语 | 94 |
| 9 | 906 | 计算机 | 90 |
| 10 | 906 | 英语 | 85 |
+----+--------+-----------+-------+
10 rows in set (0.00 sec)
mysql5.7 [school]>
3、备份数据库school到/backup目录
[root@localhost ~]# mkdir /backup
[root@localhost ~]# mysqldump -uroot -p'Admin123!' -B school > /backup/db1
[root@localhost backup]# ll
总用量 4
-rw-r--r-- 1 root root 3443 7月 22 14:10 db1
4、备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库
[root@localhost backup]# mysqldump -uroot -p'Admin123!' --add-drop-table -d school > /backup/add-drop-db
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost backup]# ll
总用量 8
-rw-r--r-- 1 root root 2305 7月 22 14:11 add-drop-db
5、直接将MySQL数据库压缩备份
[root@localhost ~]# mysqldump -uroot -pAdmin123! -B school | gzip > /backup/dbzip
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# ll /backup/
总用量 12
-rw-r--r-- 1 root root 2305 7月 22 14:11 add-drop-db
-rw-r--r-- 1 root root 3443 7月 22 14:11 db1
-rw-r--r-- 1 root root 1202 7月 22 14:13 dbzip
6、备份MySQL数据库某个(些)表。此例备份student表
[root@localhost ~]# mydumper -uroot -pAdmin123! school student > /backup/dbschool_student
[root@localhost ~]# ll /backup/
总用量 12
-rw-r--r-- 1 root root 2305 7月 22 14:11 add-drop-db
-rw-r--r-- 1 root root 3443 7月 22 14:11 db1
-rw-r--r-- 1 root root 0 7月 22 14:15 dbschool_student
-rw-r--r-- 1 root root 1202 7月 22 14:13 dbzip
7、同时备份多个MySQL数据库(其他数据库素材自行准备)
[root@localhost ~]# mysqldump -uroot -pAdmin123! -B school wp > /backup/db_databases
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# ll /backup/
总用量 16
-rw-r--r-- 1 root root 2305 7月 22 14:11 add-drop-db
-rw-r--r-- 1 root root 3443 7月 22 14:11 db1
-rw-r--r-- 1 root root 3574 7月 22 14:17 db_databases
-rw-r--r-- 1 root root 0 7月 22 14:15 dbschool_student
-rw-r--r-- 1 root root 1202 7月 22 14:13 dbzip
8、仅仅备份数据库结构
[root@localhost ~]# mysqldump -uroot -pAdmin123! -d school > /backup/school_stru
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# ll /backup/
总用量 20
-rw-r--r-- 1 root root 2305 7月 22 14:11 add-drop-db
-rw-r--r-- 1 root root 3443 7月 22 14:11 db1
-rw-r--r-- 1 root root 3574 7月 22 14:17 db_databases
-rw-r--r-- 1 root root 0 7月 22 14:15 dbschool_student
-rw-r--r-- 1 root root 1202 7月 22 14:13 dbzip
-rw-r--r-- 1 root root 2305 7月 22 14:19 school_stru
9、备份服务器上所有数据库
[root@localhost ~]# mysqldump -uroot -pAdmin123! -A > /backup/db_all
[root@localhost ~]# ll /backup/
总用量 784
-rw-r--r-- 1 root root 2305 7月 22 14:11 add-drop-db
-rw-r--r-- 1 root root 3443 7月 22 14:11 db1
-rw-r--r-- 1 root root 779829 7月 22 14:20 db_all
-rw-r--r-- 1 root root 3574 7月 22 14:17 db_databases
-rw-r--r-- 1 root root 0 7月 22 14:15 dbschool_student
-rw-r--r-- 1 root root 1202 7月 22 14:13 dbzip
-rw-r--r-- 1 root root 2305 7月 22 14:19 school_stru
10、还原MySQL数据库
(1)先删除数据库school
mysql5.7 [(none)]>drop database school;
Query OK, 2 rows affected (0.01 sec)
(2)还原数据库school
mysql5.7 [(none)]>source /backup/db1
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Database changed
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
(3)查看数据库列表
mysql5.7 [school]>show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
| wp |
+--------------------+
6 rows in set (0.00 sec)
mysql5.7 [school]>select * from student;
+-----+-----------+------+-------+--------------+--------------------+
| id | name | sex | birth | department | address |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+-----------+------+-------+--------------+--------------------+
6 rows in set (0.00 sec)
mysql5.7 [school]>
11、还原压缩的MySQL数据库
(1)先删除数据库school
mysql5.7 [school]>drop database school;
Query OK, 2 rows affected (0.01 sec)
(2)还原压缩的数据库school
[root@localhost backup]# zcat dbzip | mysql -uroot -pAdmin123!
(3)查看还原情况
mysql5.7 [(none)]>show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
| wp |
+--------------------+
6 rows in set (0.00 sec)
mysql5.7 [school]>show tables;
+------------------+
| Tables_in_school |
+------------------+
| score |
| student |
+------------------+
2 rows in set (0.00 sec)
12、使用xtrabackup 备份数据库
(1)安装xtrabackup
[root@localhost tools]# ls
percona-xtrabackup-24-2.4.24-1.el7.x86_64.rpm
[root@localhost tools]# yum localinstall percona-xtrabackup-24-2.4.24-1.el7.x86_64.rpm -y
(2)备份
[root@localhost backup]# innobackupex -u root -p Admin123! --socket=/tmp/mysql.sock
[root@localhost mysql.sock]# ls
2023-07-22_16-31-51
[root@localhost mysql.sock]# ls 2023-07-22_16-31-51/
backup-my.cnf ibdata1 school xtrabackup_checkpoints
db_itheima mysql sys xtrabackup_info
ib_buffer_pool performance_schema wp xtrabackup_logfile
[root@localhost mysql.sock]#
13、在另外的数据库服务器上还原xtrabackup 备份
[root@localhost mysql]# innobackupex --apply-log /tmp/mysql.sock/2023-07-22_16-31-51/
[root@localhost mysql]# systemctl stop mysqld.service
[root@localhost mysql]# rm -rf /var/lib/mysql
[root@localhost mysql]# innobackupex --copy-back /tmp/mysql.sock/2023-07-22_16-31-51/
[root@localhost mysql]# chown -R mysql:mysql /var/lib/mysql
[root@localhost mysql]# systemctl restart mysqld
14、使用mydumper备份数据库
[root@localhost ~]# mydumper -u root -p Admin123! -S /tmp/mysql.sock -B school -o /backup/
15、使用mydumper恢复数据库
[root@localhost ~]# myloader -u root -p Admin123! -S /tmp/mysql.sock -d /backup/ -o -B school
到了这里,关于数据库运维——备份恢复的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!