标签:事物 min tables mysql高可用 权限 节点 uid mit tom
mysqlfailover是mysql官方用python语言写的一款工具,包含在mysql utilities工具集中。主要作用是保障mysql高可用。他会定时检测节点状态,当master节点不可用时,会自动转移到从节点上,同时剩余的从节点都会指向转以后的节点。
mysqlfailover使用前提:
1、必须开启GTID模式,在GTID模式下,复制延迟已经减小到最低。我自己的生产环境中没有过延迟。
2、配置文件中必须添加:
report-host=
report-port=
master-info-repository=TABLE
relay-log-info-repository=TABLE
用于从节点可以被检测到。
3、权限:
必须要有with grant option权限。
安装也非常简单。
下载好mysql utilities工具集:https://downloads.mysql.com/archives/utilities/
unzip mysql-utilities-1.6.5.zip
cd mysql-utilities-1.6.5
python ./setup.py build
python ./setup.py install
到此为止安装完成。
使用:
mysqlfailover --master=failover:123456@‘192.168.0.105‘:3306 --discover-slaves-login=failover:123456 --daemon=start --log=/data/failover.log
建立好主从服务。这里略。
检测事物是否完整转移:
这里使用sysbentch工具来进行批量插入。
sysbench --test=oltp --mysql-db=test --mysql-user=root --mysql-password=123456 --oltp-table-size=1000000000 --oltp-num-tables=15 prepare 批量插入
sysbench 0.4.12.10: multi-threaded system evaluation benchmark
No DB drivers specified, using mysql
Creating table ‘sbtest1‘...
Creating table ‘sbtest5‘...
Creating table ‘sbtest4‘...
Creating table ‘sbtest8‘...
Creating table ‘sbtest9‘...
Creating table ‘sbtest6‘...
Creating table ‘sbtest2‘...
Creating table ‘sbtest‘...
Creating table ‘sbtest3‘...
Creating table ‘sbtest14‘...
Creating table ‘sbtest10‘...
Creating table ‘sbtest12‘...
Creating table ‘sbtest11‘...
Creating table ‘sbtest7‘...
Creating table ‘sbtest13‘...
Creating 1000000000 records in table ‘sbtest11‘...
Creating 1000000000 records in table ‘sbtest6‘...
Creating 1000000000 records in table ‘sbtest4‘...
Creating 1000000000 records in table ‘sbtest5‘...
Creating 1000000000 records in table ‘sbtest8‘...
Creating 1000000000 records in table ‘sbtest14‘...
Creating 1000000000 records in table ‘sbtest3‘...
Creating 1000000000 records in table ‘sbtest13‘...
Creating 1000000000 records in table ‘sbtest9‘...
Creating 1000000000 records in table ‘sbtest10‘...
Creating 1000000000 records in table ‘sbtest1‘...
Creating 1000000000 records in table ‘sbtest12‘...
Creating 1000000000 records in table ‘sbtest‘...
Creating 1000000000 records in table ‘sbtest7‘...
Creating 1000000000 records in table ‘sbtest2‘...
等待几分钟后:
kill -9 17448
kill -9 18350
之后,该工具自动转移输出,可以看到已经转移到了丛机上:
Q-quit R-refresh H-health G-GTID Lists U-UUIDs
Failed to reconnect to the master after 3 attemps.
Failover starting in ‘auto‘ mode...
# Candidate slave 192.168.0.106:3306 will become the new master.
# Checking slaves status (before failover).
# Preparing candidate for failover.
# Creating replication user if it does not exist.
# Stopping slaves.
# Performing STOP on all slaves.
# Switching slaves to new master.
# Disconnecting new master as slave.
# Starting slaves.
# Performing START on all slaves.
# Checking slaves for errors.
# Failover complete.
# Discovering slaves for master at 192.168.0.106:3306
Failover console will restart in 5 seconds.
MySQL Replication Failover Utility
Failover Mode = auto Next Interval = Sat Sep 1 15:43:11 2018
Master Information
------------------
Binary Log File Position Binlog_Do_DB Binlog_Ignore_DB
mysql-bin.000005 265370219
GTID Executed Set
0789f077-ad98-11e8-9f58-000c29715801:1-5 [...]
UUIDs
+----------------+-------+---------+--------+------------+---------+
| host | port | role | state | gtid_mode | health |
+----------------+-------+---------+--------+------------+---------+
| 192.168.0.106 | 3306 | MASTER | UP | ON | OK |
+----------------+-------+---------+--------+------------+---------+
现在在主机上输出二进制日志,看最后一次插入是哪个事物:
mysqlbinlog --base64-output=decode-rows -v mysql-bin.000005 > ~/bin.log
vim ~/bin.log
截取最后一部分:
### INSERT INTO `test`.`sbtest8`
### SET
### @1=289999
### @2=0
### @3=‘‘
### @4=‘qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt‘
### INSERT INTO `test`.`sbtest8`
### SET
### @1=290000
### @2=0
### @3=‘‘
### @4=‘qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt‘
# at 265373582
#180901 15:41:10 server id 1 end_log_pos 265373613 CRC32 0xa53bca62 Xid = 7014
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*/;
可以看到在主库上最后一次插入的操作是在test库下的sbtest8表,第一列值为290000,也就是id列。
现在切换到从库上进入sbtest8这张表,看看这条事物是否已经复制到了从库:
mysql> use test
Database changed
mysql> select * from sbtest8 where id = ‘290000‘;
+--------+---+---+----------------------------------------------------+
| id | k | c | pad |
+--------+---+---+----------------------------------------------------+
| 290000 | 0 | | qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt |
+--------+---+---+----------------------------------------------------+
1 row in set (0.00 sec)
可以看到已经有了数据,看看是不是最后一条,从库是否回滚了未提交的事物:
mysql> select * from sbtest8 where id = ‘290001‘;
Empty set (0.00 sec)
mysql> select * from sbtest8 order by id desc limit 1;
+--------+---+---+----------------------------------------------------+
| id | k | c | pad |
+--------+---+---+----------------------------------------------------+
| 290000 | 0 | | qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt |
+--------+---+---+----------------------------------------------------+
1 row in set (0.00 sec)
mysql> \q
Bye
可以看出id=290000确实是最后一条事物,如果有未提交的事物可能已经回滚,证明主库与复制到从库的事物不会丢失。
标签:事物 min tables mysql高可用 权限 节点 uid mit tom
原文地址:https://www.cnblogs.com/magmell/p/9570743.html