1. 程式人生 > >MySQL負載均衡讀寫分離

MySQL負載均衡讀寫分離

在瞭解《MySQL主從複製原理》和《MySQL主從同步配置》之後就要考慮的是,既然我MySQL的架構是一主兩從,那麼該如何做到負載均衡,是寫操作在Master上執行,讀操作在Slave上操作。

--------

這種負載均衡的解決方案有:

        1、MySQL-Proxy  這是MySQL原生的解決方案,但配置複雜。

        2、Atlas Proxy    Atlas是360團隊弄出來的一套基於MySQL-Proxy基礎之上的代理,修改了MySQL-Proxy的一些BUG,並且優化了很多東西。而且安裝方便。配置的註釋寫的蠻詳細的,都是中文。

-------

Atlas官方連結:https://github.com/Qihoo360/Atlas/blob/master/README_ZH.md

Atlas下載連結: https://github.com/Qihoo360/Atlas/releases

我選擇的版本為:Atlas-2.2.1.el6.x86_64.rpm   我將它傳到了百度雲,這裡

---------

部署規劃

這個規劃是基於《MySQL主從同步配置

name ip 說明
Master 192.168.160.130
3306 主節點
Slave1  192.168.160.131 3306 從節點
Proxy 192.168.160.130 1234 代理節點

---------

安裝與解除安裝Atlas

我選擇的版本為:

將Atlas-2.2.1.el6.x86_64.rpm傳到192.168.160.130。

幫助:

        

#安裝

shell> rpm -i Atlas-2.2.1.el6.x86_64.rpm

#解除安裝

shell> rpm -e Atlas-2.2.1.el6.x86_64.rpm

如下,我的操作
[[email protected] software]# ll
total 878356
-rw-r--r--.  1 root  root    4963681 Jun 26 00:51 Atlas-2.2.1.el6.x86_64.rpm
drwxrwxrwx.  9 root  root       4096 May  8 08:05 elasticsearch-6.2.2
-rw-r--r--.  1 root  root   29049540 May  7 01:01 elasticsearch-6.2.2.tar.gz
drwxr-xr-x.  8 uucp    143      4096 Jul 21  2017 jdk8
drwxrwxrwx. 12  1000  1000      4096 Feb 16 11:20 kibana-6.2.2-linux-x86_64
-rw-r--r--.  1 root  root   83415765 May  8 00:24 kibana-6.2.2-linux-x86_64.tar.gz
drwxrwxrwx. 12 root  root       4096 May  8 20:20 logstash-6.2.2
-rw-r--r--.  1 root  root  139464029 May  8 00:52 logstash-6.2.2.tar.gz
drwxr-xr-x. 10 mysql mysql      4096 Jun 25 19:57 mysql
-rw-r--r--.  1 root  root  641127384 Oct 31  2017 mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
drwxrwxr-x.  6 root  root       4096 Dec 18  2015 redis-3.0.6
-rw-r--r--.  1 root  root    1372648 May 25  2017 redis-3.0.6.tar.gz
[[email protected] software]# pwd
/usr/local/software
[[email protected] software]# rpm -i Atlas-2.2.1.el6.x86_64.rpm
[[email protected] software]# 

此時Atlas被安裝到了/usr/local/mysql-proxy當中。

PS、這是我不喜歡rmp安裝的原因,不能自定義。


配置Atlas

Atlas的配置有中文註釋,非常方便。

只需要主要下面幾個吧。而我只修改了紅色部分。

#Atlas後端連線的MySQL主庫的IP和埠,可設定多項,用逗號分隔

proxy-backend-addresses = 127.0.0.1:3306         #因為代理和主庫是一套,所有127.0.0.1也是可以的

#Atlas後端連線的MySQL從庫的IP和埠,@後面的數字代表權重,用來作負載均衡,若省略則預設為1,可設定多項,用逗號分隔

proxy-read-only-backend-addresses = 192.168.160.131:3306@1

#使用者名稱與其對應的加密過的MySQL密碼,密碼使用PREFIX/bin目錄下的加密程式encrypt加密。使用者密碼所有主庫和從庫都必須一至

pwds = root:DAJnl8cVzy8=

#Atlas監聽的工作介面IP和埠

proxy-address = 0.0.0.0:1234

[[email protected] bin]# ./encrypt root
DAJnl8cVzy8=

啟動Atlas

./mysql-proxyd test start

./mysql-proxyd test stop

[[email protected] bin]# pwd
/usr/local/software/mysql/bin
[[email protected] bin]#  ./mysql -h127.0.0.1 -P1234 -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.81-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| rorodb             |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> 


為了測試讀寫分離我需要開啟配置

#SQL日誌的開關,可設定為OFF、ON、REALTIME,OFF代表不記錄SQL日誌,ON代表記錄SQL日誌,REALTIME代表記錄SQL日誌且實時寫入磁碟,預設為OFF
sql-log = REALTIME

這樣在執行插入查詢的時候就能在sql_test.log裡清晰的看到被sql被分配到master還是slave從而實現了讀寫分離



參考資料

    https://github.com/Qihoo360/Atlas/blob/master/README_ZH.md