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 20:04
下一篇 2017-06-29 22:19

相關推薦

  • RPM軟件包管理器及前端工具yum

    目前在linux系統之上軟件安裝方式最常見的有兩種,分別是: dpkg:這個機制最早是由Debian Linux社區所開發出來的,透過dpkg的機制,Debian提供的軟件就能夠簡單的安裝起來,同時還能提供安裝后的軟件資訊,實在非常不錯。只要是衍生于Debian的其他linux發行版,大多使用dpkg這個機制來管理軟件的,包括B2D、Ubuntu等等。 RP…

    Linux干貨 2016-08-20
  • 磁盤管理

    設備文件 I/O Ports: I/O 設備地址 一切皆文件: open(), read(), write(), close() 設備類型: 塊設備:block ,存取單位“塊”,磁盤 [root@localhost ~]# ll /dev/ brw-rw—-. 1 root disk 253, 0 Apr 29 03:53 dm-0 塊設備 brw-r…

    2017-04-28
  • DHCP服務

    dhcp server: DHCP:Dynamic Host Configuration Protocol     arp –> address resolving Protocol     &nbsp…

    Linux干貨 2016-11-01
  • 文件的查找

    文件的查找 使用locate命令 默認從根開始搜索 非實時查找(數據庫查找) locate 文件名 查詢系統上預建的的文件索引數據庫    /var/lib/mlocate/mlocate.db 依賴于事先構建的索引 索引的構建是在系統較為空閑時自動進行(周期性任務),管理員手動更新數據庫(updatedb)。注意工作中不能輕易…

    Linux干貨 2016-08-18
  • Linux的終端類型

         序  終端的概念是由Ken Thompson提出的,是人機交互的接口。它是一種字符型設備,有多種類型,它大體上分為設備終端,物理終端,虛擬終端,圖形終端,串行終端,偽終端。 一、   設備終端  設備終端就是顯而易見的外在設備,比如鍵盤、鼠標和顯示器等。 二、&nbsp…

    Linux干貨 2016-10-19
  • 行編輯器sed

    行編輯器sed   Sed,grep,awk命令被稱為文本編輯三劍客,目前學習了grep命令,sed命令 自我覺得sed命令更強大,排名第一,grep暫居第二,awk沒學不太清楚。   這次的博客寫一下剛學習的sed命令,grep命令是全局搜索剪切,sed命令則是逐行進行處理,它一次處理一行內容。Sed命令有兩個空間,…

    2017-08-26
欧美性久久久久