[root@bug ~]# ? mysqldump
--master-data[=#]
mysqldump导出数据时,当这个参数的值为1的时候,mysqldump出来的文件就会包括CHANGE MASTER TO这个语句,CHANGE MASTER TO后面紧接着就是file和position的记录,在slave上导入数据时就会执行这个语句,salve就会根据指定这个文件位置从master端复制binlog。
--master-data=2表示在dump过程中记录主库的binlog和pos点,并在dump文件中注释掉这一行;
--master-data=1表示在dump过程中记录主库的binlog和pos点,并在dump文件中不注释掉这一行,即恢复时会执行;
默认情况下这个值是1。
--lock-tables #Lock all tables for read.这样在flush tables with read lock后,直到整个dump过程结束,断开线程后才会unlock tables释放锁,整个dump过程其他线程不可写,从而保证数据的一致性,适用于MyISAM存储引擎, 因为MyISAM存储引擎不支持事物。
--single-transaction #设置事务的隔离级别,适用于innodb存储引擎。
--dump-slave=2表示在dump过程中,在从库dump,mysqldump进程也要在从库执行,记录当时主库的binlog和pos点,并在dump文件中注释掉这一行;
--dump-slave=1表示在dump过程中,在从库dump,mysqldump进程也要在从库执行,记录当时主库的binlog和pos点,并在dump文件中不注释掉这一行;
注意,在从库上执行备份时,即--dump-slave=2,这时整个dump过程都是stop io_thread的状态。
详细信息,引用一位大牛的文章:
https://blog.csdn.net/rewiner120/article/details/70598828
二,操作记录
适用于innodb引擎,mysql5.7,编译安装
2.1构造测试数据
mysql> create database test;
Query OK, row affected (0.00 sec) mysql> use test
Database changed mysql> create table a(x int() not null auto_increment primary key);
Query OK, rows affected (0.01 sec) mysql> insert into a values();
Query OK, row affected (0.00 sec) mysql> insert into a values(),(),(),(),(),(),(),(),(),();
Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: mysql> select count(*) from a;
+----------+
| count(*) |
+----------+
| |
+----------+
row in set (0.00 sec)
2.2备份数据
[root@bug ~]# mysqldump --single-transaction\
> --flush-logs --master-data=\
> --set-gtid-purged=off --all-databases\
> -uroot -porange -S/mysqldb/d_red/mysql.sock\
> >/mysqldb/backup/`date "+%Y_%m_%d_%H_%M"`.dump [root@bug ~]# cd /mysqldb/backup/
[root@bug backup]# ll
总用量
-rw-r--r-- root root 8月 : 2018_08_31_20_01.dump
2.3,模拟业务,误删数据
mysql> drop table a;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from a;
ERROR 1146 (42S02): Table 'test.a' doesn't exist
2.4,使用mysqldump恢复数据
[root@bug ~]# mysql -uroot -porange -S/mysqldb/d_red/mysql.sock< /mysqldb/backup/2018_08_31_20_01.dump mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 3161 (HY000) at line 70: Storage engine MyISAM is disabled (Table creation is disallowed).
2.5恢复失败,排查错误
[root@bug log]# more general.log 2018-08-31T20:01:50.058290+08:00 22 Query DROP TABLE IF EXISTS `columns_priv`
2018-08-31T20:01:50.060213+08:00 22 Query /*!40101 SET @saved_cs_client = @@character_set_client */
2018-08-31T20:01:50.060407+08:00 22 Query /*!40101 SET character_set_client = utf8 */
2018-08-31T20:01:50.060973+08:00 22 Query CREATE TABLE `columns_priv` (
`Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
`Db` char(64) COLLATE utf8_bin NOT NULL DEFAULT '',
`User` char(32) COLLATE utf8_bin NOT NULL DEFAULT '',
`Table_name` char(64) COLLATE utf8_bin NOT NULL DEFAULT '',
`Column_name` char(64) COLLATE utf8_bin NOT NULL DEFAULT '',
`Timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`Column_priv` set('Select','Insert','Update','References') CHARACTER SET utf8 NOT NULL DEFAULT '',
PRIMARY KEY (`Host`,`Db`,`User`,`Table_name`,`Column_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Column privileges'
2018-08-31T20:01:50.062082+08:00 22 Quit
2018-08-31T20:01:50.102468+08:00 21 Query show engine MyISAM status #日志文件到此停止输出,初步判断因为出现show engine MyISAM status语句,导致mysqldump脚本执行过程报错跳出.
#查看备份集文件,重点检查存储引擎部分,
[root@bug backup]# more 2018_08_31_20_01.dump -- Table structure for table `columns_priv`
-- DROP TABLE IF EXISTS `columns_priv`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `columns_priv` (
`Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
`Db` char(64) COLLATE utf8_bin NOT NULL DEFAULT '',
`User` char(32) COLLATE utf8_bin NOT NULL DEFAULT '',
`Table_name` char(64) COLLATE utf8_bin NOT NULL DEFAULT '',
`Column_name` char(64) COLLATE utf8_bin NOT NULL DEFAULT '',
`Timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`Column_priv` set('Select','Insert','Update','References') CHARACTER SET utf8 NOT NULL DEFAULT '',
PRIMARY KEY (`Host`,`Db`,`User`,`Table_name`,`Column_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Column privileges';
/*!40101 SET character_set_client = @saved_cs_client */;
#果然,在备份的过程中,创建权限表的时候,mysqldump使用了MyISAM引擎,而数据库设置的默认存储引擎是innodb;
#到数据库内,进行进一步确认 mysql> show variables like '%engine%';
+----------------------------------+------------------+
| Variable_name | Value |
+----------------------------------+------------------+
| default_storage_engine | InnoDB |
| default_tmp_storage_engine | InnoDB |
| disabled_storage_engines | MyISAM,FEDERATED |
| internal_tmp_disk_storage_engine | InnoDB |
+----------------------------------+------------------+
4 rows in set (0.01 sec) #问题锁定无误,MyISAM引擎被禁用,导致恢复时,无法创建权限表
2.5故障排除
mysql> set disabled_storage_engines=FEDERATED;
ERROR 1238 (HY000): Variable 'disabled_storage_engines' is a read only variable #在数据库内修改失败,此参数为只读,只能去配置文件内修改,因为我采用的是个性化编译安装,所以默认配置文件为自定义的/mysqldb/d_red/conf/red.cnf. [root@bug ~]# vi /mysqldb/d_red/conf/red.cnf disabled_storage_engines ="FEDERATED" #找到disabled_storage_engines参数,修改赋值. #因为修改的是静态参数,所以需要重启数据库才能生效, [root@bug bin]# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/mysqldb/d_red/mysql.sock' (2) #关闭数据库进程后,发现无法登陆 [root@bug bin]# ps -ef|grep mysql
root 19400 7903 0 20:36 pts/1 00:00:00 grep --color=auto mysql #发现没有mysqld进程,也没有mysqld_safe进程,判断为启动失败 #查看错误日志 [root@bug log]# more mysql.err 2018-08-31T20:33:38.990846+08:00 0 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
2018-08-31T20:33:38.991415+08:00 0 [ERROR] unknown variable 'rpl_semi_sync_master_enabled=ON' #提示权限表故障,原因很简单,当在使用mysqldump恢复时,默认情况下,恢复某表前,mysqldump会先删除同名的表,但权限表删除后,又没能恢复成功,导致数据库无法启动. #解决思路,既然手里有备份的数据文件,只要构造全部的系统表的表结构就行了 #进行数据库的初始化,就能解决表构造表结构的问题. [root@bug base]# mysqld --defaults-file=/mysqldb/d_red/conf/red.cnf --initialize-insecure --user=mysql --datadir=/mysqldb/d_red/data/ #因为使用了--initialize-insecure参数,所以初始化后的数据库密码为空 #使用mysqldump恢复数据,只能在数据库启动的情况下进行 #mysqldump默认会将数据恢复到同名的库下 #所以要先启动数据库,登陆,创建同名的库 [root@bug d_red]# mysql -uroot -S/mysqldb/d_red/mysql.sock mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.01 sec) mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec) mysql> create database a;
Query OK, 1 row affected (0.01 sec) #应用备份文件,进行数据恢复 [root@bug data]# mysql -uroot -p123456 -S/mysqldb/d_red/mysql.sock< /mysqldb/backup/2018_08_31_20_01.dump
mysql: [Warning] Using a password on the command line interface can be insecure. #登陆数据库,进行验证
mysql> use test
Database changed
mysql> select * from a;
+----+
| x |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
+----+
11 rows in set (0.00 sec) #被误删的数据已经全部找回