mysql数据库的备份和恢复

时间:2022-09-04 11:24:17

Mysql数据库的备份和恢复

1.备份单个数据库  

  mysql数据库自带了一个很好用的备份命令,就是mysqldump,它的基本使用如下:

    语法:mysqldump –u <用户名> -p <数据库名>备份的文件名

      单个数据库备份例子:      

MariaDB [oldboy]> select database();
+------------+
| database() |
+------------+
| oldboy |
+------------+
1 row in set (0.00 sec)

MariaDB [oldboy]> show tables;
+------------------+
| Tables_in_oldboy |
+------------------+
| test |
+------------------+
1 row in set (0.00 sec)

备份单个库:oldboy库

mysqldump -uroot -p123456 -B oldboy > oldboy0531_B.sql

  注意:加上-B后,备份的时候,会有创建数据库并use库的过程。

删除数据库中备份过的库oldboy,然后将备份的数据重新导入数据库:

  删除前的oldboy库

  

MariaDB [(none)]> use oldboy;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [oldboy]> show tables;
+------------------+
| Tables_in_oldboy |
+------------------+
| test |
+------------------+
1 row in set (0.00 sec)

MariaDB [oldboy]> select * from test;
+----+-----+-------------+
| id | age | name |
+----+-----+-------------+
| 1 | 16 | li xiao hai |
| 2 | 20 | li lei |
| 3 | 22 | da yu er |
| 4 | 26 | li p |
+----+-----+-------------+

开始删除oldboy库

  

MariaDB [oldboy]> drop database oldboy;
Query OK, 1 row affected (0.09 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| test2_utf8 |
+--------------------+
5 rows in set (0.00 sec)

上述可知:oldboy库删除成功!接下来开始恢复删除前的备份:

  开始恢复oldboy库

  [root@localhost tmp]# mysql -uroot -p123456 </tmp/oldboy0531_B.sql

开始查看是否恢复oldboy库:

  

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| oldboy |
| performance_schema |
| test |
| test2_utf8 |
+--------------------+
6 rows in set (0.00 sec)

MariaDB [(none)]> use oldboy ;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [oldboy]> select * from test;
+----+-----+-------------+
| id | age | name |
+----+-----+-------------+
| 1 | 16 | li xiao hai |
| 2 | 20 | li lei |
| 3 | 22 | da yu er |
| 4 | 26 | li p |
+----+-----+-------------+
4 rows in set (0.00 sec)

上述可知:恢复oldboy库成功!

 

 1.1利用mysqldump命令对指定的库进行压缩备份

       [root@localhost tmp]# mysqldump -uroot -p123456 -B oldboy|gzip > oldboy0531_B_B.sql.gz       

       [root@localhost tmp]# ls -l oldboy0531_B_B.sql.gz
        -rw-r--r-- 1 root root 807 May 16 23:25 oldboy0531_B_B.sql.gz

  1.2利用mysqldump命令备份多个库(-B参数后可以指定多个库)

        具体事例参看1.1

 

 

2.分库备份

  2.1利用命令对单个库的多个表备份:实例  

语法:mysqldump –u用户名–p 数据库名表名1 表名2 >备份的文件名

提示:不能加-B参数了,因为库oldboy后面就是oldboy表了,指定-B就表示后面的都是库

  

mysql> show tables;
+-----------------+
| Tables_in_test2 |
+-----------------+
| class1 |
| class2 |
| school |
+-----------------+
3 rows in set (0.00 sec)

mysqldump -uroot -p  test2 class1 class2 >/tmp/ class1_and_class2.sql

[root@localhost tmp]# ls
class1_and_class2.sql ks-script-oDCh_J mysql.sock mysql.sock.lock test1_and_test2.sql

  2.2备份单个表

    语法:mysqldump –u 用户名–p 数据库名表名>备份的文件名

提示:不能加-B参数了,因为库oldboy后面就是oldboy表了,指定-B就表示后面的都是库

    实例:

      mysqldump -uroot -p  test2 class1  > class1.sql      

                      [root@localhost tmp]# ls
                       class1_and_class2.sql class1.sql

 

 

最后说明下一个问题:

关于mysqldump的参数说明:  

1、-B备份多个库(添加create和use库的语句)

2、-d只备份库表结构

3、-t只备份数据(sql语句形式)

4、-T分离库表和数据不同的文件,数据是文本,非SQL语句

5、-A备份数据库中所有的数据

6、--compact去掉注释,适合调试输出,生产不使用

7、-F刷新binglog日志,生成新文件,将来增量恢复从这个文件开始

8、--master-data增加binlog日志文件名及对应的位置点(即CHANGE MASTER语句)

--master-data=1不注释,--master-data=2注释

9、-x –lock-all-tables  锁表,当某一时刻备份数据时需要加入此参数,以确定备份数据时是从某一时刻开始的

10、-l –lock-tables  对读锁表

11、--single-transaction适合innodb事务数据库备份

innodb表在备份时,通常启动选项—single-transaction来保证备份的一致性,实际上它的工作原理是设定本次会话的隔离级别为REPEATABLE READ,以确保本次会话(dump)时,不会看到其他会话已经提交了的数据

12、-q 不做缓冲查询,直接导入输出