这篇文章主要介绍了CentOS7如何搭建MySQL5.7高可用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
创新互联是一家专业从事成都网站建设、成都网站制作的网络公司。作为专业网络公司,创新互联依托的技术实力、以及多年的网站运营经验,为您提供专业的成都网站建设、网络营销推广及网站设计开发服务!数据库架构:一主两从
master:192.168.8.57
slave1:192.168.8.58
slave2:192.168.8.59
manager:192.168.8.60
MHA工具包:
mha4mysql-manager-0.58.tar.gz
mha4mysql-node-0.58.tar.gz
Manager工具包主要包括以下几个工具:
masterha_check_ssh 检查MHA的SSH配置状况
masterha_check_repl 检查MySQL复制状况
masterha_manger 启动MHA
masterha_check_status 检测当前MHA运行状态
masterha_master_monitor 检测master是否宕机
masterha_master_switch 控制故障转移(自动或者手动)
masterha_conf_host 添加或删除配置的服务器信息
Node工具包(这些工具通常由MHA Manager的脚本触发,无需人为操作)主要包括以下几个工具:
save_binary_logs 保存和复制master的二进制日志
apply_diff_relay_logs 识别差异的中继日志事件并将其差异的事件应用于其他的slave
filter_mysqlbinlog 去除不必要的ROLLBACK事件(MHA已不再使用这个工具)
purge_relay_logs 清除中继日志(不会阻塞SQL线程)
一、配置MySQL5.7
1.在配置文件添加,三台服务器一样,只需修改server-id和log-bin。
注意:binlog-do-db 和 replicate-ignore-db 设置必须相同。 MHA 在启动时候会检测过滤规则,如果过滤规则不同,MHA 不启动监控和故障转移,这里没有设置。
server-id=57 log-bin=mysql-bin gtid_mode = on #开启gtid,必须主从全开 enforce_gtid_consistency = 1 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 = 5000
2.启动服务器,配置主从 (这里简写,百度很多)
MySQL5.7会默认加载validate_password 模块,是来控制密码长度和规则的,可以在配置文件里面关闭该模块 加上validate_password = off ,或者在mysql命令行执行set global validate_password_policy=0;来临时取消密码规则。
在三台服务器上配置复制用户和监控用户,三台服务器都要添加。
添加复制用户
grant replication slave on *.* to 'repl'@'192.168.8.%' identified by 'mysql';
这里注意一定要用repl用户,否则后边会报找不到复制用户
加监控用户
grant all privileges on *.* to 'root'@'192.168.8.%' identified by 'mysql';
在slave1和slave2上执行
change master to master_host='192.168.8.57', master_port=3306, master_user='repl', master_password='mysql', master_auto_position=1;
设置从服务器只读,不要在配置文件里写,重点!
set global read_only=1
二、配置MHA
1.安装依赖包(所有节点)
yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN
2.安装 manager
tar xf mha4mysql-manager-0.58.tar.gz cd mha4mysql-manager-0.58 perl Makefile.PL make && make install
安装node (四台服务器都安装)
tar xf mha4mysql-node-0.58.tar.gz cd mha4mysql-node-0.58 perl Makefile.PL make && make install
安装完成后会在/usr/local/bin目录下面生成相应的脚本,复制相关脚本到/usr/local/bin目录(manager 服务器执行)
cp -ra /root/mha4mysql-manager-0.57/samples/scripts/* /usr/local/bin
master_ip_failover
#自动切换时vip管理的脚本,不是必须,如果我们使用keepalived的,我们可以自己编写脚本完成对vip的管理,比如监控mysql,如果mysql异常,我们停止keepalived就行,这样vip就会自动漂移
master_ip_online_change
#在线切换时vip的管理,不是必须,同样可以可以自行编写简单的shell完成
power_manager
#故障发生后关闭主机的脚本,不是必须
send_report
#因故障切换后发送报警的脚本,不是必须,可自行编写简单的shell完成。
3.配置SSH登录无密码验证
master
ssh-keygen -t rsa ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.58 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.59 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.60
slave1
ssh-keygen -t rsa ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.57 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.59 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.60
slave2
ssh-keygen -t rsa ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.57 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.58 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.60
manager
ssh-keygen -t rsa ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.57 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.58 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.59
4.配置MHA
修改/usr/local/bin/master_ip_failover,这里使用脚本管理vip
将如下代码全部复制进去,根据自己的实际情况进行修改
#!/usr/bin/env perl # Copyright (C) 2011 DeNA Co.,Ltd. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ## Note: This is a sample script and is not complete. Modify the script based on your environment. use strict; use warnings FATAL => 'all'; use Getopt::Long; use MHA::DBHelper; my ( $command, $ssh_user, $orig_master_host, $orig_master_ip, $orig_master_port, $new_master_host, $new_master_ip, $new_master_port, $new_master_user, $new_master_password ); my $vip = '192.168.8.88/24'; my $key = '1'; my $ssh_start_vip = "/sbin/ifconfig enp0s3:$key $vip"; my $ssh_stop_vip = "/sbin/ifconfig enp0s3:$key down"; GetOptions( 'command=s' => \$command, 'ssh_user=s' => \$ssh_user, 'orig_master_host=s' => \$orig_master_host, 'orig_master_ip=s' => \$orig_master_ip, 'orig_master_port=i' => \$orig_master_port, 'new_master_host=s' => \$new_master_host, 'new_master_ip=s' => \$new_master_ip, 'new_master_port=i' => \$new_master_port, 'new_master_user=s' => \$new_master_user, 'new_master_password=s' => \$new_master_password, ); exit &main(); sub main { if ( $command eq "stop" || $command eq "stopssh" ) { # $orig_master_host, $orig_master_ip, $orig_master_port are passed. # If you manage master ip address at global catalog database, # invalidate orig_master_ip here. my $exit_code = 1; eval { # updating global catalog, etc $exit_code = 0; }; if ($@) { warn "Got Error: $@\n"; exit $exit_code; } exit $exit_code; } elsif ( $command eq "start" ) { # all arguments are passed. # If you manage master ip address at global catalog database, # activate new_master_ip here. # You can also grant write access (create user, set read_only=0, etc) here. my $exit_code = 10; eval { my $new_master_handler = new MHA::DBHelper(); # args: hostname, port, user, password, raise_error_or_not $new_master_handler->connect( $new_master_ip, $new_master_port, $new_master_user, $new_master_password, 1 ); ## Set read_only=0 on the new master $new_master_handler->disable_log_bin_local(); print "Set read_only=0 on the new master.\n"; $new_master_handler->disable_read_only(); ## Creating an app user on the new master print "Creating app user on the new master..\n"; FIXME_xxx_create_user( $new_master_handler->{dbh} ); $new_master_handler->enable_log_bin_local(); $new_master_handler->disconnect(); ## Update master ip on the catalog database, etc # FIXME_xxx; $exit_code = 0; }; if ($@) { warn $@; # If you want to continue failover, exit 10. exit $exit_code; } exit $exit_code; } elsif ( $command eq "status" ) { # do nothing exit 0; } else { &usage(); exit 1; } } sub usage { print "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n"; }
5.修改MHA配置文件
mkdir -p /etc/masterha cp mha4mysql-manager-0.58/samples/conf/app1.cnf /etc/masterha/
[server default] manager_workdir=/var/log/masterha/app1 manager_log=/var/log/masterha/app1/manager.log master_binlog_dir=/mysql/data master_ip_failover_script= /usr/local/bin/master_ip_failover master_ip_online_change_script= /usr/local/bin/master_ip_online_change password=mysql user=root ping_interval=1 remote_workdir=/tmp repl_password=mysql repl_user=repl report_script=/usr/local/bin/send_report secondary_check_script= /usr/local/bin/masterha_secondary_check -s 192.168.8.58 -s 192.168.8.59 shutdown_script="" ssh_user=root [server1] hostname=192.168.8.57 port=3306 [server2] hostname=192.168.8.58 port=3306 candidate_master=1 check_repl_delay=0 [server3] hostname=192.168.8.59 port=3306
6.测试MHA
测试ssh的连接情况
masterha_check_ssh –conf=/etc/masterha/app1.cnf
测试mysq集群的连接情况
masterha_check_repl –conf=/etc/masterha/app1.cnf
检查MHA的状态
masterha_check_status --conf=/etc/masterha/app1.cnf
启动MHA
nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &
–remove_dead_master_conf 该参数代表当发生主从切换后,老的主库的ip将会从配置文件中移除。
–manger_log 日志存放位置
–ignore_last_failover 在缺省情况下,如果MHA检测到连续发生宕机,且两次宕机间隔不足8小时的话,则不会进行Failover,之所以这样限制是为了避免ping-pong效应。该参数代表忽略上次MHA触发切换产生的文件,默认情况下,MHA发生切换后会在日志目录,也就是上面我设置的/data产生app1.failover.complete文件,下次再次切换的时候如果发现该目录下存在该文件将不允许触发切换,除非在第一次切换后收到删除该文件,为了方便,这里设置为–ignore_last_failover。
为了方便启停MHA,创建以下脚本
cat masterha_start.sh nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 & cat masterha_stop.sh masterha_stop --conf=/etc/masterha/app1.cnf
7.检查MHA的启动状态
tail -f /var/log/masterha/app1/manager.log
如果最后一行是
[info] Ping(SELECT) succeeded, waiting until MySQL doesn’t respond..
表明启动成功
8.master添加vip
在master上执行
/sbin/ifconfig enp0s3:1 192.168.8.88/24
[ifconfig enp0s3: flags=4163mtu 1500 inet 192.168.8.57 netmask 255.255.255.0 broadcast 192.168.8.255 inet6 fe80::5198:593b:cdc5:1f90 prefixlen 64 scopeid 0x20 ether 08:00:27:c0:45:0d txqueuelen 1000 (Ethernet) RX packets 72386 bytes 9442794 (9.0 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 24221 bytes 2963104 (2.8 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 enp0s3:1: flags=4163 mtu 1500 inet 192.168.8.88 netmask 255.255.255.0 broadcast 192.168.8.255 ether 08:00:27:c0:45:0d txqueuelen 1000 (Ethernet) lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 84 bytes 9492 (9.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 84 bytes 9492 (9.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 virbr0: flags=4099 mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:f4:55:bb txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
感谢你能够认真阅读完这篇文章,希望小编分享的“CentOS7如何搭建MySQL5.7高可用”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联-成都网站建设公司行业资讯频道,更多相关知识等着你来学习!