数据库备份,数据库为school,素材如下
1.创建student和score表
目录
4.备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库
6.备份MySQL数据库某个(些)表。此例备份student表
[14.使用xtrabackup 备份数据库](#14.使用xtrabackup 备份数据库)
[15.在另外的数据库服务器上还原xtrabackup 备份](#15.在另外的数据库服务器上还原xtrabackup 备份)
-------------------------创建student表。SQL代码如下--------------------------------
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表增加记录
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目录
[root@master backup]# mysqldump -uroot -p030118 -B school > /backup/school_1.sql
4.备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库
[root@master backup]# mysqldump -uroot -p030118 --add-drop-database school > /backup/school_4.sql
5.直接将MySQL数据库压缩备份
systemctl stop mysqld
[root@master data]# tar czf /backup/database.tar.gz *
6.备份MySQL数据库某个(些)表。此例备份student表
[root@master backup]# mysqldump -uroot -p030118 school student > /backup/student_1.sql
7.同时备份多个MySQL数据库(其他数据库素材自行准备)
[root@master backup]# mysqldump -uroot -p030118 -B school friend > /backup/db_2.sql
8.仅仅备份数据库结构
[root@master backup]# mysqldump -uroot -p030118 -d school > /backup/school_2.sql
9.备份服务器上所有数据库
[root@master backup]# mysqldump -uroot -p030118 --all-databases > /backup/db_1.sql
10.还原MySQL数据库
mysql8.0 [(none)]>drop database school;
mysql8.0 [(none)]>create database school;
Query OK, 1 row affected (0.01 sec)
mysql8.0 [(none)]>use school
Database changed
mysql8.0 [school]>source /backup/school_1.sql
----------------------------------方法二-----------------------------------------
[root@master backup]# mysqldump -uroot -p030118 school < /backup/school_1.sql
11.还原压缩的MySQL数据库
systemctl stop mysqld
[root@master data]# tar czf /backup/database.tar.gz *
[root@master data]# scp /backup/database.tar.gz 192.168.140.134:/backup/
The authenticity of host '192.168.140.134 (192.168.140.134)' can't be established.
ECDSA key fingerprint is SHA256:whHWCTyntsyeb2srFJBPjiB8EBZiOFSOvD0A+BA+pJU.
ECDSA key fingerprint is MD5:dd:a9:66:9a:94:86:08:40:e9:bb:6f:c1:a5:ea:f5:a6.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.140.134' (ECDSA) to the list of known hosts.
root@192.168.140.134's password:
database.tar.gz 100% 1383KB 51.4MB/s 00:00
------------------------------下面是目标服务器--------------------------------------
[root@master backup]# systemctl stop mysqld
[root@master backup]# rm -rf /usr/local/mysql/data/*
[root@master backup]# tar xf /backup/database.tar.gz -C /usr/local/mysql/data/
[root@master backup]# chown -R mysql.mysql /usr/local/mysql/data/*
[root@master backup]# systemctl start mysqld
12.使用mydumper备份数据库
[root@master ~]# mydumper -u root -p 030118 -B school -S /tmp/mysql.sock -o /backup/school_5
13.使用mydumper恢复数据库
[root@master backup]# myloader -u root -p 030118 -S /tmp/mysql.sock -d /backup/school_5 -B school
14.使用xtrabackup 备份数据库
[root@master backup]# innobackupex --user=root --password=030118 --socket=/tmp/mysql.sock /server/backup/
15.在另外的数据库服务器上还原xtrabackup 备份
[root@master backup]# tar czf /server/backup/full_2023-03-20.tar.gz full_2023-03-20/
[root@master backup]# scp /server/backup/full_2023-03-20.tar.gz 192.168.140.134:/server/backup/
root@192.168.140.134's password:
full_2023-03-20.tar.gz 100% 600KB 83.9MB/s 00:00
[root@master backup]# tar xf full_2023-03-20.tar.gz
[root@master backup]# innobackupex --copy-back /server/backup/full_2023-03-20/