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)
);
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 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.备份数据库student到/backup目录中,使用mysqldump备份工具;
5.备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库
mysqldump -uroot -p --add-drop-table student > /backup/student_drop_table.sql
5.直接将mysql数据库压缩备份
[root@localhost ~]# mysqldump -uroot -p student | gzip > /backup/student-gzip.sql.gz
6.备份mysql某些表。
[root@localhost ~]# mysqldump -uroot -p student score > /backup/score-`date +%F`.sql
7.备份多个数据库
[root@localhost ~]# mysqldump -uroot -p -B city student > /backup/city-student.sql
8.仅仅备份数据库结构
mysqldump -uroot -p -d city > /backup/city-jiegou.sql
9.备份服务器上所有数据库
[root@localhost ~]# mysqldump -uroot -p -A > /backup/all-database.sql
10.还原mysql数据库、
删除student数据库
mysql> drop database student;
还原student数据库
mysql> source /backup/school-2023-03-31.sql
或者
[root@localhost ~]# mysql -uroot -p < /backup/school-2023-03-31.sql
11.使用xtrabackup备份数据库
innobackupex --user=root --password=123456 --defaults-file=/etc/my.cnf --
socket=/tmp/mysql.sock /server/backup/
12.使用mydumper备份数据库
mydumper -h localhost -u root -p 888 -t 6 -S /tmp/mysql.sock -B school -o /mysqlbackup/
13.使用mydumper恢复数据库
myloader -h localhost -u root -p 888 -S /tmp/mysql.sock -d /mysqlbackup/ -o -B school