数据库备份,数据库为school,素材如下
1.创建student和score表
CREATE TABLE student (
id INT(10) NOT NULL UNIQUE name primary key 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目录
使用Gtid:
查看binlog文件名以及最后一条sql语句产生的uuid
mysql> show master status;
查看创建school数据库时产生的uuid;
mysql> show binlog events in 'binlog.000035'
备份
[root@server mysql]# pwd
/var/lib/mysql
mysqlbinlog --skip-gtids --include-gtids=' a58df9d1-11ff-11ed-803f-000c296ee409:12-32' binlog.000035 -r /tmp/school.sql
使用mysqldump
[
root@server ~]# mysqldump -uroot -phjj --default-character-set=utf8 -B school >/tmp/school.sql
5.直接将MySQL数据库压缩备份
[root@server ~]# mysqldump -uroot -phjj -B mysql |gzip >/tmp/mysql.sql.gz
6.备份MySQL数据库某个(些)表。此例备份student表
[root@server ~]# mysqldump -uroot -phjj school student >/tmp/school.student.sql
7.同时备份多个MySQL数据库(其他数据库素材自行准备)
[root@server ~]# mysqldump -uroot -phjj -B mysql school >/tmp/mysql.school.sql
8.仅仅备份数据库结构
[root@server ~]# mysqldump -uroot -phjj -B -d school >/tmp/schoold.sql
9.备份服务器上所有数据库
[root@server ~]# mysqldump -uroot -phjj -A >/tmp/all.sql