mysqdump

Posted john5yang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysqdump相关的知识,希望对你有一定的参考价值。

[[email protected] b]# mysqldump -uroot -poldboy123 -A > /b/full.sql

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

[[email protected] b]#ll

total 656

-rw-r--r-- 1 root root 667860 Mar 11 15:39 full.sql

打开看下都是一些插入类的语句比较容易读懂,可以用awk.sed,等命令过滤需要的内容

[[email protected] b]#vim full.sql

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `mysql` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `mysql`;

-- 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 ‘‘,

?

对单库进行备份

[[email protected] b]#mysqldump -uroot -poldboy123 -B test test1 test2 > /b/test_jt.sql

mysql> show master status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| mysql-bin.000029 | 120 | | | |

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

?

?

-F flush-logs刷新binlog 有几个库刷新几个binlog

[[email protected] b]#mysqldump -uroot -poldboy123 -A -R --triggers --flush-logs > /b/flush.sql

mysql> show master status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| mysql-bin.000032 | 120 | | | |

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

?

1周日23:00全备此命令就是定时任务里的命令

[[email protected] ~]#mysqldump -uroot -poldboy123 -A -R --triggers --master-data=2 --single-transaction| gzip > /backup/all_$(date +%F-+%T).sql.

gz

?

周一数据变化

模拟故障此时有人删库了,业务,产品都瘫痪了

mysql> drop database oss;

经过排除,数据库对象找不到,作为运维,恢复数据库是我们的重任

产品,业务,开发开会研究这个问题,到底是怎么回事

?

我打开crontalb 发现备份脚本任务,查看里面的脚本,备份数据在/backup下,起点应该在备份文件22行那里,

技术图片技术图片技术图片????

恢复

应该先检查全备

  1. 检测全备找到二进制日志.起始位置,

-- CHANGE MASTER TO MASTER_LOG_FILE=‘mysql-bin.000013‘, MASTER_LOG_POS=120;

?

  1. 恢复全备????

mysql> ????????????

mysql> source /backup/all_2019-03-04-+23:38:10.sql

?

  1. 截取二进制日志

    查看当前所用的binlog

    mysql> show master status;

?

[[email protected] b]#mysqldump -uroot -poldboy123 -A -R --triggers --master-data=2 --single-transaction | gzip > /b/all_`date +%F-%H-%M-%S`.sql.gz

?

?

模拟数据变化

mysql> create table ttt.nnn(id int primary key auto_increment not null, name char(20) not null);

mysql> insert into nnn values(1,‘aa‘);

Query OK, 1 row affected (0.00 sec)

?

mysql> insert into nnn values(2,‘bb‘);

Query OK, 1 row affected (0.00 sec)

?

mysql> insert into nnn values(3,‘cc‘);

Query OK, 1 row affected (0.00 sec)

?

mysql> insert into nnn values(4,‘dd‘);

Query OK, 1 row affected (0.00 sec)

?

查看插入的数据

mysql> select * from ttt.nnn;

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

| id | name |

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

| 1 | aa |

| 2 | bb |

| 3 | cc |

| 4 | dd |

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

?

mysql> show master status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| mysql-bin.000028 | 850 | | | |

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

?

模拟删库

mysql> drop databases ttt;

?

?

全备时候加了 master-data=2 ,生成的备份文件,22行有恢复数据的起点,120

[[email protected] ~]#sed -n ‘22p‘ /b/all_2019-03-11-19-03-55.sql

-- CHANGE MASTER TO MASTER_LOG_FILE=‘mysql-bin.000028‘, MASTER_LOG_POS=120;

?

?

查看当前binlog是000028

mysql> show master status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| mysql-bin.000028 | 939 | | | |

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

?

?

结束点位置 ,就是drop pos的位置

mysql> show binlog events in "mysql-bin.000028";

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

| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |

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

| mysql-bin.000028 | 4 | Format_desc | 6 | 120 | Server ver: 5.6.40-log, Binlog ver: 4 |

| mysql-bin.000028 | 120 | Query | 6 | 224 | create database ttt charset utf8 |

| mysql-bin.000028 | 224 | Query | 6 | 384 | use `ttt`; create table ttt.nnn(id int primary key auto_increment not null, name char(20) not null) |

| mysql-bin.000028 | 384 | Query | 6 | 455 | BEGIN |

| mysql-bin.000028 | 455 | Table_map | 6 | 503 | table_id: 323 (ttt.nnn) |

| mysql-bin.000028 | 503 | Write_rows | 6 | 546 | table_id: 323 flags: STMT_END_F |

| mysql-bin.000028 | 546 | Table_map | 6 | 594 | table_id: 323 (ttt.nnn) |

| mysql-bin.000028 | 594 | Write_rows | 6 | 637 | table_id: 323 flags: STMT_END_F |

| mysql-bin.000028 | 637 | Table_map | 6 | 685 | table_id: 323 (ttt.nnn) |

| mysql-bin.000028 | 685 | Write_rows | 6 | 728 | table_id: 323 flags: STMT_END_F |

| mysql-bin.000028 | 728 | Table_map | 6 | 776 | table_id: 323 (ttt.nnn) |

| mysql-bin.000028 | 776 | Write_rows | 6 | 819 | table_id: 323 flags: STMT_END_F |

| mysql-bin.000028 | 819 | Xid | 6 | 850 | COMMIT /* xid=3690 */ |

| mysql-bin.000028 | 850 | Query | 6 | 939 | drop database ttt |

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

?

[[email protected] ~]#mysqlbinlog -uroot -poldboy123 --start-position=120 --stop-position=850 /data/mysql/mysql-bin.000028 > /b/delttt_binlog.sql

?

停止binlog写入

mysql> set sql_log_bin=1

?

恢复全备:

mysql> source /b/all_2019-03-11-19-03-55.sql;

mysql> source /b/delttt_binlog.sql;

?

?

数据恢复了

mysql> select * from ttt.nnn;

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

| id | name |

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

| 1 | aa |

| 2 | bb |

| 3 | cc |

| 4 | dd |

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

?

?

以上是关于mysqdump的主要内容,如果未能解决你的问题,请参考以下文章