使用 HAProxy 进行 MySQL 负载均衡

本章教程主要记录如何用HAProxy 实现MySQL负载均衡配置。

一、安装haproxy

在 Ubuntu/Debian 上:

sudo apt-get update
sudo apt-get install haproxy

在 CentOS/RHEL 上:

sudo yum install haproxy

二、配置haproxy

编辑 HAProxy 配置文件(通常位于 /etc/haproxy/haproxy.cfg)。

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   https://www.haproxy.org/download/1.8/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

    # utilize system-wide crypto-policies
    ssl-default-bind-ciphers PROFILE=SYSTEM
    ssl-default-server-ciphers PROFILE=SYSTEM

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    tcp
    log                     global
    option                  tcplog
    option                  dontlognull
    option                  redispatch
    retries                 3
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    maxconn                 3000

#---------------------------------------------------------------------
# MySQL frontend and backend configuration
#---------------------------------------------------------------------
frontend mysql_frontend
    bind *:3308
    default_backend mysql_backend

backend mysql_backend
    balance roundrobin
    server mysql1 172.19.0.2:3306 check
    server mysql2 172.19.0.3:3306 check

三、启动/重启/停止haproxy

1、启动haproxy

sudo systemctl start haproxy

2、重启haproxy

sudo systemctl restart haproxy

3、停止haproxy

sudo systemctl restart haproxy

相关推荐

  1. 使用 HAProxy 进行 MySQL 负载均衡

    2024-07-09 22:02:03       23 阅读
  2. Haproxy负载均衡

    2024-07-09 22:02:03       61 阅读
  3. Haproxy负载均衡

    2024-07-09 22:02:03       20 阅读
  4. linux系统haproxy负载均衡工具的介绍以及使用

    2024-07-09 22:02:03       57 阅读
  5. Haproxy 负载均衡集群

    2024-07-09 22:02:03       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 22:02:03       49 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 22:02:03       53 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 22:02:03       42 阅读
  4. Python语言-面向对象

    2024-07-09 22:02:03       53 阅读

热门阅读

  1. 【Tools】了解人工通用智能 (AGI):未来的智能体

    2024-07-09 22:02:03       20 阅读
  2. HTML最快速最简单

    2024-07-09 22:02:03       21 阅读
  3. 等保测评推动黑龙江数字化转型中的安全保障

    2024-07-09 22:02:03       19 阅读
  4. 嵌入式硬件电路常用设计软件

    2024-07-09 22:02:03       24 阅读
  5. [终端安全]-2 移动终端之硬件安全(SE)

    2024-07-09 22:02:03       18 阅读
  6. Kafka 典型问题与排查以及相关优化

    2024-07-09 22:02:03       19 阅读