下面介绍几个比较重要的选项:
i2ksvr6/home/mysql> mysqldump -uroot -pzhangyun --add-drop-database--tab="/home/mysql/test" xmppdb
i2ksvr6/home/mysql> cd test/
i2ksvr6mysql/test> ls
short_news.sql tig_deleted_index.txt tig_groups.sql tig_pairs_key.txt tig_users.sql
short_news.txt tig_gpfeature.sql tig_groups.txt tig_pairs.sql tig_users.txt
tig_blacklist.sql tig_gpfeature.txt tig_nodes.sql tig_pairs.txt xmpp_stanza.sql
tig_blacklist.txt tig_gpmember.sql tig_nodes.txt tig_sysmsg.sql xmpp_stanza.txt
tig_dbofuser.sql tig_gpmember.txt tig_offlinemsg.sql tig_sysmsg.txt
tig_dbofuser.txt tig_gpprivilege.sql tig_offlinemsg.txt tig_user_cfg.sql
tig_deleted_index.sql tig_gpprivilege.txt tig_pairs_key.sql tig_user_cfg.txt
i2ksvr6 mysql/test>
大多数DBA喜欢用select …into outfile的方式来导出一张表,但是mysqldump一样可以完成工作,而且可以一次完成多张表的导出,并且保证导出数据的一致性。
--where=’where_condition’(-w ‘where_condition’):导出给定条件的数据。
比如下面导出数据库xmppdb中表tig_users中rcs_uid为1000000002的数据。
mysqldump -uroot -pzhangyun --single-transaction--where='rcs_uid=1000000002' xmppdb tig_users > tig_users_bak.sql
i2ksvr6 mysql/test> cat tig_users_bak.sql
……………..
--
-- Dumping datafor table `tig_users`
--
-- WHERE: rcs_uid=1000000002
LOCK TABLES`tig_users` WRITE;
/*!40000 ALTERTABLE `tig_users` DISABLE KEYS */;
INSERT INTO`tig_users` VALUES ('1000000002','[email protected]',NULL,'2011-09-1505:59:13','0000-00-00 00:00:00','0000-00-0000:00:00',0,0,1,'2','+2','0000-00-00 00:00:00','false','false');
/*!40000 ALTERTABLE `tig_users` ENABLE KEYS */;
UNLOCK TABLES;
………………………..
select… into 语句也是一种逻辑备份的方法,或者更准确地说是导出一张表中的数据。
mysql> select * into outfile'/home/mysql/hello.sql' from tig_users;
Query OK, 3 rows affected (0.00 sec)
默认导出的文件是以tab键分隔的:
i2ksvr6 /home/mysql> cat hello.sql
1000000002 [email protected] \N 2011-09-1513:59:13 0000-00-00 00:00:00 0000-00-00 00:00:00 0 0 1 2+2 0000-00-00 00:00:00 false false
1000000001 db-properties \N 2011-09-1513:59:12 0000-00-00 00:00:00 0000-00-00 00:00:00 0 0 -1 1+1 0000-00-00 00:00:00 false false
1000000003 vhost-manager \N 2011-09-2116:06:54 0000-00-00 00:00:00 0000-00-00 00:00:00 0 0 -1 3+3 0000-00-00 00:00:00 false false
如果想用其他的分隔符,使用fieldsterminated by ‘string’选项。
mysql> select * into outfile'/home/mysql/helloworld.sql' fields terminated by ',' from tig_users;
Query OK, 3 rows affected (0.00 sec)
或者执行:
i2ksvr6 /home/mysql> mysql -u root -pzhangyunxmppdb -e "select * into outfile'/home/mysql/helloworld.sql' fields terminated by ',' from tig_users;"
若文件存在,则会报错。
mysql> select * into outfile'/home/mysql/hello.sql' from tig_users;
ERROR 1086 (HY000): File'/home/mysql/hello.sql' already exists
导出的文件的路径的权限必须是mysql:mysql,否则MySQL会报告没有权限。
mysql> select * into outfile'/root/hello.sql' from tig_users;
ERROR 1 (HY000): Can't create/write to file'/root/hello.sql' (Errcode: 13)
i2ksvr6 /home/mysql> mysqldump -u root-pzhangyun --database mysql > mysql.dump
i2ksvr6 /home/mysql> mysql -u root -pzhangyun< mysql.dump
或者
mysql> source /home/mysql/mysql.dump;
通过mysqldump可以恢复数据库,但是常发生的一个问题是mysqldump可以导出存储过程,触发器,事件,数据,但是却不能导出视图。需要用mysqldump后再导出视图的定义,或者保存视图定义的frm文件,并在恢复时进行导入,确保完全恢复。
如是通过mysqldump –tab或select into outfile 导出的数据需要恢复时,这时需要通过 load data infile命令来进行导入。
i2ksvr6 /home/mysql> cat helloworld.txt
1000000002,[email protected],\N,2011-09-1513:59:13,0000-00-00 00:00:00,0000-00-00 00:00:00,0,0,1,2,+2,0000-00-0000:00:00,false,false
1000000001,db-properties,\N,2011-09-1513:59:12,0000-00-00 00:00:00,0000-00-00 00:00:00,0,0,-1,1,+1,0000-00-0000:00:00,false,false
1000000003,vhost-manager,\N,2011-09-2116:06:54,0000-00-00 00:00:00,0000-00-00 00:00:00,0,0,-1,3,+3,0000-00-0000:00:00,false,false
mysql> load data infile'/home/mysql/helloworld.txt' into table tig_users;
为了加快InnoDB存储引擎的导入,你可能希望导入过程忽略对外键的检查,因此可以使用如下的方式:
mysql> set @@foreign_key_checks=0;
mysql> load data infile '/home/mysql/helloworld.txt' intotable tig_users;
mysql> set @@foreign_key_checks=1;
另外也可以针对指定的列进行导入,如将数据导入列a、b,而c列等于a、b列之和:
i2ksvr6 /home/mysql> cat helloworld.txt
1 2 3
4 5 6
mysql> create table b (
->a int ,
->b int,
->c int,
->primary key(a))
->engine=innodb;
Query OK, 0 rows affected (0.09 sec)
mysql>
mysql> load data infile'/home/mysql/helloworld.txt' into table b fields terminated by ' ' (a,b) set c= a + b;
Query OK, 2 rows affected, 2 warnings (0.00sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 2
mysql> select * from b;
+---+------+------+
| a | b | c |
+---+------+------+
| 1 | 2 | 3 |
| 4 | 5 | 9 |
+---+------+------+
2 rows in set (0.00 sec)
从本质上来说,是loaddata infile的命令接口,而且大多数的选项都和load data infile语法相同。
可以导入几张表
bmsuite /home/mysql> mysqlimport -uroot-pzhangyun --use-thread=2 xmppdb /home/mysql/tig_users.txt/home/mysql/tig_users.txt
查看上面命令运行过程中查看MySQL的数据库线程列表,会看到mysqlimport实际上是同时执行了2条load data infile语句来完成并发导入操作。
mysql> mysql> show full processlist;
37、二进制日志备份与恢复
二进制日志非常重要,可以完成point-in-time的恢复工作。
默认情况下并不启动二进制日志,要使用二进制日志,首先必须启动它:
[mysqld]
port=5306
socket = /tmp/mysql.sock
log-error = ../log/error/mysqlerr
log-bin=../binlog/mysqlbinlog
log-bin-index = ../binlog/mysqlbinlog.index
注:InnoDB存储引擎简单地启动二进制日志是不够的,还需要启动一些其他的参数来保证安全和正确记录二进制日志,如下:
sync_binlog = 1
innodb_support_xa = 1
备份二进制日志前,可以通过Flushlogs命令来生成一个新的二进制日志文件,然后备份之前的二进制日志。
恢复二进制日志:
bmsuite /home/mysql> mysqlbinlogbinlog/mysqlbinlog.000003 | mysql -u root –pzhangyun
恢复多个二进制日志文件,最正确的做法是同时恢复多个二进制日志文件,而不是一个一个恢复:
bmsuite /home/mysql> mysqlbinlogbinlog/mysqlbinlog.[0-10]* | mysql -u root –pzhangyun
也可以先通过mysqlbinlog命令导出到一个文件,然后再通过source命令导入。这样做的好处是,可以对导出的文件进行修改后再导入,
bmsuite mysql/binlog> mysqlbinlogmysqlbinlog.000001 > /tmp/statements.sql
bmsuite mysql/binlog> mysqlbinlogmysqlbinlog.000002 >> /tmp/statements.sql
bmsuite mysql/binlog> mysql -u root -pzhangyun-e "source /tmp/statements.sql"
38、热备
(1)ibbackup