mysql数据库备份

数据库备份,数据库为school

mysql> create database if not exists school;

1.创建student和score表

创建student:

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表:

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 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.备份数据库school到/backup目录

[root@localhost mysql]# cp -pr /var/lib/mysql/school /backup

4.备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库

mysqldump --add-drop-table -uroot -p110119 --databases school > /backup/school.sql

5.直接将MySQL数据库压缩备份

#tar备份方式
tar czf /backup/school.tar.gz *

6.备份MySQL数据库某个(些)表。此例备份student表

#备份前关闭mysqld程序
[root@localhost backup]# systemctl stop mysqld
#检查mysqld进程状态                                  
[root@localhost backup]# netstat -lnupt | grep 3306
#对表的备份
[root@localhost backup]# mysql  -uroot -p school student > /backup/student.$(date +%F).sql
#进入目录查看备份情况
[root@localhost backup]# ll
total 28
-rw-r--r-- 1 root root    45 Aug 5 11:23 school.tar.gz
-rw-r--r-- 1 root root 12281 Aug 5 16:59 student
-rw-r--r-- 1 root root 12281 Aug 5 17:02 student.2023-07-22.sql

7.同时备份多个MySQL数据库

mysql> create database if not exists pop;
mysql> create database if not exists mkl;

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mkl                |
| mysql              |
| performance_schema |
| pop                |
| school             |
| sys                |
+--------------------+
7 rows in set (0.00 sec)
[root@localhost backup]# systemctl stop mysqld
[root@localhost backup]# systemctl status mysqld
[root@localhost backup]# mysqldump -uroot -p'Redhat@123' --default-character-set=utf8 school pop mkl > backup/db.$(date +%F).sql

8.仅备份数据库结构

[root@localhost ~]# mysqldump -uroot -p'Redhat@123' -d school > /backup/t1.sql
#mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# cd /backup

9.备份服务器上所有数据库

[root@localhost ~]# mysql -uroot -p'Redhat@123' -B databases information_schema mkl mysql performance_schema pop school sys > test/alldb.sql

10.还原MySQL数据库

[root@localhost ~]# myloader -u root -p Redhat@123 -d test/db/ -o -B school
[root@localhost ~]# mysql -uroot -p'Redhat@123' -e 'show databases'

11.还原压缩的MySQL数据库

[root@localhost mysql]# tar tf all_db.tar.gz

12.使用xtrabackup 备份数据库

innobackupex --user=root --password=Redhat@123 --defaults-file=/etc/my.cnf --
socket=/tmp/mysql.sock /server/backup/

13.在另外的数据库服务器上还原xtrabackup 备份

 innobackupex --copy-back /server/backup/

14.使用mydumper备份数据库

[root@localhost ~]# mydumper -u root -p Redhat@123 -B school -o test/school

15.使用myloader恢复数据库

[root@localhost test]# mysql -uroot -p'Redhat@123' -e 'drop database school'
[root@localhost test]# mysql -uroot -p'Redhat@123' -e 'show databases'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| -S                 |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

[root@localhost ~]# myloader -u root -p Redhat@123 -d test/school -o -B school
[root@localhost ~]# mysql -uroot -p'Redhat@123' -e 'show databases'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| -S                 |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+

你可能感兴趣的:(mysql,数据库,mysql)