mysql备份和恢复
Posted aaallenn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql备份和恢复相关的知识,希望对你有一定的参考价值。
1、mysqldump的基本语法:
mysqldump -uroot -pyuanqk -A -B --master-data=2 --single-transaction|gzip > /tmp/all.sql.gz
2、测试一下分库备份:
[[email protected] backup]# mysql -uroot -pyuanqk -S /data/3306/mysql.sock -e "show databases;"|grep -Evi "Database|infor|perfor"|sed -r ‘s#^([a-z].*$)#mysqldump -uroot -pyuanqk -S /data/3306/mysql.sock --events -B \1|gzip > /tmp/\1.backup.sql.gz #g‘ |bash [[email protected] backup]# cd /tmp [[email protected] tmp]# ls -lrt total 260 drwxrwxrwt. 2 root root 4096 Jan 17 15:10 keyring-NVQMdL drwxrwxrwt. 2 root root 4096 Jan 17 15:10 pulse-dJ06wMSd3KZp drwx------. 2 root root 4096 May 10 12:23 virtual-root.LWTIk3 -rw-r-----. 1 oracle dba 0 May 10 14:33 pki_data829609165.lck.tmp drwxr-----. 2 oracle dba 4096 May 17 21:08 oracle-dfw-5653875815388066450.tmp -rw-r-----. 1 oracle dba 0 May 17 21:08 oracle-dfw-5653875815388066450.tmp.lck drwx------. 2 root root 4096 May 17 21:08 virtual-root.dvCZEm drwx------. 2 root root 4096 May 18 10:52 virtual-root.PiAh8b drwxr-----. 2 oracle dba 4096 May 18 10:52 oracle-dfw-2618725649369468169.tmp -rw-r-----. 1 oracle dba 0 May 18 10:52 oracle-dfw-2618725649369468169.tmp.lck drwxr-----. 2 oracle dba 4096 May 19 10:35 oracle-dfw-3445292226768824394.tmp -rw-r-----. 1 oracle dba 0 May 19 10:35 oracle-dfw-3445292226768824394.tmp.lck drwx------. 2 root root 4096 May 19 10:35 virtual-root.cQHdmF -rw-r--r--. 1 root root 229 May 19 15:09 grant.txt -rw-r--r--. 1 root root 12636 May 19 20:45 backup.sql -rwxr-xr-x. 1 root root 14 May 19 21:46 test.sh -rw-r--r--. 1 root root 40 May 19 22:27 test.sql drwxr-----. 2 oracle dba 4096 May 20 09:38 oracle-dfw-5924615879017712431.tmp -rw-r-----. 1 oracle dba 0 May 20 09:38 oracle-dfw-5924615879017712431.tmp.lck drwxr-----. 2 oracle dba 4096 May 20 09:39 hsperfdata_oracle drwx------. 2 gdm gdm 4096 May 20 09:41 orbit-gdm drwx------. 2 root root 4096 May 20 09:41 keyring-cOaui4 drwx------. 2 root root 4096 May 20 09:41 pulse-tGp4UvZcaWXd drwx------. 2 root root 4096 May 20 09:41 virtual-root.ONihhN drwx------. 2 gdm gdm 4096 May 20 09:41 pulse-HkcvmYvLKxjp drwx------. 2 root root 4096 May 20 11:47 orbit-root -rw-r--r--. 1 root root 155226 May 20 16:59 mysql.backup.sql.gz -rw-r--r--. 1 root root 1032 May 20 16:59 yuanqk.backup.sql.gz -rw-r--r--. 1 root root 549 May 20 16:59 yuanqk_gbk.backup.sql.gz -rw-r--r--. 1 root root 548 May 20 16:59 yuanqk_utf8.backup.sql.gz [[email protected] tmp]#
3、测试一下导出某个库的某张表
[[email protected] backup]# mysqldump -uroot -pyuanqk -S /data/3306/mysql.sock --compact mysql user <=== "--compact"主要是去掉注释,方便调试查看,该命令导出mysql库的user表,如果要导出多张表,再后面以空格分开,如mysql user1 user2 user3 /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `user` ( `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT ‘‘, `User` char(16) COLLATE utf8_bin NOT NULL DEFAULT ‘‘, `Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT ‘‘, `Select_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Insert_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Update_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Delete_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Create_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Drop_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Reload_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Shutdown_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Process_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `File_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Grant_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `References_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Index_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Alter_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Show_db_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Super_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Create_tmp_table_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Lock_tables_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Execute_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Repl_slave_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Repl_client_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Create_view_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Show_view_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Create_routine_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Alter_routine_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Create_user_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Event_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Trigger_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Create_tablespace_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `ssl_type` enum(‘‘,‘ANY‘,‘X509‘,‘SPECIFIED‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘‘, `ssl_cipher` blob NOT NULL, `x509_issuer` blob NOT NULL, `x509_subject` blob NOT NULL, `max_questions` int(11) unsigned NOT NULL DEFAULT ‘0‘, `max_updates` int(11) unsigned NOT NULL DEFAULT ‘0‘, `max_connections` int(11) unsigned NOT NULL DEFAULT ‘0‘, `max_user_connections` int(11) unsigned NOT NULL DEFAULT ‘0‘, `plugin` char(64) COLLATE utf8_bin DEFAULT ‘‘, `authentication_string` text COLLATE utf8_bin, PRIMARY KEY (`Host`,`User`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT=‘Users and global privileges‘; /*!40101 SET character_set_client = @saved_cs_client */; INSERT INTO `user` VALUES (‘localhost‘,‘root‘,‘*427D48AF41439640C2609E542B852C82D042BB8A‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,‘‘),(‘mysql‘,‘root‘,‘‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,‘‘),(‘127.0.0.1‘,‘root‘,‘‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,‘‘),(‘::1‘,‘root‘,‘‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,‘‘),(‘localhost‘,‘‘,‘‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,NULL),(‘mysql‘,‘‘,‘‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,NULL),(‘localhost‘,‘yuanqk‘,‘*7B7BAF90C18E5D56BEBC190308B11429F282F5D5‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,NULL),(‘localhost‘,‘yuanqk1‘,‘*7B7BAF90C18E5D56BEBC190308B11429F282F5D5‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,NULL),(‘172.168.179.%‘,‘test‘,‘*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,NULL),(‘172.168.179.0/255.255.255.0‘,‘test2‘,‘*7CEB3FDE5F7A9C4CE5FBE610D7D8EDA62EBE5F4E‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,NULL),(‘%‘,‘test1‘,‘*06C0BF5B64ECE2F648B5F048A71903906BA08E5C‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,NULL); [[email protected] backup]#
4、测试下只备份表结构,参数 "-d":
[[email protected] backup]# mysqldump -uroot -pyuanqk -S /data/3306/mysql.sock --compact -d mysql user /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `user` ( `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT ‘‘, `User` char(16) COLLATE utf8_bin NOT NULL DEFAULT ‘‘, `Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT ‘‘, `Select_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Insert_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Update_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Delete_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Create_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Drop_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Reload_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Shutdown_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Process_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `File_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Grant_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `References_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Index_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Alter_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Show_db_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Super_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Create_tmp_table_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Lock_tables_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Execute_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Repl_slave_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Repl_client_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Create_view_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Show_view_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Create_routine_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Alter_routine_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Create_user_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Event_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Trigger_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `Create_tablespace_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘, `ssl_type` enum(‘‘,‘ANY‘,‘X509‘,‘SPECIFIED‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘‘, `ssl_cipher` blob NOT NULL, `x509_issuer` blob NOT NULL, `x509_subject` blob NOT NULL, `max_questions` int(11) unsigned NOT NULL DEFAULT ‘0‘, `max_updates` int(11) unsigned NOT NULL DEFAULT ‘0‘, `max_connections` int(11) unsigned NOT NULL DEFAULT ‘0‘, `max_user_connections` int(11) unsigned NOT NULL DEFAULT ‘0‘, `plugin` char(64) COLLATE utf8_bin DEFAULT ‘‘, `authentication_string` text COLLATE utf8_bin, PRIMARY KEY (`Host`,`User`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT=‘Users and global privileges‘; /*!40101 SET character_set_client = @saved_cs_client */; [[email protected] backup]#
5、只备份数据,参数 “-t”
[[email protected] backup]# mysqldump -uroot -pyuanqk -S /data/3306/mysql.sock --compact -t mysql user INSERT INTO `user` VALUES (‘localhost‘,‘root‘,‘*427D48AF41439640C2609E542B852C82D042BB8A‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,‘‘),(‘mysql‘,‘root‘,‘‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,‘‘),(‘127.0.0.1‘,‘root‘,‘‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,‘‘),(‘::1‘,‘root‘,‘‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘Y‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,‘‘),(‘localhost‘,‘‘,‘‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,NULL),(‘mysql‘,‘‘,‘‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,NULL),(‘localhost‘,‘yuanqk‘,‘*7B7BAF90C18E5D56BEBC190308B11429F282F5D5‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,NULL),(‘localhost‘,‘yuanqk1‘,‘*7B7BAF90C18E5D56BEBC190308B11429F282F5D5‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,NULL),(‘172.168.179.%‘,‘test‘,‘*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,NULL),(‘172.168.179.0/255.255.255.0‘,‘test2‘,‘*7CEB3FDE5F7A9C4CE5FBE610D7D8EDA62EBE5F4E‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,NULL),(‘%‘,‘test1‘,‘*06C0BF5B64ECE2F648B5F048A71903906BA08E5C‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘N‘,‘‘,‘‘,‘‘,‘‘,0,0,0,0,‘‘,NULL); [[email protected] backup]#
6、全库备份,参数 “-A”
[[email protected] backup]# mysqldump -uroot -pyuanqk -S /data/3306/mysql.sock -A -B -F --single-transaction --skip-events
--由于我使用的是5.5的版本,如果不指定“--single-transaction” 会报以下的错误:
[[email protected] backup]# mysqldump -uroot -pyuanqk -S /data/3306/mysql.sock -A -B --events|gzip >/tmp/db_all.sql.gz
mysqldump: Got error: 1142: SELECT,LOCK TABL command denied to user ‘root‘@‘localhost‘ for table ‘cond_instances‘ when using LOCK TABLES
[[email protected] backup]#
--另外也不能指定“--events”参数,如果指定了会报以下的错误:
Couldn‘t execute ‘show events‘: Access denied for user ‘user‘@‘localhost‘ to database ‘performance_schema‘
7、msqldump关键参数说明:
1、-A 备份所有数据库 2、-B 指定多个库,增加建库语句和use语句。 3、--compact 去掉注释,适合调试输出,生产不用 4、-F 刷新binlog日志 5、--master-data 增加binlog日志文件名及对应的位置点。 6、-x,--locak-all-table 锁表,在备份时为了保证数据一致性,一般都要锁表 7、-l,--lock-table 只读锁表 8、-d 只备份表结构 9、-t 只备份数据 10、--single-transaction 适合InnoDB事务数据库备份 一般myisam引擎备份是-x InnoDB使用--single-transaction确保在备份期间数据的一致性,即使其他数据提交,也不会dump出来 myisam: mysqldump -uroot -pyuanqk -A -B --master-data=2 -x|gzip > /tmp/all.sql.gz innodb: mysqldump -uroot -pyuanqk -A -B --master-data=2 --single-transaction|gzip > /tmp/all.sql.gz 如果是混合引擎,那以myisam为主。
8、测试恢复:
mysql> drop database yuanqk <===删除数据库 -> ; Query OK, 3 rows affected (0.23 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | yuanqk_gbk | | yuanqk_utf8 | +--------------------+ 5 rows in set (0.00 sec) mysql> source /data/3306/backup/yuanqk.backup.sql <===使用source命令进行恢复 Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 1 row affected (0.00 sec) Database changed Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.05 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) ERROR 1665 (HY000): Cannot execute statement: impossible to write to binary log since BINLOG_FORMAT = STATEMENT and at least one table uses a storage engine limited to row-based logging.
结果报错了。。。。这个错前两天测试碰到过,此时检查数据库和表是已经恢复了,但是数据是空的。
9、解决方案:
mysql> show variables like ‘binlog_format‘; +---------------+-----------+ | Variable_name | Value | +---------------+-----------+ | binlog_format | STATEMENT | +---------------+-----------+ 1 row in set (0.00 sec) mysql> exit
在my.cnf中增加binlog_format=MIXED参数,然后重启mysql
mysql> show variables like ‘binlog_format‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | MIXED |
+---------------+-------+
1 row in set (0.01 sec)
mysql>
10、再次测试恢复,成功恢复
mysql> drop database yuanqk; Query OK, 3 rows affected (0.02 sec) mysql> mysql> mysql> source /data/3306/backup/yuanqk.backup.sql Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 1 row affected (0.00 sec) Database changed Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.02 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 4 rows affected (0.00 sec) Records: 4 Duplicates: 0 Warnings: 0 .......省略...... mysql>
以上是关于mysql备份和恢复的主要内容,如果未能解决你的问题,请参考以下文章