利用Keepalived构建高可用的MySQL
公司大牛让用keepalived构建高可用的MySQL,在网上看到一篇很完整的文章,于是乎照着前辈的足迹,加上自己补充。如上述均正确配置
前言:公司大牛让用keepalived构建高可用的MySQL,在网上看到一篇很完整的文章,于是乎照着前辈的足迹,加上自己补充。
环境拓扑如下:
MySQL-VIP:192.168.1.150
MySQL-master1:192.168.1.151
MySQL-master2:192.168.1.152
OS版本:RedHat 5.4
MySQL版本:5.0.77
Keepalived版本:1.1.20
一、MySQL master-master的安装及配置
1、安装mysql
#yum install mysql-server -y \\为了节省时间,这里两台server都直接yum装,希望你配好了yum源。
2、修改配置文件
On server1:
#vim /etc/my.cnf
[mysqld]
server-id = 10
log-bin = mysql-bin
replicate-do-db = ccledb
auto-increment-increment = 2
auto-increment-offset = 1
On server2 :
#vim /etc/my.cnf
[mysqld]
server-id = 20
log-bin = mysql-bin
replicate-do-db = ccledb
auto-increment-increment = 2
auto-increment-offset = 2
3、建立授权用户
On server1:
mysql> grant replication client,replication slave on *.* to cclo1@’192.168.1.%’ identified by ‘123456’;
On server2:
mysql> grant replication client,replication slave on *.* to cclo2@’192.168.1.%’ identified by ‘123456’;
4、指定主服务器
On server1
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| MySQL-bin.000003 | 374 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> change master to master_host='192.168.1.152',master_user='cclo2',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=374;
Query OK, 0 rows affected (0.05 sec)
On server2
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| MySQL-bin.000003 | 374 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> change master to master_host='192.168.1.151',master_user='cclo1',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=374;
Query OK, 0 rows affected (0.05 sec)
5、开启主主服务
mysql> start slave; \\两台server分别执行
MySQL> show slave status\G \\查看主主状态
Slave_IO_Running: Yes
Slave_SQL_Running: Yes \\如果此2项都为yes,master-master配置即成功
如上述均正确配置,现在在任何一台MySQL上更新数据都会同步到另一台MySQL。
二、keepalived安装及配置
1、192.168.1.151服务器上keepalived安装及配置
安装keepalived
#tar zxvf keepalived-1.1.20.tar.gz
#cd keepalived-1.1.20
#./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686
\\此处一定确保你的linux中有/usr/src/kernels/2.6.18-164.el5-i686这个目录,如果没有请安装kernel- devel包,建议最好是下载和你的linux系统内核版本相同的devel包安装(一般安装镜像有),此处直接#yum install -y kernel-devel
#make && make install
配置keepalived
此处需新建一个配置文件,默认情况下keepalived启动时会去/etc/keepalived目录下找配置文件
#mkdir /etc/keepalived
#vim /etc/keepalived/keepalived.conf \\配置文件内容如下:
! Configuration File for keepalived
global_defs {
notification_email {
cclo@cer.cn
}
notification_email_from cclo@cer.cn
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MySQL-ha
}
vrrp_instance VI_1 {
state BACKUP #两台配置此处均是BACKUP
interface eth0
virtual_router_id 51
priority 100 #优先级,另一台改为90
advert_int 1
nopreempt #不主动抢占资源,只在优先级高的机器上设置即可,优先级低的机器不设置
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.150
}
}
virtual_server 192.168.1.150 3306 {
delay_loop 2 #每个2秒检查一次real_server状态
lb_algo wrr #LVS算法
lb_kind DR #LVS模式
persistence_timeout 60 #会话保持时间
protocol TCP
real_server 192.168.1.151 3306 {
weight 3
notify_down /usr/local/my/my.sh #检测到服务down后执行的脚本
TCP_CHECK {
connect_timeout 10 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连间隔时间
connect_port 3306 #健康检查端口
}
}
编写检测服务down后所要执行的脚本:
#vim /usr/local/my/my.sh
#!/bin/sh
pkill keepalived
#chmod +x /usr/local/my/my.sh
注:此脚本是上面配置文件notify_down选项所用到的,keepalived使用notify_down选项来检查real_server的服务状态,当发现real_server服务故障时,便触发此脚本;我们可以看到,脚本就一个命令,通过pkill keepalived强制杀死keepalived进程,从而实现了MySQL故障自动转移。另外,,我们不用担心两个MySQL会同时提供数据更新操作,因为每台MySQL上的keepalived的配置里面只有本机MySQL的IP+VIP,而不是两台MySQL的IP+VIP
启动keepalived:
#/usr/local/keepalived/sbin/keepalived –D
#ps -aux | grep keepalived
测试
找一台局域网PC,然后去ping MySQL的VIP,这时候MySQL的VIP是可以ping的通的
停止MySQL服务,#ps -aux | grep keepalived 看keepalived健康检查程序是否会触发我们编写的脚本

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use distributed database architecture to build a highly available MySQL cluster. With the development of the Internet, the demand for high availability and scalability of databases is getting higher and higher. Distributed database architecture has become one of the effective ways to solve these needs. This article will introduce how to use a distributed database architecture to build a highly available MySQL cluster and provide relevant code examples. Building a MySQL master-slave replication cluster MySQL master-slave replication is the basic high-availability solution provided by MySQL. Through master-slave replication, data can be

Golang is an efficient, concise, and safe programming language that can help developers implement highly available distributed systems. In this article, we will explore how Golang implements highly available distributed systems and provide some specific code examples. Challenges of Distributed Systems A distributed system is a system in which multiple participants collaborate. Participants in a distributed system may be different nodes distributed in multiple aspects such as geographical location, network, and organizational structure. When implementing a distributed system, there are many challenges that need to be addressed, such as:

With the rapid development of cloud computing and containerization technology, container orchestration systems have become an important part of modern application deployment and management. The container orchestration system can automatically schedule, deploy and manage multiple containers, providing high availability and scalability. Among many programming languages, the Go language has received widespread attention due to its powerful concurrency features and high performance, and is used by many well-known container orchestration systems such as Docker and Kubernetes. This article will introduce how to use Go language to develop a highly available container orchestration system

Linux and Docker: How to implement a highly available container cluster? Abstract: With the development of container technology, more and more enterprises are gradually deploying applications into containers. In a production environment, achieving high availability for a container cluster is critical. This article will introduce how to use Linux and Docker to build a highly available container cluster, and demonstrate the specific implementation method through code examples. Build a DockerSwarm cluster DockerSwarm is a native container cluster management provided by Docker

How to configure high-availability container orchestration platform monitoring on Linux With the development of container technology, container orchestration platforms are used by more and more enterprises as an important tool for managing and deploying containerized applications. In order to ensure the high availability of the container orchestration platform, monitoring is a very important part. It can help us understand the operating status of the platform in real time, quickly locate problems, and perform fault recovery. This article will introduce how to configure high-availability container orchestration platform monitoring on Linux and provide relevant code examples. 1. Choose appropriate monitoring tools

With the rapid development of the Internet, more and more data need to be stored and processed. In order to ensure the security and reliability of data, distributed storage systems are becoming more and more important. This article will introduce how to use Go language to develop a highly available distributed storage system, and explore some of the key concepts and technologies in practice. Before starting, let's first understand the basic principles of distributed storage systems. A distributed storage system is composed of multiple storage nodes, each node independently stores a portion of data. In order to ensure high availability of data, the system will

How to implement a highly available distributed system in Go language development Summary: With the rapid development of the Internet, the demand for distributed systems is increasing. How to implement a highly available distributed system in Go language development has become an important issue. This article will introduce how to use Go language to develop a highly available distributed system. 1. Introduction A distributed system is composed of multiple independent nodes, and the nodes communicate and coordinate through the network. High availability is one of the core requirements of distributed systems. It can ensure that the system can still operate in the face of various abnormalities and failures.

With the rapid development of Internet applications, caching has become an indispensable part for many Internet companies to speed up access and improve user experience. In order to improve the availability of cache clusters, many companies choose to use Golang language to implement a highly available cache cluster. This article will introduce how to use Golang language to implement a highly available cache cluster, including ideas, implementation methods and optimization suggestions. 1. The architectural idea of cache cluster adopts distributed storage mechanism. In order to ensure the high availability of cache cluster, we need to adopt distributed storage mechanism.
