haproxy實戰之haproxy實現mysql負載均衡

haproxy實戰之haproxy實現mysql負載均衡

實驗目的
haproxy + mysql實現負載均衡

1.準備機器,做好時間同步,域名主機名解析

192.168.42.151 [node2 haproxy]
192.168.42.152 [node3 mariadb]
192.168.42.153 [node4 mariadb]

2.node3,node4上安裝mariadb

(1).安裝mariadb

yum install mariadb-server -y

(2).更改基本配置

vim /etc/my.cnf.d/server.cnf
[mysqld] 
skip_name_resolve=1
log-bin=mysql-bin
innodb_file_per_table = 1

(3).啟動mariadb

systemctl start mariadb

(4).更改密碼

mysqladmin -uroot -p password "root"

(5).登錄mysql

mysql -u root -p

(6).添加外部訪問賬號

MariaDB [(none)]> grant all privileges on *.* to 'magedu'@'192.168.42.%' identified by '123456';
MariaDB [(none)]> flush privileges;

(7)登錄測試一下

mysql -u magedu -h 192.168.42.155 -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>

3.安裝haproxy
node1: (1).haproxy的安裝

yum install haproxy -y

(2).配置haproxy 配置文件如下:

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    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

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    tcp
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  mysql
    bind *:3306
    log   global
    mode  tcp
    default_backend             mysqlsrvs

backend mysqlsrvs
    balance     roundrobin
    server      mysql1 192.168.42.152:3306 check
    server      mysql2 192.168.42.153:3306 check

(3)啟動haproxy

systemctl start haproxy
[root@node2 haproxy]# ss -tnl
State      Recv-Q Send-Q Local Address:Port  Peer Address:Port              
LISTEN     0      128               *:22               *:*                  
LISTEN     0      100       127.0.0.1:25               *:*                  
LISTEN     0      3000              *:3306             *:*                  
LISTEN     0      128              :::22              :::*                  
LISTEN     0      100             ::1:25              :::*

(4)安裝mariadb 客戶端工具

yum install mariadb -y

(5).測試

mysql -u maged -h 192.168.42.151 -p

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+

此時是只有4個庫,我們創建一個庫,并退出

MariaDB [(none)]> create database  leip;
Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| leip               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> exit;
Bye

(6).再一次連接測試

[root@node2 haproxy]# mysql -u magedu -h 192.168.42.151 -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> exit
Bye

從上面可以看到,此時我們的數據庫是只有4個庫了,因此我們可以判斷,我們連接的是兩個不同主機上的mariadb,也就是說負載均衡mysql成功了

原創文章,作者:srayban,如若轉載,請注明出處:http://www.www58058.com/78829

(4)
sraybansrayban
上一篇 2017-06-29
下一篇 2017-06-29

相關推薦

  • httpd服務之虛擬主機、訪問控制、https配置詳解

    前言 上文講解了http協議及httpd的一些特性,是學習web服務需要掌握的一些基礎知識,接下來讓我們進一步了解httpd相關功能的配置,本文講解的是虛擬主機,訪問控制及https等功能的配置。 httpd之虛擬主機 虛擬主機共分為三種模式:基于IP、基于端口、基于主機名(FQDN) 實驗環境介紹 開始之前,先介紹一下httpd在CentOS6.6版本及文…

    Linux干貨 2015-04-14
  • shell編程循環語法作業

    判斷/var/目錄下所有文件的類型 [root@www sh.log]# cat typefile.sh  #!/bin/bash #author:DYW #顯示目錄下文件類型 if [ $# -lt 1 ];then echo "Please&nb…

    Linux干貨 2016-08-21
  • N22-妙手-第七周博客作業第四題:RAID各級別的特性

    什么是RAID RAID的全稱是Redundant Array of Independent Disks – 獨立磁盤冗余陣列?;舅枷胧菍⒍鄠€相對便宜的硬盤組合成為一個硬盤陣列,使其在性能上達到或超過一塊價格昂貴、容量巨大的硬盤。RAID通常被用在服務器上,使用完全相同的硬盤組成一個邏輯扇區,因此操作系統會把其當做一塊硬盤。RAID分為不同的等…

    Linux干貨 2016-09-27
  • 第一周作業-03

    ifconfig命令:查看及臨時修改網卡配置信息    查看的用法:ifconfig [-v] [-a] [-s] [INTERFACE]        OPTIONS            -v:詳細顯示接口報錯信息   &nb…

    Linux干貨 2016-09-19
  • 集群-ipvsadm和NAT實驗(3)

    糾正:報文進入內核空間后,當到達input鏈時發現是一個集群服務時,則直接發送到postrouting鏈,不經過forward鏈。 調度算法: 1、靜態方法: rr:輪詢,即依照次序從所有RS中進行挑選 wrr:加權輪詢,按照權重在RS中進行輪詢 sh:source hashing,源地址哈希,即對來自相同客戶端的請求發送至同一RS,這樣會破壞負載均衡效果。…

    Linux干貨 2015-12-15
  • N21_第7周_磁盤及文件系統管理

    N21_第7周_磁盤及文件系統管理 作業題目: 1、創建一個10G分區,并格式為ext4文件系統;    (1) 要求其block大小為2048, 預留空間百分比為2, 卷標為MYDATA, 默認掛載屬性包含acl;    (2) 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳…

    Linux干貨 2016-09-26
欧美性久久久久