In the MYSQL_HA cluster environment that uses KeepAlived to achieve high availability, MySQL is the (Master/Master) master/master synchronous replication relationship to ensure the consistency of MYSQL server data, use KeepAlived to provide virtual IP, and use KeepAlived to monitor faults. switch automatically.
The deployment environment topology is as follows:
Mysql VIP: 192.168.187.61
Master1: 192.168.187.129
Master2: 192.168.187.132
OS environment: Cent OS 6.5
Mysql version: Mysql5.5.31
One: set the configuration file:
Mysql is replicated synchronously through the log, first create a log file
touch /var/log/mysql/mysql-bin.log //Create a log file, please set permissions, such as 777 chown mysql.mysql /var/log/mysql/mysql-bin.log //Change the user and user group of the log file to mysql
The my.cnf files on the two mysql servers to be backed up are configured as follows (add the following configurations to the my.cnf of the relevant server respectively, and put them on the [mysqld_safe] of the configuration file):
Master1(192.168.187.129) |
Master(192.168.187.132) |
#Main standard service identification number, must be unique server-id = 1 #Because MYSQL is based on binary logs for synchronization, the size of each log file is 1G log-bin=/var/log/mysql/mysql-bin.log #The name of the library to be synchronized binlog-do-db = test #Libraries that do not record logs, that is, libraries that do not require synchronization binlog-ignore-db=mysql #Use the log function on the slave server log-slave-updates #After 1 log write operation, write the log file to the hard disk once (synchronize the log information once). n=1 is the safest practice, but the least efficient. The default setting is n=0. sync_binlog=1 # auto_increment, controls the behavior of auto-increment column AUTO_INCREMENT Used for replication between MASTER-MASTER to prevent duplicate values, auto_increment_increment=n how many servers, n is set to how many, auto_increment_offset=1 sets the step size, which is set to 1 here, so that the value generated by the auto_increment field of the Master is: 1, 3, 5, 7, ... and other odd IDs auto_increment_offset=1 auto_increment_increment=2 #Database for mirroring replicate-do-db = test #Database without mirroring replicate-ignore-db= mysql |
#Main standard service identification number, must be unique server-id = 2 #Because MYSQL is based on binary logs for synchronization, the size of each log file is 1G log-bin=/var/log/mysql/mysql-bin.log #The name of the library to be synchronized binlog-do-db = test #Libraries that do not record logs, that is, libraries that do not require synchronization binlog-ignore-db=mysql #Use the log function on the slave server log-slave-updates #After 1 log write operation, write the log file to the hard disk once (synchronize the log information once). n=1 is the safest practice, but the least efficient. The default setting is n=0. sync_binlog=1 # auto_increment, controls the behavior of auto-increment column AUTO_INCREMENT Used for replication between MASTER-MASTER to prevent duplicate values, auto_increment_increment=n how many servers, n is set to how many, auto_increment_offset=2 sets the step size, which is set to 2 here, so that the value generated by the auto_increment field of the Master is: 2, 4, 6, 8, ... and other odd IDs auto_increment_offset=2 auto_increment_increment=2 #Database for mirroring replicate-do-db = test #Database without mirroring replicate-ignore-db= mysql
|
Two: Check the configuration
After configuring the two servers according to the above configuration, restart the mysql service, and use showmaster status to check the Master configuration of the two servers. It can be seen that the configuration has been successful, as follows:
NO1: The case of Master1 (192.168.187.129)
# mysql –u root –p 1234 mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001| 107 | test | mysql | +------------------+----------+--------------+------------------+
NO2:Master2(192.168.187.132)的情况
# mysql –u root –p 1234 mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000002| 125 | test | mysql | +------------------+----------+--------------+------------------+
三:建立权限帐户,实现同步
Master1(192.168.187.129) |
Master2(192.168.187.132) |
mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY '123456'; mysql> FLUSH PRIVILEGES; #在MYSQL中设置对端Master2的复制账号
mysql> change master to master_host='192.168.187.132', master_user='slave', master_password='123456', master_log_file='mysql-bin.000002', master_log_pos=125; mysql> start slave; #启动从服务器复制功能
mysql> show slave status; #检测slave状态,如果Slave_IO_Running: Yes Slave_SQL_Running: Yes Seconds_Behind_Master: 0就证明已经同步了 |
mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY '123456'; mysql> FLUSH PRIVILEGES; #在MYSQL中设置对端Master1的复制账号
mysql> change master to master_host='192.168.187.129', master_user='slave', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=107; mysql> start slave; #启动从服务器复制功能
mysql> show slave status; #检测slave状态,如果Slave_IO_Running: Yes Slave_SQL_Running: Yes Seconds_Behind_Master: 0就证明已经同步了
|
四:测试同步效果
在Master1上创建表:
mysql -uroot -p1234; use test; CREATE TABLE `card` ( `card_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID', `card_number` varchar(100) DEFAULT NULL COMMENT '卡号', PRIMARY KEY (`card_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC; insert into `card`(`card_number`) values ('haha1'); insert into `card`(`card_number`) values ('haha2');
执行完毕后,master2下如果对应的card表已经生成,并且也有相应的数据,证明同步成功,相应在master2上执行任何操作,master1也会相应修改。
五:配置keepalived实现热备
A) 安装keepalived
yum -y install keepalived
B) 修改配置
[root@masterr ~] vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MYSQL_HA #标识,双主相同 } vrrp_instance VI_1 { state BACKUP #两台都设置BACKUP interface eth0 virtual_router_id 51 #主备相同 priority 100 #优先级,backup设置90 advert_int 1 #组播信息发送间隔,两个节点设置必须一样 nopreempt #不主动抢占资源,只在master这台优先级高的设置,backup不设置 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.187.61 #指定VIP,两个节点设置必须一样 } } virtual_server 192.168.187.61 3306 { delay_loop 2 #每个2秒检查一次real_server状态 #lb_algo rr #LVS算法,用不到,我们就关闭了 #lb_kind NAT #LVS模式,如果不关闭,备用服务器不能通过VIP连接本机MySQL persistence_timeout 50 #会话保持时间 protocol TCP #使用的协议是TCP还是UDP real_server 192.168.187.132 3306 { //这里填写各自的真实内网IP地址 weight 3 #权重 notify_down /usr/local/keepalived/mysql.sh #当mysq服down时,执行此脚本,杀死keepalived实现切换 TCP_CHECK { connect_timeout 3 #连接超时 nb_get_retry 3 #重试次数 delay_before_retry 3 #重试间隔时间 } } }
C) 配置keepalived脚本
[root@masterr ~] vim /usr/local/keepalived/mysql.sh #!/bin/bash pkill keepalived [root@masterr ~]# chmod +x /usr/local/keepalived/mysql.sh [root@masterr ~]# /etc/init.d/keepalived start
六:测试高可用性
1、通过Mysql客户端通过VIP连接,看是否连接成功。 2、停止master这台mysql服务,是否能正常切换过去,可通过ip addr命令来查看VIP在哪台服务器上。 3、可通过查看/var/log/messges日志,看出主备切换过程 4、master服务器故障恢复后,是否主动抢占资源,成为活动服务器。