Centos7 Mysql 集群MHA--之一(MYSQL5.7双主配置)

  1. 统一设置hosts
[root@elk-node2 ~]#vi /etc/hosts
---------------------------------------------------------------------------------------------------------
192.168.40.171  MYSQLM1  MYSQLM1.COM
192.168.40.172  MYSQLS1  MYSQLS1.COM
192.168.40.173  MYSQLS2  MYSQLS2.COM
192.168.40.174  MYSQLMHA MYSQLMHA.COM
---------------------------------------------------------------------------------------------------------
[root@elk-node2 ~]# vim /etc/sysconfig/network
[root@elk-node2 ~]# vim /etc/hostname
  1. 配置SSH无密码登录认证
(1)、在MYSQLMHA(192.168.40.174)上
ssh-keygen -t rsa
#一路回车,直到完成
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

(2)、在MYSQLM1(192.168.40.171)上
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

(3)、在MYSQLS1(192.168.40.172)上
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

(4)、在MYSQLS2(192.168.40.173)上
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

--test
ssh MYSQLM1
  1. 时间同步
查看时间是否同步:
shell> ssh MYSQLM1 date; ssh MYSQLS1 date;ssh MYSQLS2 date;ssh MYSQLMHA date;

发现时间不同步,进行时间同步,依据当前时间为准
shell>ssh MYSQLM1 "date -s '2018-04-07 00:20:00'"; ssh MYSQLS1 "date -s '2018-04-07 00:20:00'"; ssh MYSQLS2 "date -s '2018-04-07 00:20:00'";
  1. 修改配置(4台都要做)
shell> vim /etc/security/limits.conf
* soft nproc 65535 
* hard nproc 65535 
* soft nofile 65535 
* hard nofile 65535

shell> echo "fs.file-max=65535" >> /etc/sysctl.conf

[root@MYSQL src]# mkdir /usr/local/mysql/binlog
[root@MYSQL src]# mkdir /usr/local/mysql/iblog
[root@MYSQL src]# chown mysql:mysql /usr/local/mysql/binlog
[root@MYSQL src]# chown mysql:mysql /usr/local/mysql/iblog
[root@MYSQL src]# chmod -R 755 /usr/local/mysql
  1. 在各个节点安装半同步插件
在192.168.40.171、172、173上:
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so'; 
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
mysql> set global rpl_semi_sync_master_enabled=on;  
  1. 修改 /etc/my.cnf 配置
#修改server_id=ip尾数,增加如下:
read_only=1                                 #S1、S2上配置
#binlog
log_bin=/usr/local/mysql/binlog/binlog      #开启二进制日志
log-slave-updates=1                         # 做为从库时,数据库的修改也会写到bin-log里
#开启半同步复制  否则自动切换主从的时候会报主键错误
#plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
loose_rpl_semi_sync_master_enabled = 1
loose_rpl_semi_sync_slave_enabled = 1
loose_rpl_semi_sync_master_timeout = 500
relay-log = mysql-relay-bin
#replicate-wild-ignore-table=mysql.%
#replicate-wild-ignore-table=test.%
#replicate-wild-ignore-table=information_schema.%
replicate-do-db=test                #要同步的数据库,默认所有库
  1. 搭建主从半同步复制
#说明:下面的搭建方法是比较通用的方法(本例中三个节点都是新的数据库,可以不用迁移数据)。
1、在192.168.40.171上创建复制用户
grant replication slave on *.* to 'repl'@'192.168.40.%' identified by '123456';

2、在192.168.40.171上执行备份
[root@MYSQL src]# mysqldump -uroot -p --master-data=2 --single-transaction --default-character-set=utf8 -R --triggers -A >all2.sql

3、查看上备份时刻Binlog的名称和位置,MASTER_LOG_FILE 和 MASTER_LOG_FILE
[root@panda001 opt]# head -n 30 all2.sql|grep -i "CHANGE MASTER TO"
-- CHANGE MASTER TO MASTER_LOG_FILE='binlog.000001', MASTER_LOG_POS=716;

4、将all.sql拷贝到 192.168.40.172 和 192.168.40.173机器上
scp all2.sql [email protected]:/home
scp all2.sql [email protected]:/home

5、在192.168.40.172上搭建备库
root@panda002 home]# mysql -uroot -p change master to master_host='192.168.40.171',master_user='repl',master_password='123456',master_port=3306,master_log_file='mysql-bin.000001',master_log_pos=716;

6、在192.168.40.173上搭建备库
root@panda002 home]# mysql -uroot -p change master to master_host='192.168.40.171',master_user='repl',master_password='123456',master_port=3306,master_log_file='mysql-bin.000001',master_log_pos=716;
  1. 启动主从复制
#在192.168.40.172上:
mysql> start slave;
mysql> set global read_only=1;

#在192.168.40.173上:
mysql> start slave;
mysql> set global read_only=1;

#查看S1、S2
mysql> show slave status\G
----------------------------------------------------------------------------------------------
        Slave_IO_Running: No
        Slave_SQL_Running: Yes
        Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
----------------------------------------------------------------------------------------------
#在库MYSQLS1及MYSQLS1上执行(相同则则修改b库id,找到data文件夹下的auto.cnf文件,修改里面的uuid值,保证各个db的uuid不一样,重启db即可)
mysql> show variables like '%server_uuid%';

#在各库mysqla及mysqlb上执行,都为yes则ok
mysql> show slave status\G
----------------------------------------------------------------------------------------------
        Slave_IO_Running: Yes
        Slave_SQL_Running: Yes
----------------------------------------------------------------------------------------------
  1. 各库my.cnf 参考171
[mysql]
default-character-set=utf8
prompt=\\u@\\h : \\d【\\r:\\m:\\s】\\c SQL->
[mysqld]
server_id=171
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 
# 一般配置选项
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port=3306
socket=/usr/local/mysql/mysql.sock
default-storage-engine=INNODB
character_set_server=utf8
log_error=/usr/local/mysql/logs/error.log
#read_only=1                                 #S1、S2上配置
#binlog
log_bin=/usr/local/mysql/binlog/binlog      #开启二进制日志
log-slave-updates=1                         # 做为从库时,数据库的修改也会写到bin-log里
#开启半同步复制  否则自动切换主从的时候会报主键错误
#plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
loose_rpl_semi_sync_master_enabled = 1
loose_rpl_semi_sync_slave_enabled = 1
loose_rpl_semi_sync_master_timeout = 500
relay-log = mysql-relay-bin
#replicate-wild-ignore-table=mysql.%
#replicate-wild-ignore-table=test.%
#replicate-wild-ignore-table=information_schema.%
replicate-do-db=test                #要同步的数据库,默认所有库
  1. 各库my.cnf 参考172
[mysql]
default-character-set=utf8
prompt=\\u@\\h : \\d【\\r:\\m:\\s】\\c SQL->
[mysqld]
server_id=172
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 
# 一般配置选项
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port=3306
socket=/usr/local/mysql/mysql.sock
default-storage-engine=INNODB
character_set_server=utf8
log_error=/usr/local/mysql/logs/error.log
read_only=1                                 #S1、S2上配置
#binlog
log_bin=/usr/local/mysql/binlog/binlog
log-slave-updates=1
#开启半同步复制  否则自动切换主从的时候会报主键错误
#plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
loose_rpl_semi_sync_master_enabled = 1
loose_rpl_semi_sync_slave_enabled = 1
loose_rpl_semi_sync_master_timeout = 500
relay-log = mysql-relay-bin
#replicate-wild-ignore-table=mysql.%
#replicate-wild-ignore-table=test.%
#replicate-wild-ignore-table=information_schema.%
replicate-do-db=test                #要同步的数据库,默认所有库
  1. 各库my.cnf 参考173
[mysql]
default-character-set=utf8
prompt=\\u@\\h : \\d【\\r:\\m:\\s】\\c SQL->
[mysqld]
server_id=173
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 
# 一般配置选项
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port=3306
socket=/usr/local/mysql/mysql.sock
default-storage-engine=INNODB
character_set_server=utf8
log_error=/usr/local/mysql/logs/error.log
read_only=1                                 #S1、S2上配置
#binlog
log_bin=/usr/local/mysql/binlog/binlog
log-slave-updates=1
#开启半同步复制  否则自动切换主从的时候会报主键错误
#plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
loose_rpl_semi_sync_master_enabled = 1
loose_rpl_semi_sync_slave_enabled = 1
loose_rpl_semi_sync_master_timeout = 500
relay-log = mysql-relay-bin
#replicate-wild-ignore-table=mysql.%
#replicate-wild-ignore-table=test.%
#replicate-wild-ignore-table=information_schema.%
replicate-do-db=test                #要同步的数据库,默认所有库

你可能感兴趣的:(Centos7 Mysql 集群MHA--之一(MYSQL5.7双主配置))