hydd的Linux笔记Day56

本文详细介绍了Linux环境下MySQL的用户授权,包括grant授权、权限撤销,以及root密码的重置方法。此外,还讲解了数据库的完全备份策略,包括物理备份和逻辑备份的操作步骤。最后,讨论了binlog日志的重要性和如何利用binlog进行数据恢复,为数据库运维提供了实用指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Day56

用户授权

用户授权

grant授权

授权:添加用户并设置权限。

命令格式

​ grant 权限列表 on 库名 to 用户名@“客户端地址”

​ identified by “密码” //授权用户密码

​ wIth grant option; //有授权权限,可选项

权限列表

all		//所有权限
usage	//无权限
seelct,update,insert	//个别权限
select,update(字段1,……,字段N)	//指定字段

库名

*.*			//所有库所有表
库名.*		//一个库
库名.表名		//一张表

用户名

授权时自定义 要有标识性

存储在mysql库的user表里

客户端地址

%					//所有主机
192.168.4.%			//网段内所有的主机
192.168.4.1			//1台主机
localhost			//数据库服务器本机
相关命令

登录用户使用

命令作用
select user();显示用户名及客户端地址
show grants;用户显示自身访问权限
show grants for 用户名@“客户端地址”;管理员查看已有授权用户权限
set password= password(“密码”);授权用户连接后修改连接密码
set password for 用户@“客户端地址”=password(“密码”);管理员重置授权用户连接密码
drop user 用户名@“客户端地址”;删除授权用户(必须有管理员权限)
授权库

mysql库记录授权信息,主要表如下:

user表 记录已有授权用户及权限

db表 记录已有授权用户对数据库的访问权限

tables_priv表 记录已有授权用户对表的访问权限

columns_priv表 记录已有授权用户对字段的访问权限

查看表记录可以获取用户权限:也可以通过更新记录,修改用户权限

撤销权限

命令格式:

revoke 权限列表 on 库名.表 from 用户名@“客户端地址”;

root密码

root密码忘记怎么办

1.停止MySQL服务程序

[root@dbsvr1 ~]# systemctl  stop mysqld.service           //停止服务
[root@dbsvr1 ~]# systemctl  status mysqld.service          //确认状态
mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled)
   Active: inactive (dead) since 五 2017-04-07 23:01:38 CST; 21s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 20260 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 20238 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 20262 (code=exited, status=0/SUCCESS)

2.跳过授权表启动MySQL服务程序

这一步主要利用mysqld的 --skip-grant-tables选项

修改my.cnf配置,添加 skip_grant_tables=1启动设置:

[root@dbsvr1 ~]# vim /etc/my.cnf
[mysqld]
skip_grant_tables
.. ..
[root@dbsvr1 ~]# systemctl  start mysqld.service
[root@dbsvr1 ~]# service mysql status
mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled)
   Active: active (running) since 五 2017-04-07 23:40:20 CST; 40s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 11698 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 11676 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 11701 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─11701 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.p...

3.修改root密码

由于前一步启动的MySQL服务跳过了授权表,所以可以root从本机直接登录

[root@dbsvr1 ~]# mysql  //直接回车即可
                                
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 

进入 mysql> 环境后,通过修改mysql库中user表的相关记录,重设root用户从本机登录的密码:

mysql> UPDATE mysql.user SET authentication_string=PASSWORD('123qqq…A')
    -> WHERE user='root' AND host='localhost';              //重设root的密码
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1
mysql> FLUSH PRIVILEGES;                                  //刷新授权表
Query OK, 0 rows affected (0.01 sec)
mysql> exit                                              //退出mysql> 环境
Bye

通过执行“FLUSH PRIVILEGES;”可使授权表立即生效,对于正常运行的MySQL服务,也可以用上述方法来修改密码,不用重启服务。本例中因为是恢复密码,最好重启MySQL服务程序,所以上述“FLUSH PRIVILEGES;”操作可跳过。

4.已正常方式重启MySQL服务程序

如果前面是修改/etc/my.cnf配置的方法来跳过授权表,则重置root密码后,应去除相应的设置以恢复正常:

[root@dbsvr1 ~]# vim /etc/my.cnf
[mysqld]
#skip_grant_tables=1                              //注释掉或删除此行
.. ..

按正常方式,通过mysql脚本重启服务即可:

[root@dbsvr1 ~]# systemctl  restart mysqld.service

验证无密码登录时,将会被拒绝:

[root@dbsvr1 ~]# mysql -u root
Enter password:                            //没有跳过授权表回车会报错
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

只有提供重置后的新密码,才能成功登入:

[root@dbsvr1 ~]# mysql -uroot –p123qqq…A
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 
重置root密码

正常的前提是:已知当前MySQL管理用户(root)的密码。

1)方法1,在Shell命令行下设置

使用mysqladmin管理工具,需要验证旧的密码。比如,以下操作将会把root的密码设置为 1234567:

[root@dbsvr1 ~]# mysqladmin -uroot -p password 'A…qqq321'                    
Enter password:                                   //验证原来的密码
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.                              //提示明文修改不安全,并不是报错
[root@dbsvr1 ~]# mysql -uroot –pA…qqq321  //使用修改后的密码登录
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 
修改管理员root密码的其他方法

1)方法1,以root登入mysql> 后,使用SET PASSWORD指令设置

这个与新安装MySQL-server后首次修改密码时要求的方式相同,平时也可以用:

mysql> SET PASSWORD FOR root@localhost=PASSWORD('1234567');

2)方法2,以root登入mysql> 后,使用GRANT授权工具设置

这个是最常见的用户授权方式(下一节会做更多授权的练习):

mysql> GRANT all ON *.* TO root@localhost IDENTIFIED BY '1234567';

3)方法3,以root登入mysql> 后,使用UPDATE更新相应的表记录

这种方法与恢复密码时的操作相同:

mysql> UPDATE mysql.user SET authentication_string=PASSWORD('1234567')
    -> WHERE user='root' AND host='localhost';          //重设root的密码
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 1
mysql> FLUSH PRIVILEGES;         

在上述方法中,需要特别注意:当MySQL服务程序以 skip-grant-tables 选项启动时,如果未执行“FLUSH PRIVILEGES;”操作,是无法通过SET PASSWORD或者GRANT方式来设置密码的。比如,验证这两种方式时,都会看到ERROR 1290的出错提示:

mysql> UPDATE mysql.user SET authentication_string=PASSWORD('1234567')
    -> WHERE user='root' AND host='localhost';          //重设root的密码
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 1
mysql> FLUSH PRIVILEGES;                                  //刷新授权表

在上述方法中,需要特别注意:当MySQL服务程序以 skip-grant-tables 选项启动时,如果未执行“FLUSH PRIVILEGES;”操作,是无法通过SET PASSWORD或者GRANT方式来设置密码的。比如,验证这两种方式时,都会看到ERROR 1290的出错提示:

mysql> SET PASSWORD FOR root@localhost=PASSWORD('1234567');
mysql> GRANT all ON *.* TO root@localhost IDENTIFIED BY '1234567';

完全备份

备份概述

数据备份的方式

物理备份

​ 冷备:cp、tar、……

逻辑备份

​ mysqldump //备份命令

​ mysql //恢复命令

物理备份及恢复

备份操作

cp -r /var/lib/mysql 备份目录 /mysql.bak

tar -zcvf /root/mysql.tar.gz /var/lib/mysql/*

恢复操作

cp -r 备份目录/mysql.bak /var/lib/mysql/

tar -zxvf /root/mysql.tar.gz -C /var/lib/mysql/

chown -R mysql:mysql /var/lib/mysql

逻辑备份

数据备份策略

完全备份:备份所有数据

增量备份:备份上次备份后,所有新产生的数据

差异备份:备份完全备份后,所有新产生的数据

完全备份及恢复

完全备份

mysqldump -uroot -p密码 库名 > 目录/xxx.sql

完全恢复

mysql -uroot -p密码 [库名] < 目录/xxx.sql

备份时库名表示方式

–all-databases 或 -A //所有库

数据库名 //单个库

数据库名 表名 //单张表

-B 数据库1 数据2 //多个库

注意事项:无论备份还是恢复,都要验证用户权限

案例

练习mysqldump命令的使用

1)备份MySQL服务器上的所有库

将所有的库备份为mysql-all.sql文件:

[root@dbsvr1 ~]# mysqldump -u root -p --all-databases > /root/alldb.sql
Enter password:                                  //验证口令
[root@dbsvr1 mysql]# file /root/alldb.sql          //确认备份文件类型
/root/alldb.sql: UTF-8 Unicode English text, with very long lines

查看备份文件alldb.sql的部分内容:

[root@dbsvr1 ~]# grep -vE '^/|^-|^$' /root/alldb.sql | head -15
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `home` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `home`;
DROP TABLE IF EXISTS `biao01`;
CREATE TABLE `biao01` (
  `id` int(2) NOT NULL,
  `name` varchar(8) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
LOCK TABLES `biao01` WRITE;
UNLOCK TABLES;
DROP TABLE IF EXISTS `biao02`;
CREATE TABLE `biao02` (
  `id` int(4) NOT NULL,
  `name` varchar(8) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
.. ..

注意:若数据库都使用MyISAM存储引擎,可以采用冷备份的方式,直接复制对应的数据库目录即可;恢复时重新复制回来就行。

2)只备份指定的某一个库

将userdb库备份为userdb.sql文件:

[root@dbsvr1 ~]# mysqldump -u root -p userdb > userdb.sql
Enter password:                                  //验证口令

查看备份文件userdb.sql的部分内容:

[root@dbsvr1 ~]# grep -vE '^/|^-|^$' /root/userdb.sql
DROP TABLE IF EXISTS `stu_info`;
CREATE TABLE `stu_info` (
  `name` varchar(12) NOT NULL,
  `gender` enum('boy','girl') DEFAULT 'boy',
  `age` int(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
LOCK TABLES `stu_info` WRITE;
.. ..

3)同时备份指定的多个库

同时备份mysql、userdb库,保存为mysql+userdb.sql文件:

[root@dbsvr1 ~]# mysqldump -u root -p -B mysql  userdb > mysql+test+userdb.sql
Enter password:                                  //验证口令

查看备份文件userdb.sql的部分内容:

[root@dbsvr1 ~]# grep '^CREATE DATA' /root/mysql+userdb.sql
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `mysql` /*!40100 DEFAULT CHARACTER SET latin1 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `userdb` /*!40100 DEFAULT CHARACTER SET latin1 */;

使用mysql 命令恢复删除的数据

以恢复userdb库为例,可参考下列操作。通常不建议直接覆盖旧库,而是采用建立新库并导入逻辑备份的方式执行恢复,待新库正常后即可废弃或删除旧库。

1)创建名为userdb2的新库

mysql> CREATE DATABASE userdb2;

2)导入备份文件,在新库中重建表及数据

[root@dbsvr1 ~]# mysql -u root -p userdb2 < /root/userdb.sql
Enter password:                                  //验证口令

3)确认新库正常,启用新库

mysql> USE userdb2;                              //切换到新库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> SELECT sn,username,uid,gid,homedir          //查询数据,确认可用
    -> FROM userlist LIMIT 10;
+----+----------+-----+-----+-----------------+
| sn | username | uid | gid | homedir         |
+----+----------+-----+-----+-----------------+
|  1 | root     |   0 |   0 | /root           |
|  2 | bin      |   1 |   1 | /bin            |
|  3 | daemon   |   2 |   2 | /sbin           |
|  4 | adm      |   3 |   4 | /var/adm        |
|  5 | lp       |   4 |   7 | /var/spool/lpd  |
|  6 | sync     |   5 |   0 | /sbin           |
|  7 | shutdown |   6 |   0 | /sbin           |
|  8 | halt     |   7 |   0 | /sbin           |
|  9 | mail     |   8 |  12 | /var/spool/mail |
| 10 | operator |  11 |   0 | /root           |
+----+----------+-----+-----+-----------------+

4)废弃或删除旧库

mysql> DROP DATABASE userdb;

binlog日志

日志概述

什么是binlog日志

binlog 也叫二进制日志,MySQL服务日志的一种,记录出查询外所有的SQL命令可用于数据备份和恢复,配置mysql主从同步的必要条件

启用日志
配置项用途
server_id=数字指定id值(1-255)
log_bin[=目录名/文件名]启用binlog日志
max_binlog_size=数值m指定日志文件容量,默认1G

修改配置文件,并重启服务。

[root@dbsvr1 ~]# vim  /etc/my.cnf
[mysqld]    
server_id=1  //指定server_id
log-bin=/mylog/db50  //指定日志目录及名称                        [root@dbsvr1 ~]# mkdir  /mylog   //创建目录
[root@dbsvr1 ~]# chown  mysql  /mylog   //修改所有者
[root@dbsvr1 ~]# systemctl  restart mysqld.service  //重启服务

biglog相关文件

主机名-bin.index 索引文件

主机名-bin.000001 第一个二进制日志

主机名-bin.000002 第二个二进制日志

清理日志

删除指定编号之前的binlog日志文件

mysql> purge master logs to “binlog文件名”;

删除所有binlog日志,重建新日志

mysql>reset master;

恢复数据

分析日志

查看日志当前记录格式

三种记录方式:

1.statement 2.row 3.mixed

show variables like "binlog_format";

修改日志记录格式

vim /etc/my.cnf
	[mysql]
		binlog_format="名称"
systemctl restart mysqld

查看日志内容

​ mysqlbinlog [选项] binlog日志文件名

选项用途
–start-datetime=“yyyy-mm-dd hh:mm:ss”起始时间
–stop-datetime=“yyyy-mm-dd hh:mm:ss”结束时间
–start-position=数字起始偏移量
–stop-position=数字结束偏移量
恢复数据

基本思路

​ -使用mysqlbinlog提取历史SQL操作

​ -通过管道交给mysql命令执行

格式:
mysqlbinlog 日志文件 | mysql -uroot -p密码

案例

查看日志信息

[root@localhost ~]# mysql -uroot -p123qqq...A //管理员登录
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show master status; //查看日志信息
+-------------+----------+--------------+------------------+-------------------+
| File        | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------+----------+--------------+------------------+-------------------+
| db50.000001 |      154 |              |                  |                   |
+-------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

删除编号3之前的日志文件

mysql> purge  master  logs  to  "db50.000003"; //删除日志
Query OK, 0 rows affected (0.05 sec)
mysql> system ls /mylog/    //查看日志文件
db50.000003  db50.000004  db50.index
mysql> 
mysql> system cat /mylog/db50.index //查看索引文件
/mylog/db50.000003
/mylog/db50.000004
mysql>
使用binlog日志恢复数据

启用binlog日志

1)调整/etc/my.cnf配置,并重启服务

[root@dbsvr1 ~]# vim  /etc/my.cnf
[mysqld]
server_id=1  //定义server_id
log-bin=mysql-bin  //定义日志名
binlog_format=”mixed”     //定义日志格式                           
[root@dbsvr1 ~]# systemctl  restart mysqld.service //重启服务

2)确认binlog日志文件

新启用binlog后,每次启动MySQl服务都会新生成一份日志文件:

[root@dbsvr1 ~]# ls /var/lib/mysql/mysql-bin.*
/var/lib/mysql/mysql-bin.000001  /var/lib/mysql/mysql-bin.index

其中mysql-bin.index文件记录了当前保持的二进制文件列表:

重启MySQL服务程序,或者执行SQL操作“FLUSH LOGS;”,会生成一份新的日志:

利用binlog日志重做数据库操作

1)执行数据库表添加操作

创建db1·库tb1表,表结构自定义:

mysql> CREATE DATABASE db1;
Query OK, 1 row affected (0.05 sec)
mysql> USE db1;
Database changed
mysql> CREATE TABLE tb1(
    -> id int(4) NOT NULL,name varchar(24)
    -> );
Query OK, 0 rows affected (0.28 sec)

插入3条表记录:

mysql> INSERT INTO tb1 VALUES
    -> (1,'Jack'),
    -> (2,'Kenthy'),
    -> (3,'Bob');

确认插入的表记录数据:

mysql> SELECT * FROM tb1;
+----+--------+
| id | name   |
+----+--------+
|  1 | Jack   |
|  2 | Kenthy |
|  3 | Bob    |
+----+--------+

2)删除前一步添加的3条表记录

执行删除所有表记录操作:

mysql> DELETE FROM tb1;

确认删除结果:

mysql> SELECT * FROM tb1;

通过binlog日志恢复表记录

binlog会记录所有的数据库、表更改操作,所以可在必要的时候重新执行以前做过的一部分数据操作,但对于启用binlog之前已经存在的库、表数据将不适用。

根据上述“恢复被删除的3条表记录”的需求,应通过mysqlbinlog工具查看相关日志文件,找到删除这些表记录的时间点,只要恢复此前的SQL操作(主要是插入那3条记录的操作)即可。

1)查看mysql-bin.000002日志内容

[root@dbsvr1 ~]# mysqlbinlog /var/lib/mysql/mysql-bin.000002
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#170412 12:05:32 server id 1  end_log_pos 123 CRC32 0x6d8c069c  Start: binlog v 4, server v 5.7.17-log created 170412 12:05:32 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
jKftWA8BAAAAdwAAAHsAAAABAAQANS43LjE3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAACMp+1YEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AZwGjG0=
'/*!*/;
# at 123
#170412 12:05:32 server id 1  end_log_pos 154 CRC32 0x17f50164  Previous-GTIDs
# [empty]
# at 154
#170412 12:05:59 server id 1  end_log_pos 219 CRC32 0x4ba5a976  Anonymous_GTID  last_committed=0        sequence_number=1
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 219
#170412 12:05:59 server id 1  end_log_pos 310 CRC32 0x5b66ae13  Query   thread_id=3     exec_time=0     error_code=0
SET TIMESTAMP=1491969959/*!*/;
SET @@session.pseudo_thread_id=3/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1436549152/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
CREATE DATABASE db1
/*!*/;
# at 310
#170412 12:06:23 server id 1  end_log_pos 375 CRC32 0x2967cc28  Anonymous_GTID  last_committed=1        sequence_number=2
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 375
#170412 12:06:23 server id 1  end_log_pos 502 CRC32 0x5de09aae  Query   thread_id=3     exec_time=0     error_code=0
use `db1`/*!*/;
SET TIMESTAMP=1491969983/*!*/;
CREATE TABLE tb1(
id int(4) NOT NULL,name varchar(24)
)
/*!*/;
# at 502
#170412 12:06:55 server id 1  end_log_pos 567 CRC32 0x0b8cd418  Anonymous_GTID  last_committed=2        sequence_number=3
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 567
#170412 12:06:55 server id 1  end_log_pos 644 CRC32 0x7e8f2fa0  Query   thread_id=3     exec_time=0     error_code=0
SET TIMESTAMP=1491970015/*!*/;
BEGIN
/*!*/;
# at 644
#170412 12:06:55 server id 1  end_log_pos 772 CRC32 0x4e3f728e  Query   thread_id=3     exec_time=0     error_code=0                            //插入表记录的起始时间点 
SET TIMESTAMP=1491970015/*!*/;
INSERT INTO tb1 VALUES(1,'Jack'),(2,'Kenthy'), (3,'Bob')
/*!*/;
# at 772
#170412 12:06:55 server id 1  end_log_pos 803 CRC32 0x6138b21f  Xid = 10
                                                      //确认事务的时间点 
COMMIT/*!*/;
# at 803
#170412 12:07:24 server id 1  end_log_pos 868 CRC32 0xbef3f472  Anonymous_GTID  last_committed=3        sequence_number=4
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 868
#170412 12:07:24 server id 1  end_log_pos 945 CRC32 0x5684e92c  Query   thread_id=3     exec_time=0     error_code=0
SET TIMESTAMP=1491970044/*!*/;
BEGIN
/*!*/;
# at 945
#170412 12:07:24 server id 1  end_log_pos 1032 CRC32 0x4c1c75fc         Query   thread_id=3     exec_time=0     error_code=0            //删除表记录的时间点
SET TIMESTAMP=1491970044/*!*/;
DELETE FROM tb1
/*!*/;
# at 1032
#170412 12:07:24 server id 1  end_log_pos 1063 CRC32 0xccf549b2         Xid = 12
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

2) 执行指定Pos节点范围内的sql命令恢复数据

根据上述日志分析,只要恢复从2014.01.12 20:12:14到2014.01.12 20:13:50之间的操作即可。可通过mysqlbinlog指定时间范围输出,结合管道交给msyql命令执行导入重做:

[root@dbsvr1 ~]# mysqlbinlog \
    --start-datetime="2017-04-12 12:06:55" \ 
    --stop-datetime="2017-04-12 12:07:23" \
    /var/lib/mysql/mysql-bin.000002 | mysql -u root -p
Enter password:                                     //验证口令

3)确认恢复结果


mysql> SELECT * FROM db1.tb1;
+----+--------+
| id | name   |
+----+--------+
|  1 | Jack   |
|  2 | Kenthy |
|  3 | Bob    |
+----+--------+
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值