mysql f_MySQL系列之F------MySQL备份

F.1mysqldump的工作原理

利用mysqldump命令备份数据的过程,实际上就是把数据从mysql库里以逻辑的sql语句的形式直接输出或者生成备份文件的过程。

F.2备份

F.2.1备份单个数据库,联系多种参数使用

mysql数据库自带的备份命令mysqldump

语法:mysqldump -h数据库地址 -u用户名 -p数据库 >备份的目录和文件

范例1:备份名字为test123的库

a、查看备份前的数据

[root@mysql01 ~]# mysql -h 127.0.0.1-u root -poracle -e "show databases;use test123;show tables;select * fromtest;"

Warning: Using a password on thecommand line interface can be insecure.

+--------------------+

| Database           |

+--------------------+

| information_schema |

| 3306               |

| mysql              |

| performance_schema |

| test               |

| test123            |

| testtest           |

+--------------------+

+-------------------+

| Tables_in_test123 |

+-------------------+

| student           |

| test              |

| test01            |

| test02            |

+-------------------+

+----+--------+------+----------+------+

| id | name   | age | password | sex  |

+----+--------+------+----------+------+

| 1 | test01 | NULL | test01   |NULL |

| 2 | test02 | NULL | test02   |NULL |

| 3 | test03 | NULL | test03   |NULL |

| 4 | test04 | NULL | test04   |NULL |

+----+--------+------+----------+------+

b、执行备份的命令(推荐使用-B参数)

[root@mysql01 ~]# mysqldump-h127.0.0.1 -uroot -p test123 >/opt/test123_bak.sql

Enter password:

c、检查备份的结果

egrep -v “#|\*|--|^$”/opt/mysql_bak.sql

spacer.gif

注意:其中在恢复的时候,首先做的操作是删除表,之后创建表的过程

d、使用-B进行备份

(1、在备份文件中会生成使用备份的数据库(test123),不然,在恢复的时候需要指定恢复的数据库;2、在备份文件中会有create database db信息;)

[root@mysql01 ~]# mysqldump-h127.0.0.1 -uroot -poracle -B test123 >/opt/test123_B_bak.sql

f、检查备份的结果

egrep -v “#|\*|--|^$”/opt/test123_B_bak.sql

spacer.gif

注意:添加的-B参数就相当于在恢复的时候指定了数据库。

g、恢复的时候不需要指定数据库和字符集

mysql> drop table test;

Query OK, 0 rows affected (0.64 sec)

[root@mysql01 ~]# mysql -h127.0.0.1-uroot -p

Enter password:

[root@mysql01 ~]# mysql -h127.0.0.1-uroot -poracle -e "use test123;select * from test;"

Warning: Using a password on the commandline interface can be insecure.

+----+--------+------+----------+------+

| id | name   | age | password | sex  |

+----+--------+------+----------+------+

| 1 | test01 | NULL | test01   |NULL |

| 2 | test02 | NULL | test02   |NULL |

| 3 | test03 | NULL | test03   |NULL |

| 4 | test04 | NULL | test04   |NULL |

+----+--------+------+----------+------+

h、备份的时候使用gzip进行压缩

(使用压缩可以减少使用的空间)

[root@mysql01 ~]# mysqldump-h127.0.0.1 -uroot -poracle -B test123 |gzip >/opt/test123_B.gz

[root@mysql01 ~]# ll /opt

total 24

-rw-r--r--. 1 root mysql 4435 May23 23:52 test123_bak.sql

-rw-r--r--. 1 root mysql 4583 May24 00:17 test123_B_bak.sql

-rw-r--r--. 1 root mysql 1057 May24 00:46 test123_B.gz

由上列信息可以看到,其中没有通过-B参数的比通过-B参数的备份要小,通过-B参数和gzip压缩的是最小的,比例大概为4:1(不能当做通用值使用)

总结:在通过mysqldump备份的时候参数要使用-B(省略需要指定数据库use database和create database db信息),使用gzip(减少备份所占用的空间)

例如:

mysqldump -h127.0.0.1 -uroot -poracle -B需要备份的数据库 |gzip >需要备份到的目录及备份文件的名字(注意:在不使用gzip时,后缀是sql;使用gzip后缀名是gz)

A.2.2备份多个数据库

[root@mysql01 ~]# mysqldump-h127.0.0.1 -uroot -poracle -B test123 testtest |gzip >/opt/test123_testtest_bak.gz

[root@mysql01 ~]# ll /opt

total 32

-rw-r-----. 1 root mysql  345 May 23 06:09 mysqlbin_test.000001.bak

-rw-r--r--. 1 root mysql 4435 May 2323:52 test123_bak.sql

-rw-r--r--. 1 root mysql 4583 May 2400:17 test123_B_bak.sql

-rw-r--r--. 1 root mysql 1057 May 2400:46 test123_B.gzip

-rw-r--r--. 1 root mysql 1131 May 2401:52 test123_testtest_bak.gz

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值