数据库备份,数据库为school,素材如下
1.创建student和score表
create table student(
id int 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 primary key auto_increment,
stu_id int not null,
c_name varchar(20),
grade int
);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);
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| Student |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
mysql> select * from student;
+-----+-----------+------+-------+--------------+--------------------+
| id | name | sex | birth | department | address |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+-----------+------+-------+--------------+--------------------+
mysql> select * from score;
+----+--------+-----------+-------+
| id | stu_id | c_name | grade |
+----+--------+-----------+-------+
| 1 | 901 | 计算机 | 98 |
| 2 | 901 | 英语 | 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 |
+----+--------+-----------+-------+
3.备份数据库school到/backup目录
[root@localhost ~]# mkdir /backup
[root@localhost ~]# mysqldump -uroot -pMySQL@123 -B school > /backup/db1.sql
4.备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库
[root@localhost ~]# mysqldump --add-drop-table -uroot -pMySQL@123 -d school > /backup/dbdrop.sql
5.直接将MySQL数据库压缩备份
[root@localhost ~]# mysqldump -uroot -pMySQL@123 school | gzip > /backup/dbgzip.sql.gz
6.备份MySQL数据库某个(些)表。此例备份student表
[root@localhost ~]# mysqldump -uroot -pMySQL@123 -p school student > /backup/dbtable.sql
7.同时备份多个MySQL数据库(其他数据库素材自行准备)
[root@localhost ~]# mysqldump -uroot -pMySQL@123 -B school Student > /backup/dbmore.sql
8.仅仅备份数据库结构
[root@localhost ~]# mysqldump -uroot -pMySQL@123 -d school > /backup/dbstrcu.sql
9.备份服务器上所有数据库
[root@localhost ~]# mysqldump -uroot -pMySQL@123 -A > /backup/dball.sql
10.还原MySQL数据库
[root@localhost ~]# mysql -uroot -pMySQL@123 -e 'source /backup/db1.sql'
11.还原压缩的MySQL数据库
[root@localhost ~]# gunzip < /backup/dbgzip.sql.gz | mysql -uroot -pMySQL@123 school
12.使用xtrabackup 备份数据库
安装
[root@localhost ~]# ll
-rw-r--r--. 1 root root 7722148 12月 24 22:28 percona-xtrabackup-24-2.4.8-1.el7.x86_64.rpm
[root@localhost ~]# yum install percona-xtrabackup-24-2.4.8-1.el7.x86_64.rpm
备份
[root@localhost ~]# innobackupex -u root -p MySQL@123 -S /tmp/mysql.sock /server/backup
查看
[root@localhost ~]# ls /server/backup/2021-12-24_22-32-26/
backup-my.cnf ibdata1 performance_schema Student xtrabackup_checkpoints xtrabackup_logfile
ib_buffer_pool mysql school sys xtrabackup_info
13.在另外的数据库服务器上还原xtrabackup 备份
[root@localhost ~]# mkdir -p /server/backup/
[root@localhost ~]# scp -r root@192.168.159.136:/server/backup/2021-12-24_22-32-26 /server/backup/
还原
[root@localhost ~]# systemctl stop mysqld.service
[root@localhost ~]# rm -rf /var/lib/mysql/
[root@localhost ~]# innobackupex --copy-back /server/backup/2021-12-24_22-32-26/
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql/data/
[root@localhost ~]# systemctl start mysqld
14.使用mydumper备份数据库
安装mydumper
[root@localhost backup]# yum -y install glib2-devel mysql-devel zlib-devel pcre-devel
解压
[root@localhost ~]# ll
-rw-r--r--. 1 root root 2561677 12月 24 22:10 mydumper-0.10.5-1.el7.x86_64.rpm
[root@localhost ~]# yum install mydumper-0.10.5-1.el7.x86_64.rpm
备份
[root@localhost ~]# mydumper -u root -p MySQL@123 -B school -S /tmp/mysql.sock -o /mysqlbak
15.使用mydumper恢复数据库
[root@localhost ~]# myloader -u root -p MySQL@123 -S /tmp/mysql.sock -d /mysqlbak/ -o -B school