1、登录MySQL,查看binlog日志的状态
登录MySQL后,输入show variables like '%log_bin%';查看到binlog日志为OFF关闭状态;(我的已经开了)
mysql> show variables like '%log_bin%';
+---------------------------------+---------------------------------+
| Variable_name | Value |
+---------------------------------+---------------------------------+
| log_bin | off |
| log_bin_basename | |
| log_bin_index | |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+---------------------------------+
6 rows in set (0.00 sec)
mysql>
2、开启MySQL binlog日志
退出MySQL,使用vi编辑器修改MySQL的my.cnf配置文件
在my.cnf配置文件中添加如下图的两句
[root@localhost /]# vim /opt/mysql/my.cnf
#开启二进制日志(默认放到data)
log-bin=mysql-bin
3、重启MySQL
查看/var/lib/mysql目录下的内容
使用命令:ls /var/mysql/data
auto.cnf ib_logfile0 localhost.localdomain.err mysql mysql-bin.000002 mysql-bin.000004 oper test
ibdata1 ib_logfile1 localhost.localdomain.pid mysql-bin.000001 mysql-bin.000003 mysql-bin.index performance_schema zabbix
此时再次进入MySQL,查看binlog日志的状态。显示binlog日志为ON开启状态
mysql> show variables like '%log_bin%';
+---------------------------------+---------------------------------+
| Variable_name | Value |
+---------------------------------+---------------------------------+
| log_bin | ON |
| log_bin_basename | /var/mysql/data/mysql-bin |
| log_bin_index | /var/mysql/data/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+---------------------------------+
6 rows in set (0.00 sec)
mysqlbinlog命令用法:shell> mysqlbinlog [options] log_file ...
1) mysqlbinlog 选项示例
常见的选项有以下几个:
--start-datetime
从二进制日志中读取指定等于时间戳或者晚于本地计算机的时间。取值如:="1470733768" 或者="2016-08-09 5:09:28"
示例:
[root@hcloud ~]# mysqlbinlog --start-datetime="2016-08-09 5:05:27" /var/mysql/data/mysql-bin.000001
--stop-datetime
从二进制日志中读取指定小于时间戳或者等于本地计算机的时间 取值和上述一样
--start-position
从二进制日志中读取指定position 事件位置作为开始。取值:="2698"
示例:
[root@hcloud ~]# mysqlbinlog --start-position="2698" /var/mysql/data/mysql-bin.000001
--stop-position
从二进制日志中读取指定position 事件位置作为事件截至。取值:="2698"
1) 安装好mysql后,检查开启binlog(上面已给出)
2) 检查下binlog
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 613 |
| mysql-bin.000002 | 143 |
| mysql-bin.000003 | 5963 |
| mysql-bin.000004 | 2460 |
+------------------+-----------+
4 rows in set (0.00 sec)
3) 先创建一些原始数据。
4) 备份还原 (完整备份以及还原)
先备份
[root@hcloud ~]# mysqldump -uroot -p Test_DB > /data/mysqlbackup/Test_DB_0809-16:50.sql
Enter password:
中间删数据
恢复
[root@hcloud ~]# mysql -uroot -p Test_DB < /data/mysqlbackup/Test_DB_0809-16\:50.sql
5) 利用binlog模拟还原
删数据
利用binlog恢复
a.生成sql文件
mysqlbinlog --stop-position=1709 /var/mysql/data/mysql-bin.000004 > /tmp/binlog.sql
b.sql文件导入