Keepalived+Mysql


#允许远程root连接到mysql
grant all privileges on *.* to 'root'@'%' identified by '142857'with grant option; #也适用新建用户

grant all privileges on *.* to 'amo'@'%' identified by '123456'with grant option;
mysql -uroot -p142857 -e "grant all on *.* to 'root'@'%' identified by 'root';
flush privileges";

show variables like 'datadir';
select uuid();
--------------------------------------------------------------------------------

#配keepalived高可用+Mysql双主热备(主1,主2)
real_server只绑本机
vi /etc/keepalived/keepalived.etc

vi /mysql.sh
#!/bin/bash
pkill keepalived

global_defs {
notification_email {
xx@qq.com
}
notification_email_from xx@qq.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MYSQL_HA #标识,双主相同
}

vrrp_instance VI_1 {
state BACKUP #两台配置此处均是BACKUP
interface ens33
virtual_router_id 51 #主备相同
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.221.20
}
}

virtual_server 192.168.221.138 3306 {
delay_loop 2 #每个2秒检查一次real_server状态
#lb_algo wrr #LVS算法,用不到,我们就关闭了
#lb_kind DR #LVS模式,如果不关闭,备用服务器不能通过VIP连接主MySQL
persistence_timeout 60 #会话保持时间,同一IP的连接60秒内被分配到同一台真实服务器
protocol TCP

real_server 本机ip 3306 { #检测本地mysql,backup也要写检测本地mysql
weight 3
notify_down /mysql.sh #当mysql服down时,执行此脚本,杀死keepalived实现切换
TCP_CHECK {
connect_timeout 10 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连间隔时间
connect_port 3306 #健康检查端口
}
}
}

mysql -uroot -p -h 漂移地址 -P3306

重启开启相应服务

主1创建数据表,查看主2是否同步
--------------------------------------------------------------------------------
或采用监测mysql端口来实现漂移地址
vrrp_script chk_mysql_port { #检测mysql服务是否在运行。有很多方式,比如进程,用脚本检测等等
script "/chk_mysql.sh" #这里通过脚本监测
interval 2 #脚本执行间隔,每2s检测一次
weight -5 #脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5
fall 2 #检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)
rise 1 #检测1次成功就算成功。但不修改优先级
}

track_script {
chk_mysql_port
}
}

vi /chk_mysql.sh
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]
then
/etc/init.d/keepalived stop
else
echo "running..." >> /opt/keepalived-running-info.log
sleep 5000
fi
--------------------------------------------------------------------------------

转载于:https://www.cnblogs.com/feizhuanye/p/10395029.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Keepalived is a software that provides high availability and load balancing for Linux systems. It works by monitoring the health of servers and automatically redirecting traffic to healthy servers in case of failure. MySQL is an open-source relational database management system that is widely used in web applications. When combined, Keepalived and MySQL can provide a highly available and scalable database solution. To set up Keepalived with MySQL, you need to have at least two MySQL servers running on different machines. Keepalived should be installed on both machines. The first step is to configure Keepalived to monitor the health of the MySQL servers. This can be done by setting up a health check script that checks the status of the MySQL service. Once the health check is configured, Keepalived can be set up to redirect traffic to the healthy MySQL server. This can be done by configuring virtual IP addresses (VIPs) that are managed by Keepalived. The VIPs are assigned to the active MySQL server, and if the active server fails, the VIP is moved to the standby server. To ensure data consistency between the MySQL servers, you can use MySQL replication. This involves setting up a master-slave configuration where one MySQL server acts as the master and the other as the slave. The master server replicates its data to the slave server in real-time, ensuring that both servers have the same data. Overall, Keepalived and MySQL can provide a highly available and scalable database solution for web applications. By using Keepalived to monitor the health of MySQL servers and redirect traffic to healthy servers, and using MySQL replication to ensure data consistency, you can ensure that your database is always available and can handle high traffic loads.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值