1.创建数据库为school,创建表student和score表:
mysql> 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)
-> );
mysql> 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,'计算机系', '湖南省衡阳市');
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);
1.物理备份数据库school到/backup目录
[root@localhost ~]# cp -r /usr/local/mysql/data/school/ /backup/
[root@localhost ~]# cd /backup
[root@localhost backup]# ll
total 0
drwxr-x--- 2 root root 92 Aug 10 21:23 school
2.备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库
systemctl start mysqld
mysqldump -uroot -proot -B school --set-gtid-purged=OFF > /bcakup/school_`date +%F`.sql
3.直接将MySQL数据库压缩备份
mysqldump -uroot -proot -B -A --set-gtid-purged=OFF | gzip > /backup/all_`date +%F`.sql.gz
4.备份MySQL数据库某个(些)表。此例备份student表
mysqldump -uroot -proot school student --set-gtid-purged=OFF > /backup/student_`date +%F`.sql
5.同时备份多个MySQL数据库(其他数据库素材自行准备)
mysqldump -uroot -proot -B school test1 --set-gtid-purgedd=OFF > /backup/school_test1_`date +%F`.sql
6.仅备份数据库结构
mysqldump -uroot -proot -B school -d --set-gtid-purged=OFF > /backup/school_d_`date +%F`.sql
7.备份服务器上所有数据库
mysqldump -uroot -proot -B- A --set-gtid-purged=OFF > /backup/all_`date +%F`.sql
8.还原MySQL数据库
drop database school
mysql -uroot -proot < all_2023-08-11.sql
9.还原压缩的MySQL数据库
zcat /backup/all_2023-08-11.tar.gz | mysql -uroot -proot
10.使用xtrabackup 备份数据库
#完全备份
innobackupex --user=root --password=123456 --no-timestamp --socket=/tmp/mysql.sock /backup/full_`date +%F`
#增量备份1
innobackupex --incremental --user=root --password=123456 --no-timestamp --socket=/tmp/mysql.sock --incremental-basedir=/backup/full_2023-03-31/ /backup/incr_1
#增量备份2
innobackupex --incremental --user=root --password=123456 --no-timestamp --socket=/tmp/mysql.sock --incremental-basedir=/backup/incr_1/ /backup/incr_2
11.在另外的数据库服务器上还原xtrabackup 备份
node1:
tar cavf ./xtr.tar.gz full_2023-08-11/ /incr_1/ incr_2
scp xtr.tar.gz root@192.168.11.17:~
node2:
tar xf xtr.tar.gz
#恢复准备
innobackupex --apply-log --redo-only full_2023-08-11/
innobackupex --apply-log --redo-only full_2023_08-11/ --incremental-dir=incr_1
innobackupex --apply-log --redo-only full_2023-08-11/ --incremental-dir=incr_2
#开始恢复
systemctl stop mysqld
rm -rf /usr/local/mysql/data/*
innobackupex --copy-back full_2023-08-11/
chown -R mysql.mysql /usr/local/mysql/data/*
systemctl start mysqld
12.使用mydumper备份数据库
mydumper -u root -p root -B school -S /tmp/mysql.sock -o /backup/myall
13.使用mydumper恢复数据库
myloader -u root -p root -S /tmp/mysql.sock -d /backup/myall/ -B school