目前已经完成了mysql的主备模式的搭建。mysql
mysql> show global variables like "%binlog_format%";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW |
+---------------+-------+
复制代码
执行一条语句sql
delete from t /*comment*/ where a>=4 and t_modified<='2018-11-10' limit 1;
复制代码
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 | 2847 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> show binlog events in 'master.000005';
复制代码
查看binogshell
| mysql-bin.000005 | 2627 | Query | 1 | 2714 | BEGIN |
| mysql-bin.000005 | 2714 | Table_map | 1 | 2768 | table_id: 118 (testKeep.t) |
| mysql-bin.000005 | 2768 | Write_rows | 1 | 2816 | table_id: 118 flags: STMT_END_F |
| mysql-bin.000005 | 2816 | Xid | 1 | 2847 | COMMIT /* xid=1507 */ |
| mysql-bin.000005 | 2847 | Anonymous_Gtid | 1 | 2926 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'
复制代码
为何要切换为MIXED模式?数据库
row模式很是费空间,statement模式由于执行索引不必定相同可能会致使主备不一致问题,由此产生了row格式。因此线上 MySQL 设置的 binlog 格式是 statement 的话,那基本上就能够认为这是一个不合理的设置。你至少应该把 binlog 的格式设置为 mixed。bash
如何切换markdown
vi /etc/my.cnf # 根据自身状况调整位置
log-bin=mysql-bin
binlog_format=MIXED
mysql> show global variables like "%binlog_format%";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | MIXED |
+---------------+-------+
1 row in set (0.01 sec)
复制代码
并且因为拥有statement模式的特点,咱们在数据恢复上能够很方便函数
咱们用测试数据库testKeep分别测试增、删、改的恢复方法性能
mysql> select * from t;
+----+------+---------------------+
| id | a | t_modified |
+----+------+---------------------+
| 1 | 1 | 2018-11-13 00:00:00 |
| 2 | 2 | 2018-11-12 00:00:00 |
| 3 | 3 | 2018-11-11 00:00:00 |
| 4 | 4 | 2018-11-10 00:00:00 |
| 7 | 7 | 2018-11-15 00:00:00 |
+----+------+---------------------+
5 rows in set (0.00 sec)
复制代码
增:测试
mysql> insert into t values(8,8,'2021-6-4');
Query OK, 1 row affected (0.00 sec)
复制代码
mysql> show binlog events in 'mysql-bin.000007';
+------------------+-----+----------------+-----------+-------------+------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+------------------------------------------------------+
| mysql-bin.000007 | 4 | Format_desc | 1 | 125 | Server ver: 8.0.25, Binlog ver: 4 |
| mysql-bin.000007 | 125 | Previous_gtids | 1 | 156 | |
| mysql-bin.000007 | 156 | Anonymous_Gtid | 1 | 235 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000007 | 235 | Query | 1 | 333 | BEGIN |
| mysql-bin.000007 | 333 | Query | 1 | 462 | use `testKeep`; insert into t values(8,8,'2021-6-4') |
| mysql-bin.000007 | 462 | Xid | 1 | 493 | COMMIT /* xid=58 */ |
+------------------+-----+----------------+-----------+-------------+------------------------------------------------------+
6 rows in set (0.00 sec)
复制代码
咱们发现,日志行数比之前ROW 少特别多,并且完整的记录了sql语句,能够根据sql语句对误增的数据直接删除。ui
改
mysql> update t set a = 99 where id = 8;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
复制代码
mysql> show binlog events in 'mysql-bin.000007';
+------------------+-----+----------------+-----------+-------------+------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+------------------------------------------------------+
| mysql-bin.000007 | 4 | Format_desc | 1 | 125 | Server ver: 8.0.25, Binlog ver: 4 |
| mysql-bin.000007 | 125 | Previous_gtids | 1 | 156 | |
| mysql-bin.000007 | 156 | Anonymous_Gtid | 1 | 235 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000007 | 235 | Query | 1 | 333 | BEGIN |
| mysql-bin.000007 | 333 | Query | 1 | 462 | use `testKeep`; insert into t values(8,8,'2021-6-4') |
| mysql-bin.000007 | 462 | Xid | 1 | 493 | COMMIT /* xid=58 */ |
| mysql-bin.000007 | 493 | Anonymous_Gtid | 1 | 572 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000007 | 572 | Query | 1 | 671 | BEGIN |
| mysql-bin.000007 | 671 | Query | 1 | 797 | use `testKeep`; update t set a = 99 where id = 8 |
| mysql-bin.000007 | 797 | Xid | 1 | 828 | COMMIT /* xid=69 */ |
+------------------+-----+----------------+-----------+-------------+------------------------------------------------------+
10 rows in set (0.00 sec)
复制代码
它会修改事件先后的区别,能够根据这些区别而还原数据
删
mysql> delete from t where id = 8;
Query OK, 1 row affected (0.01 sec)
复制代码
mysql> show binlog events in 'mysql-bin.000007';
+------------------+------+----------------+-----------+-------------+------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+------+----------------+-----------+-------------+------------------------------------------------------+
| mysql-bin.000007 | 4 | Format_desc | 1 | 125 | Server ver: 8.0.25, Binlog ver: 4 |
| mysql-bin.000007 | 125 | Previous_gtids | 1 | 156 | |
| mysql-bin.000007 | 156 | Anonymous_Gtid | 1 | 235 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000007 | 235 | Query | 1 | 333 | BEGIN |
| mysql-bin.000007 | 333 | Query | 1 | 462 | use `testKeep`; insert into t values(8,8,'2021-6-4') |
| mysql-bin.000007 | 462 | Xid | 1 | 493 | COMMIT /* xid=58 */ |
| mysql-bin.000007 | 493 | Anonymous_Gtid | 1 | 572 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000007 | 572 | Query | 1 | 671 | BEGIN |
| mysql-bin.000007 | 671 | Query | 1 | 797 | use `testKeep`; update t set a = 99 where id = 8 |
| mysql-bin.000007 | 797 | Xid | 1 | 828 | COMMIT /* xid=69 */ |
| mysql-bin.000007 | 828 | Anonymous_Gtid | 1 | 907 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000007 | 907 | Query | 1 | 997 | BEGIN |
| mysql-bin.000007 | 997 | Query | 1 | 1108 | use `testKeep`; delete from t where id = 8 |
| mysql-bin.000007 | 1108 | Xid | 1 | 1139 | COMMIT /* xid=88 */ |
+------------------+------+----------------+-----------+-------------+------------------------------------------------------+
14 rows in set (0.00 sec)
复制代码
很明显咱们查出了 这条sql被建立以后发生的全部事情,被新增过,修改过,后来又被删除了。
从节点开启只读模式
set global read_only是全局级别的,置为1以后root用户还能够写,其余用户不能写
mysql> set global read_only=1;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%read_only%';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| innodb_read_only | OFF |
| read_only | ON |
| super_read_only | OFF |
| transaction_read_only | OFF |
+-----------------------+-------+
复制代码
#创建只读帐户并分配权限
CREATE USER '{帐户名称}'@'%' IDENTIFIED BY '{帐户密码}';
grant SELECT on *.* to '{帐户名称}'@'%';
复制代码
切换到只读用户,并进行测试
mysql> select count(*) from t;
+----------+
| count(*) |
+----------+
| 6 |
+----------+
1 row in set (0.00 sec)
mysql> insert into t values(15,15,'2021-6-4');
ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement
mysql> delete from t where id = 2;
ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement
mysql> update t set a = 99 where id = 2;
ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement
复制代码
结果显示能够查,不能够增删改。
成文时间比较长(老存货了),已经找不到参考文献。