标签:不记录 name 命令 format 必须 ifconf man 健康 line
节点
|
系统版本
|
MySQL版本
|
业务IP
|
心跳IP
|
Master
|
CentOS 7.5
|
MySQL 5.6
|
192.168.88.100
|
192.168.77.100
|
Slave
|
CentOS 7.5
|
MySQL 5.6
|
192.168.88.101
|
192.168.77.101
|
VIP
|
192.168.88.88
|
1 [root@master ~]# yum list installed | grep mysql #查看是否存在其他MySQL组件 2 [root@master ~]# yum -y remove mysql* #为避免冲突引,卸载已存在的组件 3 [root@master ~]# yum -y install mariadb mariadb-server 4 [root@master ~]# systemctl start mariadb.service
1 [root@master ~]# mysql_secure_installation #设置root密码 2 [root@master ~]# systemctl restart mariadb.service
1 [root@master ~]# ll /usr/lib64/mysql/plugin/ #查看插件 2 [root@master ~]# mysql -uroot -p 3 Enter password: 4 MariaDB [(none)]> install plugin rpl_semi_sync_master soname ‘semisync_master.so‘; 5 MariaDB [(none)]> install plugin rpl_semi_sync_slave soname ‘semisync_slave.so‘;
1 MariaDB [(none)]> select * from information_schema.plugins where plugin_name like ‘%semi%‘\G #查看加载情况
1 [root@master ~]# vi /etc/my.cnf 2 [mysqld] 3 …… 4 server-id=1 #设置主服务器master的id 5 log-bin=mysql-bin #配置二进制变更日志命名格式 6 plugin-load=rpl_semi_sync_master=semisync_master.so 7 rpl_semi_sync_master_enabled=1 8 rpl_semi_sync_master_timeout=1000
1 [root@slave ~]# vi /etc/my.cnf 2 [mysqld] 3 …… 4 server-id=2 #设置本服务器slave的id 5 log-bin=mysql-bin #配置二进制变更日志命名格式 6 plugin-load=rpl_semi_sync_master=semisync_master.so 7 rpl_semi_sync_master_enabled=1 8 9 read-only #设为只读模式,只能从master同步,不能直接写入
1 [root@master ~] mysql -uroot -p 2 Enter password: 3 MariaDB [(none)]> grant replication slave on *.* to ‘repl_user‘@‘192.168.88.101‘ identified by ‘x12345678‘; 4 MariaDB [(none)]> grant replication slave on *.* to ‘repl_user‘@‘slave.yewu.com‘ identified by ‘x12345678‘; 5 MariaDB [(none)]> grant all privileges on *.* to ‘root‘@‘192.168.88.%‘ identified by ‘x120952576‘ with grant option; 6 MariaDB [(none)]> grant all privileges on *.* to ‘root‘@‘slave.yewu.com‘ identified by ‘x120952576‘; 7 MariaDB [(none)]> flush privileges; 8 MariaDB [(none)]> select host, user from mysql.user; #查看权限
1 [root@master ~]# systemctl restart mariadb.service 2 [root@master ~]# mysql -uroot -p 3 Enter password: 4 MariaDB [(none)]> show master status;
1 [root@slave ~] mysql -uroot -p 2 Enter password: 3 MariaDB [(none)]> grant replication slave on *.* to ‘repl_user‘@‘192.168.88.100‘ identified by ‘x12345678‘; 4 MariaDB [(none)]> grant replication slave on *.* to ‘repl_user‘@‘master.yewu.com‘ identified by ‘x12345678‘; 5 MariaDB [(none)]> grant all privileges on *.* to ‘root‘@‘192.168.88.%‘ identified by ‘x120952576‘ with grant option; 6 MariaDB [(none)]> grant all privileges on *.* to ‘root‘@‘master.yewu.com‘ identified by ‘x120952576‘ with grant option; 7 MariaDB [(none)]> select host, user from mysql.user; #查看权限
1 [root@master ~]# systemctl restart mariadb.service 2 [root@master ~]# mysql -uroot -p 3 Enter password: 4 MariaDB [(none)]> show master status;
1 [root@slave ~]# systemctl restart mariadb.service 2 [root@slave ~]# mysql -uroot -p 3 Enter password: 4 MariaDB [(none)]> change master to master_host=‘192.168.88.100‘, 5 master_user=‘repl_user‘, 6 master_password=‘x12345678‘, 7 master_log_file=‘mysql-bin.000001‘, 8 master_port=3306, 9 master_log_pos=245; 10 MariaDB [(none)]> start slave; 11 MariaDB [(none)]> show slave status\G #查看slave状态
1 [root@master ~]# wget http://www.haproxy.org/download/1.9/src/haproxy-1.9.0.tar.gz 2 [root@master ~]# tar -zxvf haproxy-1.9.0.tar.gz 3 [root@master ~]# cd haproxy-1.9.0/ 4 [root@master haproxy-1.9.0]# make TARGET=linux3100 CPU=x86_64 PREFIX=/usr/local/haprpxy 5 #编译uname -r #查看系统内核版本号 6 [root@master haproxy-1.9.0]# make install PREFIX=/usr/local/haproxy
1 [root@master ~]# mkdir /usr/local/haproxy/conf #创建配置文件目录 2 [root@master ~]# mkdir -p /etc/haproxy #创建配置文件目录 3 [root@master ~]# touch /usr/local/haproxy/haproxy.cfg #创建配置文件 4 [root@master ~]# ln -s /usr/local/haproxy/conf/haproxy.cfg /etc/haproxy/haproxy.cfg #添加配置文件软连接 5 [root@master ~]# cp -r /root/haproxy-1.9.0/examples/errorfiles /usr/local/haproxy/errorfiles #拷贝错误页面 6 [root@master ~]# ln -s /usr/local/haproxy/errorfiles /etc/haproxy/errorfiles 7 #添加软连接 8 [root@master ~]# mkdir -p /usr/local/haproxy/log #创建日志文件目录 9 [root@master ~]# touch /usr/local/haproxy/log/haproxy.log #创建日志文件目录 10 [root@master ~]# ln -s /usr/local/haproxy/log/haproxy.log /var/log/haproxy.log 11 #添加软连接 12 [root@master ~]# cp /usr/local/haproxy/sbin/haproxy /usr/sbin/ #拷贝HAProxy命令 13 [root@master ~]# cp /root/haproxy-1.7.9/examples/haproxy.init /etc/rc.d/init.d/haproxy 14 #拷贝开机启动文件 15 [root@master ~]# chmod u+x /etc/rc.d/init.d/haproxy #添加脚本执行权限 16 [root@master ~]# chkconfig haproxy on #设置开机启动
1 [root@master ~]# vi /etc/haproxy/haproxy.cfg 2 global 3 log 127.0.0.1 local2 # 日志定义级别 4 chroot /usr/local/haproxy # 当前工作目录 5 pidfile /var/run/haproxy.pid # 进程id 6 maxconn 4000 # 最大连接数 7 user haproxy # 运行改程序的用户 8 group haproxy 9 daemon # 后台形式运行 10 stats socket /usr/local/haproxy/stats 11 12 defaults 13 mode tcp # haproxy运行模式(http | tcp | health) 14 log global # 采用全局定义的日志 15 option dontlognull # 不记录健康检查的日志信息 16 option redispatch # serverId对应的服务器挂掉后,强制定向到其他健康的服务器 17 retries 3 # 三次连接失败则服务器不用 18 timeout http-request 10s 19 timeout queue 1m 20 timeout client 1m # 客户端超时 21 timeout server 1m # 服务器超时 22 timeout http-keep-alive 10s 23 timeout check 10s # 心跳检测 24 maxconn 600 # 最大连接数 25 26 listen stats # 配置haproxy状态页(用来查看的页面) 27 mode http 28 bind 0.0.0.0:8888 29 stats enable 30 stats refresh 30s #设置统计页面自动刷新的时间 31 stats uri /stats #设置统计页面url 32 stats realm Haproxy Manager #设置登录HAProxy统计页面密码框上提示文本 33 stats auth admin:admin #设置登录HAProxy统计页面用户名和密码设置 34 #stats hide-version #隐藏统计页面上HAProxy的版本信息 35 frontend read 36 bind *:3307 # 监听前端3307端口(表示任何ip访问3307端口都会将数据轮番转发到mysql服务器群组中) 37 default_backend mysql_read # 后端服务器组名 38 39 backend mysql_read 40 balance roundrobin # 使用轮询方式调度 41 server mysql1 192.168.88.100:3306 check port 3306 maxconn 300 42 server mysql2 192.168.88.101:3306 check port 3306 maxconn 300 43 44 frontend write 45 bind *:3308 # 监听前端3308端口(表示任何ip访问3308端口都会将数据轮番转发到mysql服务器群组中) 46 default_backend mysql_write # 后端服务器组名 47 48 backend mysql_write 49 server mysql1 192.168.88.100:3306 check port 3306 maxconn 300
1 [root@slave ~]# vi /etc/haproxy/haproxy.cfg 2 global 3 log 127.0.0.1 local2 # 日志定义级别 4 chroot /usr/local/haproxy # 当前工作目录 5 pidfile /var/run/haproxy.pid # 进程id 6 maxconn 4000 # 最大连接数 7 user haproxy # 运行改程序的用户 8 group haproxy 9 daemon # 后台形式运行 10 stats socket /usr/local/haproxy/stats 11 12 defaults 13 mode tcp # haproxy运行模式(http | tcp | health) 14 log global # 采用全局定义的日志 15 option dontlognull # 不记录健康检查的日志信息 16 option redispatch # serverId对应的服务器挂掉后,强制定向到其他健康的服务器 17 retries 3 # 三次连接失败则服务器不用 18 timeout http-request 10s 19 timeout queue 1m 20 timeout client 1m # 客户端超时 21 timeout server 1m # 服务器超时 22 timeout http-keep-alive 10s 23 timeout check 10s # 心跳检测 24 maxconn 600 # 最大连接数 25 26 listen stats # 配置haproxy状态页(用来查看的页面) 27 mode http 28 bind 0.0.0.0:8888 29 stats enable 30 stats refresh 30s #设置统计页面自动刷新的时间 31 stats uri /stats #设置统计页面url 32 stats realm Haproxy Manager #设置登录HAProxy统计页面密码框上提示文本 33 stats auth admin:admin #设置登录HAProxy统计页面用户名和密码设置 34 #stats hide-version #隐藏统计页面上HAProxy的版本信息 35 frontend read 36 bind *:3307 # 监听前端3307端口(表示任何ip访问3307端口都会将数据轮番转发到mysql服务器群组中) 37 default_backend mysql_read # 后端服务器组名 38 39 backend mysql_read 40 balance roundrobin # 使用轮询方式调度 41 server mysql1 192.168.88.100:3306 check port 3306 maxconn 300 42 server mysql2 192.168.88.101:3306 check port 3306 maxconn 300 43 44 frontend write 45 bind *:3308 # 监听前端3308端口(表示任何ip访问3308端口都会将数据轮番转发到mysql服务器群组中) 46 default_backend mysql_write # 后端服务器组名 47 48 backend mysql_write 49 server mysql1 192.168.88.101:3306 check port 3306 maxconn 300
1 [root@master ~]# vi /usr/local/heartbeat/etc/ha.d/authkeys 2 auth 3 3 3 md5 Yes!
1 [root@master ~]# vi /usr/local/heartbeat/etc/ha.d/ha.cf 2 logfile /var/log/ha-log #记录Heartbeat其他相关日志信息 3 logfacility local0 #设置heartbeat的日志,这里用的是系统日志 4 keepalive 2 #设定心跳(监测)时间间隔为2秒 5 deadtime 15 #宣告死亡时间 6 warntime 10 #心跳延时时间 7 initdead 60 #初始化时间 8 udpport 694 #用于通信的UDP端口 9 bcast eth1 #接受广播心跳的网卡接口 10 ucast eth1 192.168.77.101 #置对方机器心跳检测的IP 11 auto_failback off #关闭自动切回恢复正常的主节点 12 node master.yewu.com #集群节点的名称,必须匹配uname -n的结果。 13 node slave.yewu.com 14 ping 192.168.88.1 15 respawn hacluster /usr/local/heartbeat/libexec/heartbeat/ipfail
1 [root@master ~]# vi /root/remove_slave.sh 2 #!/bin/sh 3 #****************************************************************# 4 # ScriptName: remove_slave.sh 5 # Author: xhy 6 # Create Date: 2018-12-22 09:58 7 # Modify Author: xhy 8 # Modify Date: 2018-12-22 09:58 9 # Version: 10 #***************************************************************# 11 user=root 12 password=x120952576 13 log=/var/log/mariadb/remove_slave.log 14 echo "`date`" >> $log 15 rm -rf /tmp/kill.sql 16 mysql -u$user -p$password -e "select * into outfile ‘/tmp/kill.sql‘ from (select concat(‘kill ‘,id,‘;‘) from information_schema.processlist where command=‘sleep‘ union all select ‘set global read_o 17 nly=OFF;‘ union all select ‘stop slave;‘ union all select ‘reset slave all;‘) t;" 18 mysql -u$user -p$password < /tmp/kill.sql >> $log 19 /bin/sed -i ‘s#read-only#\#read-only#‘ /etc/my.cnf
1 # vi /usr/lib/systemd/system/mariadb.service 2 #PrivateTmp=true 3 PrivateTmp=false 4 # systemctl daemon-reload 5 # systemctl restart mariadb.service
1 [root@master ~]# chmod 755 /root/remove_slave.sh 2 [root@master ~]# scp /root/remove_slave.sh root@192.168.88.101:/root/
1 [root@master ~]# vi mysql_check.sh 2 #!/bin/sh 3 #****************************************************************# 4 # ScriptName: mysql_check.sh 5 # Author: xhy 6 # Create Date: 2018-12-20 16:40 7 # Modify Author: xhy 8 # Modify Date: 2018-12-20 16:40 9 # Version: 10 #***************************************************************# 11 MYSQL=/usr/bin/mysql 12 MYSQL_HOST=localhost 13 MYSQL_USER=root 14 MYSQL_PASSWORD=x120952576 15 date=`date +%y%m%d-%H:%M:` 16 echo $date 17 $MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD -e "show status;" >/dev/null 2>&1 18 #$mysqlclient --host=$host --port=$port--user=$user --password=$password -e"show databases;" > /dev/null 2>&1 19 if [ $? == 0 ] 20 then 21 echo " $host mysql login successfully " 22 exit 0 23 else 24 echo " $host mysql login faild" 25 /etc/init.d/heartbeat stop 26 exit 2 27 fi 28 [root@master ~]# chmod 755 /root/mysql_check.sh 29 [root@master ~]# scp /root/mysql_check.sh root@192.168.88.101:/root/ 30 [root@master ~]# crontab -e #定时任务 31 */1 * * * * /root/mysql_check.sh >>/var/log/mariadb/check_mysql.log 32 [root@slave ~]# crontab -e #定时任务 33 */1 * * * * /root/mysql_check.sh >>/var/log/mariadb/check_mysql.log
1 root@master ~]# vi /usr/local/heartbeat/etc/ha.d/resource.d/changemysql 2 /root/remove_slave.sh 3 /etc/init.d/haproxy restart 4 [root@master ~]# chmod 755 /usr/local/heartbeat/etc/ha.d/resource.d/changemysql 5 [root@master ~]# ll /usr/local/heartbeat/etc/ha.d/resource.d/ #查看现有资源类型 6 [root@master ~]# vi /usr/local/heartbeat/etc/ha.d/haresources 7 master.yewu.com IPaddr::192.168.88.88/24/eth0 mariadb changemysql 8 [root@master ~]# scp /usr/local/heartbeat/etc/ha.d/{ha.cf,haresources,authkeys} 192.168.88.101:/usr/local/heartbeat/etc/ha.d/ #将所有配置复制至slave节点 9 [root@master ~]# scp /usr/local/heartbeat/etc/ha.d/resource.d/changemysql root@192.168.88.101:/usr/local/heartbeat/etc/ha.d/resource.d/ 10 [root@slave ~]# vi /usr/local/heartbeat/etc/ha.d/ha.cf 11 ucast eth1 192.168.77.100 #置对方机器心跳检测的IP
1 [root@slave ~]# systemctl start heartbeat.service 2 [root@slave ~]# systemctl enable heartbeat.service 3 [root@master ~]# service haproxy start 4 [root@master ~]# systemctl start heartbeat.service 5 [root@master ~]# systemctl enable heartbeat.service
1 [root@master ~]# ifconfig
1 [root@master ~]# ps -ef | grep -E ‘heartbeat|haproxy‘ | grep -v grep
1 [root@slave ~]# ps -ef | grep -E ‘heartbeat|haproxy‘ | grep -v grep
1 [root@localhost ~]# mysql -uroot -px120952576 -P3307 -h192.168.88.88 -e "show variables like ‘server_id‘"
1 [root@localhost ~]# mysql -uroot -px120952576 -P3308 -h192.168.88.88 -e "show variables like ‘server_id‘"
1 [root@slave ~]# pkill -9 mysqld 2 [root@localhost ~]# mysql -uroot -px120952576 -P3307 -h192.168.88.88 -e "show variables like ‘server_id‘" 3 [root@localhost ~]# mysql -uroot -px120952576 -P3308 -h192.168.88.88 -e "show variables like ‘server_id‘"
1 [root@slave ~]# systemctl start mariadb.service #重启从库 2 [root@localhost ~]# mysql -uroot -px120952576 -P3307 -h192.168.88.88 -e "show variables like ‘server_id‘" #再次验证3307端口的读负载均衡转发策略
1 [root@master ~]# pkill -9 mysqld 2 [root@localhost ~]# mysql -uroot -px120952576 -P3308 -h192.168.88.88 -e "show variables like ‘server_id‘" 3 [root@localhost ~]# mysql -uroot -px120952576 -P3307 -h192.168.88.88 -e "show variables like ‘server_id‘"
Heartbeat+HAProxy+MySQL半复制高可用架构
标签:不记录 name 命令 format 必须 ifconf man 健康 line
原文地址:https://www.cnblogs.com/drizzle-xu/p/10276323.html