[Original] use RotateMaster to implement MySQL multi-master replication _ MySQL

Source: Internet
Author: User
[Original] use RotateMaster to implement MySQL multi-master replication bitsCN.com

Background:

As we all know, MySQL only supports one-to-many master-to-slave replication, rather than multi-master replication.

Of course, the emergence of the GUID function of 5.6 has also brought infinite possibilities for multi-master, but this is a digress.

This article mainly introduces a non-real-time multi-master method for MySQL versions.

Introduction:

The original idea comes from a foreign DBA blog: http://thenoyes.com/littlenoise? P = 117

The basic principle is to record the name and pos records of the current master-log to the table through SP, then read the next master record, and execute stop slave/change master/start slave. This cycle is repeated.

I personally improved his methods and added the following features:

1. the master can set weights based on business traffic.

2. monitor the running status of each master-slave

3. each master can exit the multi-master architecture in real time.

Procedure:

1. create a table to save information about each master.

Use mysql;
Create table 'rotate _ Master' ('id' int (11) not null AUTO_INCREMENT, 'master _ host' varchar (255) default null comment 'master address ', 'Master _ port' int (10) unsigned default null comment 'master port', 'master _ log_file 'varchar (255) DEFAULT Null' master-log file at last stop ', 'Master _ log_pos 'int (10) unsigned default null comment 'master-log-Pos' at the time of last stop, 'is _ slave_running' varchar (10) default null comment 'whether the master/slave node has an exception during the last stop ', 'in _ use' tinyint (1) DEFAULT '0' comment' whether the data row is currently being synchronized ', 'weight' int (11) not null default '1' comment' weight of the master, that is, the number of time slices that are repeatedly executed ', 'repeated _ times' int (11) not null default '0' comment' number of time slices already executed ', 'lastexecutetime' timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP comment 'time of Last Execution ', primary key ('id') ENGINE = InnoDB default charset = utf8


Add a new master:

Insert into 'rotate _ Master' ('master _ host', 'master _ port', 'master _ log_file ', 'master _ log_pos', 'in _ use ', 'weight') values -- master 1, weight = 1, and set it to the current master ('123. 168.0.1 ', 3307, 'mysqlbinlog. 000542 ', 192, 1), -- master 2, weight = 2 ('100. 168.0.2 ', 3306, 'mysqlbinlog. 000702 ', 64762429,0, 2), -- master 3, weight = 5 ('123. 168.0.3 ', 3306, 'mysqlbinlog. 000646 ', 22157422,0, 5)

Manually adjust the master to the current configuration item:

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; 

Create a rotate master SP:

Note: in the code, the username and password used to connect to the master are repl/repl. modify the password as needed.

DELIMITER $$DROP PROCEDURE IF EXISTS `mysql`.`rotate_master`$$CREATE DEFINER=`root`@`localhost` PROCEDURE `rotate_master`()BEGINDECLARE _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;SELECTSUBSTRING_INDEX(SUBSTRING_INDEX(_info, '/n', 3), '/n', -1),SUBSTRING_INDEX(SUBSTRING_INDEX(_info, '/n', 4), '/n', -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)THENSELECTid,master_host,master_port,master_log_file,master_log_posINTO _id, _master_host, _master_port, _master_file, _master_posFROM rotate_masterORDER BY id <= LAST_INSERT_ID(), id LIMIT 1;SET @sql := CONCAT('CHANGE MASTER TO master_host=', QUOTE(_master_host),', master_port=', _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;ELSEUPDATE mysql.rotate_master SET `repeated_times`=`repeated_times`+1 WHERE in_use = 1;END IF;START SLAVE;END$$DELIMITER ;

Create Event:

Run once every 2 minutes, rotate_master (), that is, the size of the time slice is 2 minutes

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 ;

Now, the multi-master replication has been set up.

Because the length of the time slice is 2 minutes.

After Master1 is executed for 1*2 minutes, stop slave and change master to Master2;

After Master2 is executed for 2*2 minutes, stop slave and change master to Master3;

After Master3 is executed for 5*2 minutes, stop slave and change master to Master2;

And this round-robin.

If you want to remove one of the master nodes from multi-master replication, you can set its configuration item weight to 0;

That is: update rotate_master set weigh = 0 where id = # ID #;

BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.