mysql多实例复制,Mysql实例使用Rotate Master实现MySQL 多主复制的实现方法
搜索熱詞
《MysqL實(shí)例使用Rotate Master實(shí)現(xiàn)MysqL 多主復(fù)制的實(shí)現(xiàn)方法》要點(diǎn):
本文介紹了MysqL實(shí)例使用Rotate Master實(shí)現(xiàn)MysqL 多主復(fù)制的實(shí)現(xiàn)方法,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。當(dāng)然,5.6的GUID功能的出現(xiàn)也帶來(lái)了multi-master的無(wú)限可能,不過(guò)這個(gè)已經(jīng)是題外話了.
本文主要介紹一種非實(shí)時(shí)的適用于各版本MysqL的multi-master辦法.
內(nèi)容簡(jiǎn)介:
最初的思路來(lái)源于一位國(guó)外DBA的blog : http://thenoyes.com/littlenoise/?p=117
基本原理就是通過(guò)SP記錄當(dāng)前 master-log的name和pos記錄到表中,然后讀取下一個(gè)master記錄,執(zhí)行stop slave / change master / start slave.以此循環(huán)反復(fù).
個(gè)人對(duì)他的辦法進(jìn)行了改進(jìn),增加了以下功能:
1. master可以根據(jù)業(yè)務(wù)流量設(shè)置權(quán)重值
2. 各個(gè)master-slave運(yùn)行情況的監(jiān)控
3. 各個(gè)master可以實(shí)時(shí)退出多主的架構(gòu)
具體操作過(guò)程:
1. 創(chuàng)建保存各個(gè)master信息的表
代碼如下:
use MysqL;
CREATE TABLE `rotate_master` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`master_host` varchar(255) DEFAULT NULL comment 'master地址',
`master_port` int(10) unsigned DEFAULT NULL comment 'master端口',
`master_log_file` varchar(255) DEFAULT NULL ‘上次停止時(shí)的master-log文件',
`master_log_pos` int(10) unsigned DEFAULT NULL comment '上次停止時(shí)的master-log-pos',
`IS_Slave_Running` varchar(10) DEFAULT NULL comment '上次停止時(shí)主從是否有異常',
`in_use` tinyint(1) DEFAULT '0' comment '是否是當(dāng)前正在同步的數(shù)據(jù)行',
`weight` int(11) NOT NULL DEFAULT '1' comment '該master的權(quán)重,即重復(fù)執(zhí)行多少個(gè)時(shí)間片',
`repeated_times` int(11) NOT NULL DEFAULT '0' comment '當(dāng)前已經(jīng)重復(fù)執(zhí)行的時(shí)間片數(shù)',
`LastExecuteTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '上次執(zhí)行的時(shí)間',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
新增加一個(gè)master :
代碼如下:
insert into `rotate_master`
(`master_host`,`master_port`,`master_log_file`,`master_log_pos`,`in_use`,`weight`)
values
-- 1號(hào)master,權(quán)重=1,并設(shè)置為當(dāng)前master
('192.168.0.1',3307,'MysqLbinlog.000542',4,1,1),
-- 2號(hào)master,權(quán)重=2
('192.168.0.2',3306,'MysqLbinlog.000702',64762429,2),
-- 3號(hào)master,權(quán)重=5
('192.168.0.3','MysqLbinlog.000646',22157422,5)
手工把master 調(diào)整到當(dāng)前的配置項(xiàng):
代碼如下:
change master to master_host='192.168.0.1',master_port=3306,master_log_file='MysqLbinlog.000542',master_log_pos=4,master_user='repl',master_password='repl';
start slave;
創(chuàng)建rotate master SP:
注意:代碼中用于連接master的用戶(hù)名和暗碼是 : repl / repl,請(qǐng)根據(jù)自己的情況修改.
代碼如下:
DELIMITER $$
DROP PROCEDURE IF EXISTS `MysqL`.`rotate_master`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `rotate_master`()
BEGIN
DECLARE _info text;
DECLARE _master_file varchar(255);
DECLARE _master_pos int unsigned;
DECLARE _master_host varchar(255);
DECLARE _master_port int unsigned;
DECLARE _is_slave_running varchar(10);
DECLARE _id int;
DECLARE _weight int;
DECLARE _repeated_times int;
select VARIABLE_VALUE from information_schema.GLOBAL_STATUS where VARIABLE_NAME = 'Slave_running' into _is_slave_running;
STOP SLAVE;
SELECT LOAD_FILE(@@relay_log_info_file) INTO _info;
SELECT
SUBSTRING_INDEX(SUBSTRING_INDEX(_info,'\n',3),-1),
SUBSTRING_INDEX(SUBSTRING_INDEX(_info,4),-1)
INTO _master_file,_master_pos;
UPDATE MysqL.rotate_master SET `master_log_file` = _master_file,`master_log_pos` = _master_pos,id = LAST_INSERT_ID(id),`IS_Slave_Running` = _is_slave_running WHERE in_use = 1;
select weight,repeated_times into _weight,_repeated_times from MysqL.rotate_master where in_use =1;
if(_weight <= _repeated_times)
THEN
SELECT
id,
master_host,
master_port,
master_log_file,
master_log_pos
INTO _id,_master_host,_master_port,_master_file,_master_pos
FROM rotate_master
ORDER BY id <= LAST_INSERT_ID(),id LIMIT 1;
SET @sql := CONCAT(
'CHANGE MASTER TO master_host=',QUOTE(_master_host),
',master_port=',master_log_file=',QUOTE(_master_file),master_log_pos=',_master_pos
',master_user="repl"',master_password="repl"'
);
PREPARE myStmt FROM @sql;
EXECUTE myStmt;
UPDATE MysqL.rotate_master SET in_use = 0,repeated_times=0 WHERE in_use = 1;
UPDATE MysqL.rotate_master SET in_use = 1,repeated_times=1 WHERE id = _id;
ELSE
UPDATE MysqL.rotate_master SET `repeated_times`=`repeated_times`+1 WHERE in_use = 1;
END IF;
START SLAVE;
END$$
DELIMITER ;
創(chuàng)立Event:
每2分鐘運(yùn)行一次,rotate_master(),即時(shí)間片大小是2分鐘
代碼如下:
DELIMITER $$
CREATE EVENT `rotate_master` ON SCHEDULE EVERY 120 SECOND STARTS '2011-10-13 14:09:40' ON COMPLETION NOT PRESERVE ENABLE DO CALL MysqL.rotate_master()$$
DELIMITER ;
至此,多主復(fù)制已經(jīng)搭建完成.
由于時(shí)間片長(zhǎng)度是2分鐘.
Master1 在執(zhí)行 1*2 分鐘后,stop slave,然后change master to Master2;
Master2 在執(zhí)行 2*2 分鐘后,然后change master to Master3;
Master3 在執(zhí)行 5*2 分鐘后,然后change master to Master2;
并以此循環(huán)往復(fù).
如果希望把此中一個(gè)master移除多主復(fù)制,可以將他的配置項(xiàng)權(quán)重設(shè)置為0;
即: update rotate_master set weigh=0 where id = #ID# ;
編程之家PHP培訓(xùn)學(xué)院每天發(fā)布《MysqL實(shí)例使用Rotate Master實(shí)現(xiàn)MysqL 多主復(fù)制的實(shí)現(xiàn)方法》等實(shí)戰(zhàn)技能,PHP、MysqL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
總結(jié)
如果覺(jué)得編程之家網(wǎng)站內(nèi)容還不錯(cuò),歡迎將編程之家網(wǎng)站推薦給程序員好友。
本圖文內(nèi)容來(lái)源于網(wǎng)友網(wǎng)絡(luò)收集整理提供,作為學(xué)習(xí)參考使用,版權(quán)屬于原作者。
總結(jié)
以上是生活随笔為你收集整理的mysql多实例复制,Mysql实例使用Rotate Master实现MySQL 多主复制的实现方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 北斗大红袍的功效与作用、禁忌和食用方法
- 下一篇: django mysql connect