本文共 3224 字,大约阅读时间需要 10 分钟。
下载与安装keepalived
cp keepalived-1.2.13.tar /usr/local/srccd /usr/local/srctar zxvf keepalived-1.2.13.tar -C /optcd /opt/keepalived-1.2.13./configure --prefix=/usr/local/keepalivedmake; make install
yum install gccyum install openssl openssl-devel
默认配置路径
/usr/local/keepalived/etc/keepalived/keepalived.conf主节点配置
vim /usr/local/keepalived/etc/keepalived/keepalived.conf
global_defs { notification_email { aaron8219@xxx.xxx } router_id aaron8219}vrrp_instance my_177 { state BACKUP interface eth0 virtual_router_id 88 priority 90 advert_int 1 nopreempt { authentication { auth_type PASS auth_pass 1234 } } virtual_ipaddress { 192.168.17.166 } virtual_server { 192.168.17.166:3306 { delay_loop 2 lb_algo wrr lb_kind DR persistence_timeout 60 protocol TCP real_server { 192.168.17.177:3306 { weight 3 notify_down /opt/mysql/mysql.sh } } tcp_check { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } }}virtual_router_id需与另一节点保持一致,router_id可任意设置。从节点配置
vim /usr/local/keepalived/etc/keepalived/keepalived.conf
state设为BACKUP,priority为90,virtual_router_id保持一致。注意事项
nopreempt仅在state为BACKUP时生效。virtual_router_id必须一致,router_id可任意。state可同时设为BACKUP,但需确保priority较高的节点能接管VIP。启动keepalived
/usr/local/keepalived/sbin/keepalived -f /usr/local/keepalived/etc/keepalived/keepalived.conf -D
-D参数启用详细日志。验证启动状态
tail -30f /var/log/messages
VIP浮动测试
ifdown eth0
ip a show
故障恢复测试
init 6
keepalived进程管理
pkill keepalivedip a show
配置主从复制
server-id不同,建议格式为IP+port。1773306,Master2设置为1883306。设置复制用户
grant replication slave on *.* to 'rep'@'192.168.17.%' identified by 'rep';grant file, select on *.* to 'rep'@'%' identified by 'rep';
初始化同步
reset master;flush tables with read lock;mysqldump -uroot -p --all-databases -l -F > full_db.sqlscp full_db.sql root@192.168.17.188:/data/mysql/percona_3306/data
mysql -uroot -p < /data/mysql/percona_3306/data/full_db.sql
验证主从状态
show master status\Gshow slave status\G
master_log_pos和read_master_log_pos一致。启动从库
change master to master_host='192.168.17.177', master_user='rep', master_password='rep', master_log_file='percona-bin.000001', master_log_pos='120';start slave;
Error: Slave I/O: Fatal error: Master and slave have same UUID...
master.info文件中的UUID,或删除文件后重新初始化。测试数据一致性
insert into zlm.tb_zlm values(4, 'def');
select * from zlm.tb_zlm;
通过上述配置,实现了一个简单的MySQL高可用架构。无论哪一节点故障,VIP仍能正常提供数据库服务,确保系统的稳定性和可用性。
转载地址:http://chdfk.baihongyu.com/