F.1 mysqldump的工作原理 利用mysqldump命令备份数据的过程,实际上就是把数据从mysql库里以逻辑的sql语句的形式直接输出或者生成备份文件的过程。 F.2 备份F.2.1备份单个数据库,联系多种参数使用mysql数据库自带的备份命令mysqldump 语法:mysqldump -h数据库地址 -u用户名 -p数据库 >备份的目录和文件 范例1:备份名字为test123的库 a、查看备份前的数据 [iyunv@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参数) [iyunv@mysql01 ~]# mysqldump-h127.0.0.1 -uroot -p test123 >/opt/test123_bak.sql Enter password:
c、检查备份的结果 egrep -v “#|\*|--|^$”/opt/mysql_bak.sql 注意:其中在恢复的时候,首先做的操作是删除表,之后创建表的过程
d、使用-B进行备份 (1、在备份文件中会生成使用备份的数据库(test123),不然,在恢复的时候需要指定恢复的数据库;2、在备份文件中会有create database db信息;) [iyunv@mysql01 ~]# mysqldump-h127.0.0.1 -uroot -poracle -B test123 >/opt/test123_B_bak.sql
f、检查备份的结果 egrep -v “#|\*|--|^$”/opt/test123_B_bak.sql 注意:添加的-B参数就相当于在恢复的时候指定了数据库。 g、恢复的时候不需要指定数据库和字符集 mysql> drop table test; Query OK, 0 rows affected (0.64 sec)
[iyunv@mysql01 ~]# mysql -h127.0.0.1-uroot -p </opt/test123_B_bak.sql Enter password:
[iyunv@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进行压缩 (使用压缩可以减少使用的空间) [iyunv@mysql01 ~]# mysqldump-h127.0.0.1 -uroot -poracle -B test123 |gzip >/opt/test123_B.gz
[iyunv@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备份多个数据库[iyunv@mysql01 ~]# mysqldump-h127.0.0.1 -uroot -poracle -B test123 testtest |gzip >/opt/test123_testtest_bak.gz
[iyunv@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
|