[ Mariadb ] 經過HAProxy代理後端Mariadb實現負載均衡

1、本次環境架構圖mysql

 

  因爲公司內網服務器有限,因此後端採用Mariadb自帶的mysql_multi模型實現多實例。redis

 

mysql的多實例有兩種方式能夠實現,兩種方式各有利弊。算法

  一、使用多個配置文件啓動不一樣的進程來實現多實例,這種方式的優點邏輯簡單,配置簡單,缺點是管理起來不太方便。sql

  二、經過官方自帶的mysqld_multi使用單獨的配置文件來實現多實例,這種方式定製每一個實例的配置不太方便,優勢是管理起來很方便,集中管理。數據庫

 

2、mariadb多實例實現:後端

mariadb配置文件以下(紅色部分爲修改內容):服務器

注意使用了mysqld_multi,就不能在啓用mysqld模塊,紅色部分爲修改或者添加內容架構

[client]
port        = 3306
socket        = /tmp/mysql.sock
#default-character-set=utf8

[mysqld_multi] # 啓用 mysqld_multi 多實例模式 mysqld = /usr/local/mysql/bin/mysqld_safe         # 指定兩個管理命令
mysqladmin    = /usr/local/mysql/bin/mysqladmin


[mysqld1]     # 定義mysql1實例
port        = 3306
socket        = /tmp/mysql3306.sock
basedir = /usr/local/mysql
datadir        = /mysqldata/data3306
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip
-external-locking key_buffer_size = 384M max_allowed_packet = 1M table_open_cache = 512 sort_buffer_size = 2M read_buffer_size = 2M read_rnd_buffer_size = 8M myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size = 32M thread_concurrency = 8 log-bin=mysql-bin-1 server-id = 1 # 標記server-id 不能重複 log-bin=mysql-bin3306 # 用於同步數據的日誌文件 binlog_ignore_db = mysql # 忽視mysql庫的同步 log_slave_updates # 從庫binlog記錄主庫同步的操做日誌 slave_skip_errors = all # 跳過從庫出錯的SQL auto_increment_increment = 2 # 主主同步時,每次遞增量 auto_increment_offset = 1 # 主主同步,從自增開始,從那個數開始自增 [mysqld2] port = 3307 socket = /tmp/mysql3307.sock basedir = /usr/local/mysql datadir = /mysqldata/data3307
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip
-external-locking key_buffer_size = 384M max_allowed_packet = 1M table_open_cache = 512 sort_buffer_size = 2M read_buffer_size = 2M read_rnd_buffer_size = 8M myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size = 32M thread_concurrency = 8 log-bin=mysql-bin-2 server-id = 2 log_bin=mysql-bin3307 log_slave_updates auto_increment_increment = 2 auto_increment_offset = 2 [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash
default-character-set = utf8 [myisamchk] key_buffer_size
= 256M sort_buffer_size = 256M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout

 

初始化數據庫:負載均衡

[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/mysqldata/data3306/
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/mysqldata/data3307/

 

啓動庫:
# 這裏有不少種啓動方式 mysqld_multi start 1, mysqld_multi start 2
# 注意這裏是啓動實例,則不須要啓動mysqldsocket

[root@localhost mysql]# mysqld_multi start 1-2     
[root@localhost mysql]# ss -ntpl | grep mysqld
LISTEN     0      50           *:3306                     *:*                   users:(("mysqld",pid=4775,fd=15))
LISTEN     0      50           *:3307                     *:*                   users:(("mysqld",pid=4774,fd=15))

鏈接實例庫:

鏈接3306

[root@localhost ~]# mysql -uroot -S /tmp/mysql3306.sock

鏈接3307

[root@localhost ~]# mysql -uroot -S /tmp/mysql3307.sock

 

3、實現mariadb 主主同步

1. 在兩個實例中分別建立用於同步的用戶。注:用於同步的用戶必須具有: replication slave, reload, super 權限

3306庫:

MariaDB [(none)]> GRANT REPLICATION SLAVE, RELOAD, SUPER ON *.* TO 'backup'@'192.168.118.187' identified by '123456';

3307庫:

MariaDB [(none)]> GRANT REPLICATION SLAVE, RELOAD, SUPER ON *.* TO 'backup'@'192.168.118.187' identified by '123456';

 

2. 分別查看兩個實例的master log 和 Position

3306庫:

MariaDB [(none)]> show master status\G
*************************** 1. row ***************************
            File: mysql-bin3306.000001
        Position: 503
    Binlog_Do_DB: 
Binlog_Ignore_DB: mysql

3307庫:

MariaDB [(none)]> show master status\G
*************************** 1. row ***************************
            File: mysql-bin3307.000001
        Position: 503
    Binlog_Do_DB: 
Binlog_Ignore_DB: 

 

3. 分別爲兩個實例配置master 互爲主備

3306實例配置:

# 這裏的master爲在實例3307上配置的用戶信息,master_log_file 爲3307的master file master_log_pos 爲 3307的Position

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.118.187', MASTER_PORT=3307, MASTER_USER='backup', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin3307.000001', MASTER_LOG_POS=503;

3307實例配置:

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.118.187', MASTER_PORT=3306, MASTER_USER='backup', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin3306.000001', MASTER_LOG_POS=503;
MariaDB [(none)]> show slave status\G     # 查看是否配置成功

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.118.187
                  Master_User: backup
                  Master_Port: 3307
                Connect_Retry: 60
              Master_Log_File: mysql-bin3307.000001
          Read_Master_Log_Pos: 503
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 533
        Relay_Master_Log_File: mysql-bin3307.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
...

這裏最主要的兩個參數:Slave_IO_Running: Yes、 Slave_SQL_Running: Yes  若是這裏都是Yes表示配置成功。配置master時,注意思路要清晰,端口不能錯。

 

4. 測試:

MariaDB [(none)]> CREATE DATABASE hello_db;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hello_db           |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

3307庫查看是否同步:

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hello_db           |
| mysql              |
| performance_schema |
| test               |
+--------------------+

 

3307 刪除hello_db:

MariaDB [(none)]> DROP DATABASE hello_db;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;

3306 查看是否同步刪除:

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

 

經過以上測試,Mariadb主主配置成功。

 

4、配置HAProxy實現後端mariadb負載均衡

[root@localhost ~]# yum install haproxy -y 

HAProxy 配置文件:

global
    log         127.0.0.1 local2

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

    stats socket /var/lib/haproxy/stats

defaults
    mode                    http
    log                     global
    option                  tcplog
    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

listen proxy-sql 192.168.118.187:23306     # 建立proxy-sql
    mode tcp
    balance roundrobin     # 算法爲輪詢
    option mysql-check user haproxy     # 在mysql中建立無任何權限用戶haproxy,且無密碼用於監控
    server mysql3306 192.168.118.187:3306 check maxconn 2000 weight 1      # 後端主機
    server mysql3307 192.168.118.187:3307 check maxconn 2000 weight 1

[root@localhost haproxy]# ss -tnlp | grep haproxy 
LISTEN     0      128    192.168.118.187:23306                    *:*                   users:(("haproxy",pid=5448,fd=5))

 

2. 建立用於監控的mariadb 用戶 haproxy

# 兩個實例都須要建立,建立一個後,能夠直接查看第二個實例是否也同步了。

MariaDB [(none)]> CREATE USER 'haproxy'@'%' ;

3. 經過haproxy端口鏈接數據庫

  HAProxy工做原理:當HAProxy接收到用戶請求後,不是直接透明轉發客戶端請求到後端服務器,而是HAProxy到後端服務器取數據。
  因而可知,當客戶端發情mariadb鏈接請求後,由HAProxy來創建鏈接mariadb。所以,HAProxy客戶端地址爲HAProxy服務器地址而不是客戶端地址。

  PS: 由於這裏HAProxy是對外服務,因此監聽的地址是:192.168.118.187 而不是本地迴環地址,所以須要對IP地址爲192.168.118.187賦予權限。

  這裏就牽扯到mariadb權限的問題,給與haproxy地址訪問權限:

MariaDB [(none)]> GRANT ALL ON *.* TO 'root'@'192.168.118.187' IDENTIFIED BY '123456';

  以上操做必需要保證兩個實例都要具有:

MariaDB [(none)]> SELECT HOST, USER, PASSWORD FROM mysql.user WHERE HOST='192.168.118.187';
+-----------------+--------+-------------------------------------------+
| HOST            | USER   | PASSWORD                                  |
+-----------------+--------+-------------------------------------------+
| 192.168.118.187 | backup | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| 192.168.118.187 | root   | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------------+--------+-------------------------------------------+

啓動服務

[root@localhost ~]# systemctl start haproxy     # 啓動HAProxy服務

4. 本地測試:

[root@localhost ~]# mysql -uroot -p123456 -P 23306 -h 192.168.118.187 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 558
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)]> 

本次測試成功。

再次使用Navicat鏈接:

 

這樣經過HAProxy代理到後端mariadb負載成功。

 

5、總結:

    1. 這樣的架構最好只是在測試環境使用,由於影響到的因素會不少。好比磁盤IO問題:用戶請求響應的IO,實例同步的IO。
    生產環境建議以下架構:

相關文章
相關標籤/搜索