Home Database Mysql Tutorial MYSQL + MHA +keepalive + VIP安装配置(三)-----keepalived安装_MySQL

MYSQL + MHA +keepalive + VIP安装配置(三)-----keepalived安装_MySQL

Jun 01, 2016 pm 01:16 PM
web server Detection

一、概述

keepalived介绍:Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web 服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人 工做的只是修复故障的web服务器。

二、环境

vip:192.168.1.203/204mysql-master:192.168.1.231mysql-slave:192.168.1.232
Copy after login

三、keepalived安装

安装步骤两台机mysql-master、mysql-slave一样,如下:

1、下载地址:http://www.keepalived.org/software/keepalived-1.2.12.tar.gz

shell>wget http://www.keepalived.org/software/keepalived-1.2.12.tar.gz
Copy after login

2、安装环境

yum -y install openssl-devel
Copy after login

 否则会报如下错误

configure: error:!!! OpenSSL is not properly installed on your system. !!!!!! Can not include OpenSSL headers files.
Copy after login

3、安装

shell>tar -zxvfkeepalived-1.2.12.tar.gzshell>cd keepalived-1.2.12shell>./configure --prefix=/opt/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/shell>make shell>make install
Copy after login

 说明:

 --prefix:安装路径

 --with-kernel-dir:这个是重要的参数,这个参数并不表示我们要把Keepalived统进内核,而是指使用内核源码里面的头文件,也就是include目录。

  2.6.32-431.el6.x86_64可以通过

shell>uname -r 命令查看到
Copy after login

 4、配置

# cp /opt/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/# cp /opt/keepalived/etc/sysconfig/keepalived /etc/sysconfig/# mkdir /etc/keepalived# cp /opt/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/# cp /opt/keepalived/sbin/keepalived /usr/sbin/
Copy after login

 5、启动/停止

shell>service keepalived startshell>service keepalived stopshell>service keepalived restart
Copy after login

四、keepalived配置

1、mysql-master:192.168.1.231的配置

shell>vim /etc/keepalived/keepalived.conf
Copy after login
Copy after login

 配置如下:

global_defs { router_id mysql-master #修改为自己的主机名 notification_email { mengtao10@163.com #接收邮件,可以有多个,一行一个} #当主、备份设备发生改变时,通过邮件通知 notification_email_from lzyangel@126.com #发送邮箱服务器 smtp_server stmp.163.com #发送邮箱超时时间 smtp_connect_timeout 30 }##################第一部分###################vrrp_instance VI_1 {	state BACKUP#都修改成BACKUP	interface eth0#绑定的网卡	virtual_router_id 60 #默认51 主从都修改为60	priority 100		#优先级,在mysql-slave上LVS上修改成80	advert_int 1	nopreempt #不抢占资源,意思就是它活了之后也不会再把主抢回来	authentication {	# 认证方式,可以是PASS或AH两种认证方式	auth_type PASS	# 认证密码	auth_pass 1111	}virtual_ipaddress {	192.168.1.203	192.168.1.204#这可以增加多个VIP	}}##################第二部分###################virtual_server 192.168.1.203 3306 {	delay_loop 6	lb_algo wrr	lb_kind DR	nat_mask 255.255.255.0	persistence_timeout 50	protocol TCPreal_server 192.168.1.231 3306 {	weight 1	notify_down /root/mysql_down.sh	TCP_CHECK {	connect_timeout 10	nb_get_retry 3	connect_port 3306	}	}}
Copy after login

 2、mysql-master:192.168.1.232的配置

shell>vim /etc/keepalived/keepalived.conf
Copy after login
Copy after login

 配置如下:

global_defs { router_id mysql-master #修改为自己的主机名 notification_email { mengtao10@163.com #接收邮件,可以有多个,一行一个} #当主、备份设备发生改变时,通过邮件通知 notification_email_from lzyangel@126.com #发送邮箱服务器 smtp_server stmp.163.com #发送邮箱超时时间 smtp_connect_timeout 30 }##################第一部分###################vrrp_instance VI_1 {	state BACKUP#都修改成BACKUP	interface eth0#绑定的网卡	virtual_router_id 60 #默认51 主从都修改为60	priority 80		 #优先级,在mysql-master上LVS上修改成100	advert_int 1	authentication {	# 认证方式,可以是PASS或AH两种认证方式	auth_type PASS	# 认证密码	auth_pass 1111	}virtual_ipaddress {	192.168.1.203	192.168.1.204#这可以增加多个VIP	}}##################第二部分###################virtual_server 192.168.1.203 3306 {	delay_loop 6	lb_algo wrr	lb_kind DR	nat_mask 255.255.255.0	persistence_timeout 50	protocol TCPreal_server 192.168.1.232 3306 {	weight 1	notify_down /root/mysql_down.sh	TCP_CHECK {	connect_timeout 10	nb_get_retry 3	connect_port 3306	}	}}
Copy after login

 3、myslq_down.sh配置,两台机都要配置这一步!!!

    这里需要注意的是,notify_down /root/mysql_down.sh这个选项,这个是是在keepalived检测不到mysql的时候要执行的脚本,从上面的配置文件来看real服务器只有本机。那么,keeaplived如果启动,客户端也只是访问本机的mysql。nopreempt这个选下也得注意,这个是不抢占资源在优先级高的机器上配置就可以。
看下这个脚本的内容:

# vim /root/mysql_down.sh#!/bin/bashpkill keepalived# chmod +x /root/mysql_down.sh #授权可执行权限
Copy after login

 脚本内容就一条命令:pkill keepalived,主要作用是如果本机的mysql挂掉了,那么同时会杀死本机的keepalived,这样另外一台就会接替他工作,虚拟IP也会被另一台接管,如果不杀死keepalived虚拟IP不会被另一台接管,mysql访问也就不会切换过去。

 注意:必须要先启动MYSQL,再启动keepalived,否则keepalived启动后会运行mysql_down.sh脚本,等于自杀。

4、mysql-master(231)启动后查看IP的绑定情况,如下:

shell> ip a1: lo: <loopback> mtu 16436 qdisc noqueue state UNKNOWN 	link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00	inet 127.0.0.1/8 scope host lo	inet6 ::1/128 scope host 	 valid_lft forever preferred_lft forever2: eth0: <broadcast> mtu 1500 qdisc pfifo_fast state UP qlen 1000	link/ether 08:00:27:cc:92:22 brd ff:ff:ff:ff:ff:ff	inet 192.168.1.232/24 brd 192.168.1.255 scope global eth0	inet 192.168.1.203/32 scope global eth0	inet 192.168.1.204/32 scope global eth0	inet6 fe80::a00:27ff:fecc:9222/64 scope link 	 valid_lft forever preferred_lft forever</broadcast></loopback>
Copy after login

五、测试

1、231、232两台机同时启动MYSQL及Keepalived.

   通过win cmd 命令行的客户端(或程序)连接VIP:192.168.1.203或204,如下:

C:/Users/Duncan>mysql -h192.168.1.203 -usunney -psunneyWelcome to the MySQL monitor.Commands end with ; or /g.Your MySQL connection id is 1440Server version: 5.5.37-log MySQL Community Server (GPL)Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql|| performance_schema || sunney || test |+--------------------+5 rows in set (0.04 sec)mysql>
Copy after login
Copy after login

注意:这时存储的数据库是mysql-master(192.168.1.231)因为他是主库。配置 priority 100 优先级高。

2、mysql-slave(232)的机同时关闭MYSQL及Keepalived.这时关闭上以上的是一样的。因为他没有走这个库。

3、mysql-master(231)同时关闭MYSQL及Keepalived,mysql-slave(232)同时启动MYSQL及Keepalived

    切换很快,大概在2到3秒之间!

   通过win cmd 命令行的客户端(或程序)连接VIP:192.168.1.203或204,如下:

C:/Users/Duncan>mysql -h192.168.1.203 -usunney -psunneyWelcome to the MySQL monitor.Commands end with ; or /g.Your MySQL connection id is 1440Server version: 5.5.37-log MySQL Community Server (GPL)Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql|| performance_schema || sunney || test |+--------------------+5 rows in set (0.04 sec)mysql>
Copy after login
Copy after login

 成功!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution to i7-7700 unable to upgrade to Windows 11 Solution to i7-7700 unable to upgrade to Windows 11 Dec 26, 2023 pm 06:52 PM

The performance of i77700 is completely sufficient to run win11, but users find that their i77700 cannot be upgraded to win11. This is mainly due to restrictions imposed by Microsoft, so they can install it as long as they skip this restriction. i77700 cannot be upgraded to win11: 1. Because Microsoft limits the CPU version. 2. Only the eighth generation and above versions of Intel can directly upgrade to win11. 3. As the 7th generation, i77700 cannot meet the upgrade needs of win11. 4. However, i77700 is completely capable of using win11 smoothly in terms of performance. 5. So you can use the win11 direct installation system of this site. 6. After the download is complete, right-click the file and "load" it. 7. Double-click to run the "One-click

Fall detection, based on skeletal point human action recognition, part of the code is completed with Chatgpt Fall detection, based on skeletal point human action recognition, part of the code is completed with Chatgpt Apr 12, 2023 am 08:19 AM

Hello everyone. Today I would like to share with you a fall detection project, to be precise, it is human movement recognition based on skeletal points. It is roughly divided into three steps: human body recognition, human skeleton point action classification project source code has been packaged, see the end of the article for how to obtain it. 0. chatgpt First, we need to obtain the monitored video stream. This code is relatively fixed. We can directly let chatgpt complete the code written by chatgpt. There is no problem and can be used directly. But when it comes to business tasks later, such as using mediapipe to identify human skeleton points, the code given by chatgpt is incorrect. I think chatgpt​ can be used as a toolbox that is independent of business logic. You can try to hand it over to c

Security auditing and event log management of web servers built on CentOS Security auditing and event log management of web servers built on CentOS Aug 05, 2023 pm 02:33 PM

Overview of security auditing and event log management of web servers built on CentOS. With the development of the Internet, security auditing and event log management of web servers have become more and more important. After setting up a web server on the CentOS operating system, we need to pay attention to the security of the server and protect the server from malicious attacks. This article will introduce how to perform security auditing and event log management, and provide relevant code examples. Security audit Security audit refers to comprehensive monitoring and inspection of the security status of the server to promptly discover potential

Best Practices: Performance Tuning Guide for Building a Web Server on CentOS Best Practices: Performance Tuning Guide for Building a Web Server on CentOS Aug 04, 2023 pm 12:17 PM

Best Practices: Performance Tuning Guide for Building Web Servers on CentOS Summary: This article aims to provide some performance tuning best practices for users building web servers on CentOS, aiming to improve the performance and response speed of the server. Some key tuning parameters and commonly used optimization methods will be introduced, and some sample codes will be provided to help readers better understand and apply these methods. 1. Turn off unnecessary services. When building a web server on CentOS, some unnecessary services will be started by default, which will occupy system resources.

MIT's latest masterpiece: using GPT-3.5 to solve the problem of time series anomaly detection MIT's latest masterpiece: using GPT-3.5 to solve the problem of time series anomaly detection Jun 08, 2024 pm 06:09 PM

Today I would like to introduce to you an article published by MIT last week, using GPT-3.5-turbo to solve the problem of time series anomaly detection, and initially verifying the effectiveness of LLM in time series anomaly detection. There is no finetune in the whole process, and GPT-3.5-turbo is used directly for anomaly detection. The core of this article is how to convert time series into input that can be recognized by GPT-3.5-turbo, and how to design prompts or pipelines to let LLM solve the anomaly detection task. Let me introduce this work to you in detail. Image paper title: Largelanguagemodelscanbezero-shotanomalydete

Permissions and access control strategies that you need to pay attention to before building a web server on CentOS Permissions and access control strategies that you need to pay attention to before building a web server on CentOS Aug 05, 2023 am 11:13 AM

Permissions and access control strategies that you need to pay attention to before building a web server on CentOS. In the process of building a web server, permissions and access control strategies are very important. Correctly setting permissions and access control policies can protect the security of the server and prevent unauthorized users from accessing sensitive data or improperly operating the server. This article will introduce the permissions and access control strategies that need to be paid attention to when building a web server under the CentOS system, and provide corresponding code examples. User and group management First, we need to create a dedicated

What are the five common web servers? What are the five common web servers? Aug 25, 2022 pm 02:03 PM

The five types of web servers are: 1. IIS, a web server that allows publishing information on a public intranet or Internet; 2. Apache, an open source web server of the Apache Software Foundation; 3. WebSphere Application Server, a Web application server; 4. Tomcat is a Java-based Web application software container; 5. Lighttpsd is an open source Web server software.

Improved detection algorithm: for target detection in high-resolution optical remote sensing images Improved detection algorithm: for target detection in high-resolution optical remote sensing images Jun 06, 2024 pm 12:33 PM

01 Outlook Summary Currently, it is difficult to achieve an appropriate balance between detection efficiency and detection results. We have developed an enhanced YOLOv5 algorithm for target detection in high-resolution optical remote sensing images, using multi-layer feature pyramids, multi-detection head strategies and hybrid attention modules to improve the effect of the target detection network in optical remote sensing images. According to the SIMD data set, the mAP of the new algorithm is 2.2% better than YOLOv5 and 8.48% better than YOLOX, achieving a better balance between detection results and speed. 02 Background & Motivation With the rapid development of remote sensing technology, high-resolution optical remote sensing images have been used to describe many objects on the earth’s surface, including aircraft, cars, buildings, etc. Object detection in the interpretation of remote sensing images

See all articles