码迷,mamicode.com
首页 > 数据库 > 详细

MySQL高可用MHA集群

时间:2019-01-12 00:15:13      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:zip   自动删除   mysq   运行   serve   client   mtu   virtual   5.0   

环境准备:

  • client50做客户端。不需要运行mysql。不拷贝包。
  • mysql51-55共5台虚拟机做集群。运行mysql,mysql为初始状态。拷贝mha-soft-student包
  • host56做MHA管理主机。运行mysql。拷贝mha-soft-student包

 

主机名  作用 IP地址
client50 客户端 eth0:192.168.4.50/24
mysql51 主服务器 eth0:192.168.4.51/24
mysql52 备份服务器 eth0:192.168.4.52/24
mysql53 备份服务器 eth0:192.168.4.53/24
mysql54 从服务器 eth0:192.168.4.54/24
mysql55 从服务器 eth0:192.168.4.55/24
host56 从服务器 eth0:192.168.4.56/24

 

配置主节点

[root@mysql51 ~]# vim /etc/my.cnf

[mysqld]

log_bin=master51

server_id=51

binlog_format="mixed"

plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" //加载主库、从库模块

rpl-semi-sync-master-enabled=1

rpl-semi-sync-slave-enabled=1

relay_log_purge=0 //关闭自动删除中继日志

 

[root@mysql51 ~]# systemctl restart mysqld

 

[root@mysql51 ~]# mysql -uroot -p123qqq...A

mysql> show master status;

mysql> show global variables where variable_name="relay_log_purge"; //确认已关闭自动删除中继日志

+---------------------+--------+

| Variable_name   | Value |

+---------------------+--------+

| relay_log_purge | OFF   |

+---------------------+--------+

1 row in set (0.00 sec)

//若未关闭,则set global relay_log_purge=off;

 

mysql> grant replication slave on *.* to repluser@"%" identified by "123qqq...A"; //添加授权用户repluser

 

 

 

配置mysql52、53为mysql51的从库,并做备用主库的配置

注:52、53做相同操作

[root@mysql52 ~]# vim /etc/my.cnf

[mysqld]

log_bin=master52

server_id=52

binlog_format="mixed"

plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"

rpl-semi-sync-master-enabled=1

rpl-semi-sync-slave-enabled=1

relay_log_purge=0

 

[root@mysql52 ~]# systemctl restart mysqld

[root@mysql52 ~]# mysql -uroot -p123qqq...A

mysql> show master status;

mysql> show global variables where variable_name="relay_log_purge"; //确认关闭自动删除中继日志

mysql> grant replication slave on *.* to repluser@"%" identified by "123qqq...A";

 

//配置mysql52为mysql51的从库

mysql> change master to master_host="192.168.4.51",master_user="repluser",master_password="123qqq...A",master_log_file="master51.000001",master_log_pos=441;

mysql> start slave;

mysql> show slave status\G;

 

 

配置mysql54、55为mysql51的从库

注意:54、55做类似操作

[root@mysql54 ~]# vim /etc/my.cnf

[mysqld]

server_id=54

plugin-load="rpl_semi_sync_slave=semisync_slave.so"

rpl-semi-sync-slave-enabled=1

relay_log_purge=0

 

[root@mysql54 ~]# systemctl restart mysqld

[root@mysql54 ~]# mysql -uroot -p123qqq...A

mysql> change master to master_host="192.168.4.51",master_user="repluser",master_password="123qqq...A",master_log_file="master51.000001",master_log_pos=441;

mysql> start slave;

mysql> show slave status\G;

 

检查上述配置

在主库服务器mysql51上建个库和表,并授权一个测试用户。

mysql> create database db1;

mysql> create table db1.a(id int);

mysql> grant select,insert on db1.* to client@"%" identified by "123qqq...A";

 

在客户端用测试用户client登录主服务器mysql51,测试集群配置。

[root@client50 ~]# mysql -h192.168.4.51 -uclient -p123qqq...A

mysql> insert into db1.a values(123);

mysql> select * from db1.a;

 

配置MHA高可用

一、配置SSH

1.配置mysql51-55共5台服务器之间ssh密钥登录

mysql51为例:(其他4台同样操作)

[root@mysql51 ~]# ssh-keygen -t rsa

[root@mysql51 ~]# for i in {52..55};do ssh-copy-id 192.168.4.$i ;done

验证ssh密钥登录是否成功。

 

再到其他4台服务器做同样操作。

 

2.配置管理主机(host56)可ssh密钥登录mysql51-55

操作同步骤1

 

二、装包

3.安装perl相关包(51-56主机)

# yum -y install perl-* //204个,约12min

 

4.安装MHA依赖包(51-56主机)

# unzip mha-soft-student.zip -d /root/

# cd /root/mha-soft-student/

# yum install -y prel-*.rpm //8个

 

5.安装mha_node软件包(51-56主机)

# cd /root/mha-soft-student/

# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm

 

6.安装mha-manger软件包(56主机),并建立管理命令

[root@host56 ~]# cd /root/mha-soft-student/

# tar -xf mha4mysql-manager-0.56.tar.gz

# cd mha4mysql-manager-0.56/

# perl Makefile.PL

# make

# make installcd

 

[root@host56 ~]# ls /usr/local/bin/masterha_*   //查看是否有管理命令

若没有则:[root@host56 ~]# cp -r /root/mha-soft-student/mha4mysql-manager-0.56/bin/   /root/

 

三、配置

7.创建集群配置文件(56主机)

[root@host56 ~]# mkdir /etc/mha/

[root@host56 ~]# cp /root/mha-soft-student/mha4mysql-manager-0.56/samples/conf/app1.cnf /etc/mha/

 

[root@host56 ~]# vim /etc/mha/app1.cnf 

[server default]

manager_workdir=/etc/mha

manager_log=/etc/mha/manager.log

master_ip_failover_script=/etc/mha/master_ip_failover

ssh_user=root

ssh_port=22

repl_user=repluser

repl_password=123qqq...A

user=root

password=123qqq...A

[server1]

hostname=192.168.4.51

port=3306

candidate_master=1

[server2]

hostname=192.168.4.52

port=3306

candidate_master=1

[server3]

hostname=192.168.4.53

port=3306

candidate_master=1

[server4]

hostname=192.168.4.54

port=3306

no_master=1

[server5]

hostname=192.168.4.55

port=3306

no_master=1

 

8. 创建vip地址切换脚本

从真机资料拷贝:

[root@room9pc01 ~]# scp ‘/root/桌面/nsd1808笔记1229/software-2018/09.dba2/mha-soft-student/master_ip_failover‘ 192.168.4.56:/etc/mha/

[root@host56 ~]# vim /etc/mha/master_ip_failover

:35

my $vip = ‘192.168.4.100/24‘; # Virtual IP

[root@host56 ~]# chmod +x /etc/mha/master_ip_failover

 

9.在竞选主库服务器(51,52,53)上添加授权用户

[root@mysql51 ~]# mysql -uroot -p123qqq...A //在mysql51上操作

mysql> grant all on *.* to root@"%" identified by "123qqq...A";

 

10.把vip地址 绑定在当前的主库服务器51的 eth0接口

[root@mysql51 ~]# ifconfig eth0:1 192.168.4.100

[root@mysql51 ~]# ifconfig eth0:1

eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.4.100 netmask 255.255.255.0 broadcast 192.168.4.255

ether 52:54:00:2f:7b:4f txqueuelen 1000 (Ethernet)

 

四、测试配置

11.在管理主机56 上测试配置

[root@host56 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf

......

Fri Jan 11 19:33:26 2019 - [info] All SSH connection tests passed successfully.

 

12.测试主从同步配置

[root@host56 ~]# masterha_check_repl --conf=/etc/mha/app1.cnf

......

MySQL Replication Health is OK.

 

五、启动服务

13.启动管理服务

[root@host56 ~]# masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover

 

新开一个终端查看状态:

[root@host56 ~]# masterha_check_status --conf=/etc/mha/app1.cnf

app1 (pid:9748) is running(0:PING_OK), master:192.168.4.51

至此MHA高可用搭建完成!

停止服务的命令:# masterha_stop --conf=/etc/mha/app1.cnf

 

 

测试

客户端测试:

[root@client50 ~]# mysql -h192.168.4.51 -uclient -p123qqq...A

mysql> select @@hostname; //查看登录的服务器

+------------+

| @@hostname |

+------------+

| mysql51 |

+------------+

1 row in set (0.00 sec)

mysql> select * from db1.a;

+------+

| id |

+------+

| 123 |

+------+

1 row in set (0.00 sec)

mysql> insert into db1.a values(666);

Query OK, 1 row affected (0.11 sec)

 

模拟主服务器mysql51宕机:

[root@mysql51 ~]# systemctl stop mysqld

 

可看到masterha_manager检测到了mysql51故障,并做故障切换,然后masterha_manager服务将自动退出。

[root@host56 ~]# masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover

Fri Jan 11 19:36:11 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.

Fri Jan 11 19:36:11 2019 - [info] Reading application default configuration from /etc/mha/app1.cnf..

Fri Jan 11 19:36:11 2019 - [info] Reading server configuration from /etc/mha/app1.cnf..

Creating /var/tmp if not exists.. ok.

Checking output directory is accessible or not..

ok.

Binlog found at /var/lib/mysql, up to master51.000001

Fri Jan 11 19:53:56 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.

Fri Jan 11 19:53:56 2019 - [info] Reading application default configuration from /etc/mha/app1.cnf..

Fri Jan 11 19:53:56 2019 - [info] Reading server configuration from /etc/mha/app1.cnf..

[root@host56 ~]#

[root@host56 ~]# masterha_check_status --conf=/etc/mha/app1.cnf

app1 is stopped(2:NOT_RUNNING).

 

[root@client50 ~]# ping 192.168.4.100 //VIP依然可ping通

PING 192.168.4.100 (192.168.4.100) 56(84) bytes of data.

64 bytes from 192.168.4.100: icmp_seq=1 ttl=64 time=0.620 ms

64 bytes from 192.168.4.100: icmp_seq=2 ttl=64 time=0.571 ms

64 bytes from 192.168.4.100: icmp_seq=3 ttl=64 time=0.610 ms

64 bytes from 192.168.4.100: icmp_seq=4 ttl=64 time=0.535 ms

^C

--- 192.168.4.100 ping statistics ---

4 packets transmitted, 4 received, 0% packet loss, time 3000ms

rtt min/avg/max/mdev = 0.535/0.584/0.620/0.033 ms

[root@client50 ~]# mysql -h192.168.4.100 -uclient -p123qqq...A

mysql> select @@hostname; //mysql52自动切换成为主服务器

+-------------------+

| @@hostname |

+-------------------+

| mysql52 |

+-------------------+

1 row in set (0.01 sec)

mysql> insert into db1.a values(888);

Query OK, 1 row affected (0.07 sec)

 

mysql> select * from db1.a;

+------+

| id |

+------+

| 123 |

| 666 |

| 888 |

+------+

3 rows in set (0.00 sec)

 

[root@mysql52 ~]# ifconfig eth0:1 //VIP到了mysql52的eth0:1上

eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.4.100 netmask 255.255.255.0 broadcast 192.168.4.255

ether 52:54:00:46:8a:53 txqueuelen 1000 (Ethernet)

 

[root@host56 ~]# cat /etc/mha/app1.cnf //可看到配置文件中server1的配置已被自动清除

[server default]

manager_log=/etc/mha/manager.log

manager_workdir=/etc/mha

master_ip_failover_script=/etc/mha/master_ip_failover

password=123qqq...A

repl_password=123qqq...A

repl_user=repluser

ssh_port=22

ssh_user=root

user=root

 

[server2]

candidate_master=1

hostname=192.168.4.52

port=3306

 

[server3]

candidate_master=1

hostname=192.168.4.53

port=3306

 

[server4]

hostname=192.168.4.54

no_master=1

port=3306

 

[server5]

hostname=192.168.4.55

no_master=1

port=3306

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

mysql51再加回集群中:

  1. 手动启动mysqld
  2. 恢复数据
  3. 配置为mysql52的从库
  4. 在管理主机masterha_manager的配置文件添加mysql51相关配置
  5. 重新开启MHA

[root@mysql52 ~]# mysqldump -uroot -p123qqq...A db1 > /root/db1.sql

mysqldump: [Warning] Using a password on the command line interface can be insecure.

[root@mysql52 ~]# scp /root/db1.sql 192.168.4.51:/root/

db1.sql 100% 1783 1.9MB/s 00:00

[root@mysql52 ~]#

 

[root@mysql51 ~]# systemctl start mysqld

[root@mysql51 ~]# mysql -uroot -p123qqq...A db1 < /root/db1.sql

mysql: [Warning] Using a password on the command line interface can be insecure.

[root@mysql51 ~]# mysql -uroot -p123qqq...A

mysql> select * from db1.a;

mysql> change master to master_host="192.168.4.52",master_user="repluser",

-> master_password="123qqq...A",master_log_file="master52.000001",master_log_pos=1248;

mysql> start slave;

mysql> show slave status\G;

 

[root@host56 ~]# vim /etc/mha/app1.cnf

添加mysql51的配置:

[server1]

candidate_master=1

hostname=192.168.4.51

port=3306

 

[root@host56 ~]# masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover

 

MySQL高可用MHA集群

标签:zip   自动删除   mysq   运行   serve   client   mtu   virtual   5.0   

原文地址:https://www.cnblogs.com/iouwenbo/p/10258229.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!