MySQL 主從架構之讀寫分離

MySQL 主從架構之讀寫分離

本實驗在配置好的主從架構基礎上實現

實驗準備

  • 主機角色劃分:
MySQL主節點 :192.168.50.9       node9
MySQL從節點 :192.168.50.10      node10
MySQL從節點 :192.168.50.16      node16
R/W Splitter:172.16.50.17(外網)    192.168.50.17(內網)     node17
  • 拓撲圖如下
    這裏寫圖片描述

使用ProxySQL實現讀寫分離

注意:ProxySQL可以同時實現對多個組的讀寫分離調度,本實驗僅測試對一個組進行讀寫分離調度

  1. 安裝proxysql程序
yum install ./proxysql-1.3.6-1-centos7.x86_64.rpm
  1. 修改配置文件
cp /etc/proxysql.cnf{,.bak}
vim /etc/proxysql.cnf
#修改mysql_variables配置段
mysql_variables=
{
        threads=4
        max_connections=2048
        default_query_delay=0
        default_query_timeout=36000000
        have_compress=true
        poll_timeout=2000
        interfaces="0.0.0.0:3306;/tmp/proxysql.sock"
        default_schema="mydb"
        stacksize=1048576
        server_version="5.5.30"
        connect_timeout_server=3000
        monitor_history=600000
        monitor_connect_interval=60000
        monitor_ping_interval=10000
        monitor_read_only_interval=1500
        monitor_read_only_timeout=500
        ping_interval_server=120000
        ping_timeout_server=500
        commands_stats=true
        sessions_sort=true
        connect_retries_on_failure=10
}
#修改mysql_servers配置段
mysql_servers =
(
        {
                address = "192.168.50.9" # no default, required . If portis 0 , address is interpred as a Unix Socket Domain
                port = 3306           # no default, required . If port is0 , address is interpred as a Unix Socket Domain
                hostgroup = 0           # no default, required
                status = "ONLINE"     # default: ONLINE
                weight = 1            # default: 1
                compression = 0       # default: 0
                max_replication_lag = 200  # default 0 . If greater than 0 and replication lag passes such threshold, the server is shunned
        },
        {
                address = "192.168.50.10" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
                port = 3306           # no default, required . If port is0 , address is interpred as a Unix Socket Domain
                hostgroup = 1           # no default, required
                status = "ONLINE"     # default: ONLINE
                weight = 1            # default: 1
                compression = 0       # default: 0
                max_replication_lag = 500  # default 0 . If greater than 0 and replication lag passes such threshold, the server is shunned
        },
        {
                address = "192.168.50.16" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
                port = 3306           # no default, required . If port is0 , address is interpred as a Unix Socket Domain
                hostgroup = 1           # no default, required
                status = "ONLINE"     # default: ONLINE
                weight = 1            # default: 1
                compression = 0       # default: 0 
                max_replication_lag = 500  # default 0 . If greater than 0 and replication lag passes such threshold, the server is shunned
        }
)
#修改mysql_user配置段
mysql_users:
(
        {
                username = "myadmin" # no default , required
                password = "mypass" # default: ''
                default_hostgroup = 0 # default: 0
                active = 1            # default: 1
                default_schema="mydb"
        }
)
#定義mysql_query_rules配置段,定義查詢規則可實現防火牆的功能,如:對未設置 WHERE 子句的 SELECT 語句,將該危險操作調度到一個不存在的組,從而避免危險操作。
mysql_query_rules:
(
)
#定義讀寫分離配置段
mysql_replication_hostgroups=
(
        {
                writer_hostgroup=0
                reader_hostgroup=1
                comment="test repl 1"
        }
)
  1. 在node9(MySQL主節點)服務器上爲ProxySQL代理節點建立用戶並授權

注意:修改主節點數據庫中的數據,從節點會自動同步修改數據

GRANT ALL ON *.* TO 'myadmin'@'192.168.50.%' IDENTIFIED BY'mypass';
FLUSH PRIVILEGES;
  1. 在node17啓動proxysql服務並連接本地mysql服務
service proxysql start:ss -tnl
#使用預先建立的賬號密碼登錄node9服務器
mysql -umyadmin -pmypass -h192.168.50.17
#登錄成功後會顯示下面提示信息
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.30 (ProxySQL)

讀寫分離操作至此已經完成。

  1. 在node17登錄proxysql的內置管理接口
mysql -uadmin -hlocalhost -padmin -S '/tmp/proxysql_admin.sock'
#登錄成功顯示如下內容
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.30 (ProxySQL Admin Module)

執行下面命令,查看proxysql的代理信息

MySQL [(none)]> SHOW DATABASES;
+-----+---------+-------------------------------+
| seq | name    | file                          |
+-----+---------+-------------------------------+
| 0   | main    |                               |
| 2   | disk    | /var/lib/proxysql/proxysql.db |
| 3   | stats   |                               |
| 4   | monitor |                               |
+-----+---------+-------------------------------+
MySQL [(none)]> use monitor;
MySQL [(none)]> SELECT * FROM mysql_servers;
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname      | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 0            | 192.168.50.9  | 3306 | ONLINE | 1      | 0           | 1000            | 200                 | 0       | 0              |         |
| 1            | 192.168.50.10 | 3306 | ONLINE | 1      | 0           | 1000            | 500                 | 0       | 0              |         |
| 1            | 192.168.50.16 | 3306 | ONLINE | 1      | 0           | 1000            | 500                 | 0       | 0              |         |
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
MySQL [monitor]> SELECT * FROM runtime_mysql_replication_hostgroups;
+------------------+------------------+-------------+
| writer_hostgroup | reader_hostgroup | comment     |
+------------------+------------------+-------------+
| 0                | 1                | test repl 1 |
+------------------+------------------+-------------+
  1. ProxySQL支持運行時修改代理配置,可以在內置管理接口登錄後修改數據庫內容實現增刪組成員,定義讀寫分離組等配置。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章