Home Database Mysql Tutorial 用HAProxy来检测MySQL复制的延迟的教程_MySQL

用HAProxy来检测MySQL复制的延迟的教程_MySQL

Jun 01, 2016 pm 01:00 PM
mysql php

 在MySQL世界里,HAProxy 通常来作为软件负载均衡器使用。彼得.博罗什在过去的邮件中解释了如何使用percona xtradb集群(pxc)来对其设置。所以它只发送查询到可应用的节点。同样的方法可用于常规主从设置来读取负载并分散到多个从节点。不过,使用MySQL复制,另一个因素开始发挥作用:复制延迟。在这种情况下,被提及到的 Percona xtraDB 集群以及我们提出只返回“向上”或者“向下”的检查方法行不通。我们将希望依赖其复制延迟来调整内部Haproxy的一个权重。这就是我们要做的在这篇文章中使用HAProxy 1.5。

HAProxy的代理检测


HAProxy 1.5运行我们运行一个代理检测,这是一项可以添加到常规健康检测项的检测。代理检测的好处是返回值可以是‘up'或 ‘down',但也可以是个权重值。

代理是什么呢?它是一个简单的可以访问给定端口上TCP连接的程。所以,如果我们要在一台MySQL服务器上运行代理,这需要:

  •     如果不运行复制的话确保服务在HAProxy上是停止的
  •     如果复制延迟小于10s,设置weight为100%
  •     如果延迟大于等于10s,小于50s,设置weight为50%
  •     在其他情况下设置weight为5%


我们可以使用这样一个脚本:
 

$ less agent.php
<!--&#63;php
// Simple socket server
// See http://php.net/manual/en/function.stream-socket-server.php
$port = $argv[1];
$mysql_port = $argv[2];
$mysql = "/usr/bin/mysql";
$user = 'haproxy';
$password = 'haproxy_pwd';
$query = "SHOW SLAVE STATUS";
function set_weight($lag){
  # Write your own rules here
  if ($lag == 'NULL'){
    return "down";
  }
  else if ($lag < 10){
    return "up 100%";
  }
  else if ($lag -->= 10 && $lag < 60){
    return "up 50%";
  }
  else
    return "up 5%";
}
set_time_limit(0);
$socket = stream_socket_server("tcp://127.0.0.1:$port", $errno, $errstr);
if (!$socket) {
  echo "$errstr ($errno)
n";
} else {
  while ($conn = stream_socket_accept($socket,9999999999999)) {
    $cmd = "$mysql -h127.0.0.1 -u$user -p$password -P$mysql_port -Ee "$query" | grep Seconds_Behind_Master | cut -d ':' -f2 | tr -d ' '";
    exec("$cmd",$lag);
    $weight = set_weight($lag[0]);
    unset($lag);
    fputs ($conn, $weight);
    fclose ($conn);
  }
  fclose($socket);
}
&#63;>
Copy after login

如果你希望脚本从端口6789发出连接到运行在3306端口上的MySQL实例,这样运行:

$ php agent.php 6789 3306
Copy after login

你还需要指定MySQL用户:

mysql> GRANT REPLICATION CLIENT ON *.* TO 'haproxy'@'127.0.0.1' IDENTIFIED BY 'haproxy_pwd';
Copy after login

代理启动后,你可以检测一下它是否正常运行:

# telnet 127.0.0.1 6789
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
up 100%
Connection closed by foreign host.
Copy after login

假设它运行在本地的应用服务器上,有两个复制正在运行(192.168.10.2和192.168.10.3),应用的读请求在3307端口,你需要在HAProxy中配置一个前端和后端,像这样:
 

代码如下:


frontend read_only-front
bind *:3307
mode tcp
option tcplog
log global
default_backend read_only-back
backend read_only-back
mode tcp
balance leastconn
server slave1 192.168.10.2 weight 100 check agent-check agent-port 6789 inter 1000  rise 1  fall 1 on-marked-down shutdown-sessions
server slave2 192.168.10.3 weight 100 check agent-check agent-port 6789 inter 1000  rise 1  fall 1 on-marked-down shutdown-sessions

现在所有的都准备好了,现在让我们看看怎么使用HAProxy来动态的改变重滞服务器,代码如下:
  

代码如下:


# Slave1
$ mysql -Ee "show slave status" | grep Seconds_Behind_Master
        Seconds_Behind_Master: 0
# Slave2
$ mysql -Ee "show slave status" | grep Seconds_Behind_Master
        Seconds_Behind_Master: 0
# HAProxy
$ echo "show stat" | socat stdio /run/haproxy/admin.sock | cut -d ',' -f1,2,18,19
# pxname,svname,status,weight
read_only-front,FRONTEND,OPEN,
read_only-back,slave1,UP,100
read_only-back,slave2,UP,100
read_only-back,BACKEND,UP,200 


时延1
  

代码如下:


# Slave1
$ mysql -Ee "show slave status" | grep Seconds_Behind_Master
        Seconds_Behind_Master: 25
# Slave2
$ mysql -Ee "show slave status" | grep Seconds_Behind_Master
        Seconds_Behind_Master: 0
# echo "show stat" | socat stdio /run/haproxy/admin.sock | cut -d ',' -f1,2,18,19
# pxname,svname,status,weight
read_only-front,FRONTEND,OPEN,
read_only-back,slave1,UP,50
read_only-back,slave2,UP,100
read_only-back,BACKEND,UP,150 


时延2
  

代码如下:


# Slave1
$ mysql -Ee "show slave status" | grep Seconds_Behind_Master
        Seconds_Behind_Master: 0
# Slave2
$ mysql -Ee "show slave status" | grep Seconds_Behind_Master
        Seconds_Behind_Master: NULL
# echo "show stat" | socat stdio /run/haproxy/admin.sock | cut -d ',' -f1,2,18,19
# pxname,svname,status,weight
read_only-front,FRONTEND,OPEN,
read_only-back,slave1,UP,100
read_only-back,slave2,DOWN (agent),100
read_only-back,BACKEND,UP,100 


结论

在HAProxy 1.5中代理检查是一个很好的新增功能。 在上面的设置中是简单的: 举例来说, 如果 HAProxy 连接代理失败,它就不会被标记。 推荐与代理检查一起,保持常规的健康度检查。

细心的读取者们(reads)将会注意到这个配置,如果在所有节点上都被复制, HAProxy将会停止发送给读取者. 这可能不是最好的解决方案。但可能的选项是:停止代理并标记服务器为UP,使用状态套接字(socket)或者添加主节点作为备份服务器。

最后一点,使用Percona工具集的pt-heartbeat替代Seconds_Behind_Master,您可以编辑代理的代码,可以对复制的延迟进行测量 。

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? Apr 01, 2025 pm 03:03 PM

The page is blank after PHP connects to MySQL, and the reason why die() function fails. When learning the connection between PHP and MySQL database, you often encounter some confusing things...

Top 10 PHP CMS Platforms For Developers in 2024 Top 10 PHP CMS Platforms For Developers in 2024 Dec 05, 2024 am 10:29 AM

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content

See all articles