1. 程式人生 > 其它 >ProxySQL Cluster 叢集搭建步驟

ProxySQL Cluster 叢集搭建步驟

環境

proxysql-1:192.168.20.202
proxysql-2:192.168.20.203

均採用yum方式安裝

# cat <<EOF | tee /etc/yum.repos.d/proxysql.repo
[proxysql_repo]
name= ProxySQL YUM repository
baseurl=https://repo.proxysql.com/ProxySQL/proxysql-2.4.x/centos/\$releasever
gpgcheck=0
gpgkey=https://repo.proxysql.com/ProxySQL/repo_pub_key
EOF

# yum -y install proxysql

# systemctl start proxysql.service
# systemctl stop proxysql.service
# systemctl enable proxysql.service
# 安裝MySQL 客戶端

wget https://repo.mysql.com//mysql80-community-release-el7-5.noarch.rpm
yum localinstall -y mysql80-community-release-el7-5.noarch.rpm 
yum -y install mysql-community-client # 8.0.29版本
rpm包方式安裝,需要先安裝mysql-community-libs-compat,否則會報如下錯誤:yum -y install mysql-community-libs-compat
# rpm -ivh perl-DBD-MySQL-4.023-6.el7.x86_64.rpm
錯誤:依賴檢測失敗:
        libmysqlclient.so.18()(64bit) 被 perl-DBD-MySQL-4.023-6.el7.x86_64 需要
        libmysqlclient.so.18(libmysqlclient_18)(64bit) 被 perl-DBD-MySQL-4.023-6.el7.x86_64 需要

# 批量安裝命令:ls *.rpm | xargs rpm -ivh
# 分步安裝命令
rpm -ivh trousers-0.3.14-2.el7.x86_64.rpm
rpm -ivh nettle-2.7.1-9.el7_9.x86_64.rpm
rpm -ivh perl-Net-Daemon-0.48-5.el7.noarch.rpm
rpm -ivh perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm
rpm -ivh perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm
rpm -ivh perl-IO-Compress-2.061-2.el7.noarch.rpm
rpm -ivh perl-Data-Dumper-2.145-3.el7.x86_64.rpm
rpm -ivh perl-PlRPC-0.2020-14.el7.noarch.rpm
rpm -ivh gnutls-3.3.29-9.el7_6.x86_64.rpm
rpm -ivh perl-DBI-1.627-4.el7.x86_64.rpm
rpm -ivh perl-DBD-MySQL-4.023-6.el7.x86_64.rpm
rpm -ivh proxysql-2.4.1-1-centos7.x86_64.rpm

叢集的搭建有很多種方式,如1+1+1的方式,還可以(1+1)+1的方式。

這裡採用較簡單的(1+1)+1,即先將兩個節點作為叢集啟動,後續若有其他節點選擇性加入的方式

資料庫方面的操作

資料庫環境
master mysql: 192.168.20.200
slave mysql: 192.168.20.201

只配置同步test庫,自帶的mysql庫沒有同步,因此主庫新增賬號密碼,從庫也得新增一遍

# 新增一個監控用賬號(能監控到從庫的複製情況) ,在這裡要麼主從庫自帶的mysql庫需要同步,若是沒同步則每個MySQL都需要做這一步操作

create user 'proxysql'@'192.168.20.%' identified  with mysql_native_password by 'iD!^^EjU#Yxr5$p';
GRANT USAGE,process,replication slave,replication client ON *.* TO 'proxysql'@'192.168.20.%' with grant option;
flush privileges;

# 注意:這裡的賬號密碼要和下面我們在proxysql裡面的mysql_variables段的賬號密碼配置的一樣

# 新增一個程式連線用的賬號

create user 'sbuser'@'192.168.20.%' identified  with mysql_native_password by 'iD!^^EjU#Yxr5$p';
GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'sbuser'@'192.168.20.%' with grant option;
flush privileges;

此時MySQL主從庫中有如下賬號資訊

超級使用者:root
主從庫同步使用者:repl (從庫沒這個使用者)
普通使用者:sbuser
proxysql監控MySQL主從情況使用的使用者:proxysql

叢集搭建

1.更改所有例項的配置檔案

# cp /etc/proxysql.cnf /etc/proxysql.cnf.bak
# vim /etc/proxysql.cnf

admin_variables=
{
    admin_credentials="admin:admin;cluster_20X:123456"
    mysql_ifaces="0.0.0.0:6032"
    cluster_username="cluster_20X"
    cluster_password="123456"
    cluster_check_interval_ms=200
    cluster_check_status_frequency=100
    cluster_mysql_query_rules_save_to_disk=true
    cluster_mysql_servers_save_to_disk=true
    cluster_mysql_users_save_to_disk=true
    cluster_proxysql_servers_save_to_disk=true
    cluster_mysql_query_rules_diffs_before_sync=3
    cluster_mysql_servers_diffs_before_sync=3
    cluster_mysql_users_diffs_before_sync=3
    cluster_proxysql_servers_diffs_before_sync=3
}
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:6033"
    default_schema="information_schema"
    stacksize=1048576
    server_version="5.7.22"
    connect_timeout_server=3000
    monitor_username="proxysql"
    monitor_password="iD!^^EjU#Yxr5$p"
    monitor_history=600000
    monitor_connect_interval=60000
    monitor_ping_interval=10000
    monitor_read_only_interval=1500
    monitor_read_only_timeout=500
    ping_interval_server_msec=120000
    ping_timeout_server=500
    commands_stats=true
    sessions_sort=true
    connect_retries_on_failure=10
}
proxysql_servers =
(
    {
        hostname="192.168.20.202"
        port=6032
        comment="proxysql-202"
    },
    {
        hostname="192.168.20.203"
        port=6032
        comment="proxysql-203"
    }
)

# 其餘的配置資訊保持不動

特別注意:如果存在"proxysql.db"檔案(在/var/lib/proxysql目錄下),則ProxySQL服務只有在第一次啟動時才會去讀取proxysql.cnf檔案並解析;後面啟動會就不會讀取proxysql.cnf檔案了!如果想要讓proxysql.cnf檔案裡的配置在重啟proxysql服務後生效(即想要讓proxysql重啟時讀取並解析proxysql.cnf配置檔案),則需要先刪除/var/lib/proxysql/proxysql.db資料庫檔案,然後再重啟proxysql服務。這樣就相當於初始化啟動proxysql服務了,會再次生產一個純淨的proxysql.db資料庫檔案(如果之前配置了proxysql相關路由規則等,則就會被抹掉)。


# 倆proxysql節點啟動proxysql程序
systemctl start proxysql

# 登入進去(無特殊說明均在192.168.20.202上操作)
/usr/bin/mysql -uadmin -padmin -h 127.0.0.1 -P 6032

# 觀察叢集狀況

mysql> select * from proxysql_servers;
+----------------+------+--------+--------------+
| hostname       | port | weight | comment      |
+----------------+------+--------+--------------+
| 192.168.20.202 | 6032 | 0      | proxysql-202 |
| 192.168.20.203 | 6032 | 0      | proxysql-203 |
+----------------+------+--------+--------------+
2 rows in set (0.00 sec)

mysql> select * from stats_proxysql_servers_metrics;
+----------------+------+--------+--------------+------------------+----------+---------------+---------+------------------------------+----------------------------+
| hostname       | port | weight | comment      | response_time_ms | Uptime_s | last_check_ms | Queries | Client_Connections_connected | Client_Connections_created |
+----------------+------+--------+--------------+------------------+----------+---------------+---------+------------------------------+----------------------------+
| 192.168.20.203 | 6032 | 0      | proxysql-203 | 2                | 683      | 2593          | 0       | 0                            | 0                          |
| 192.168.20.202 | 6032 | 0      | proxysql-202 | 2                | 686      | 2923          | 0       | 0                            | 0                          |
+----------------+------+--------+--------------+------------------+----------+---------------+---------+------------------------------+----------------------------+
2 rows in set (0.00 sec)

mysql> select hostname,port,comment,Uptime_s,last_check_ms from stats_proxysql_servers_metrics;  
+----------------+------+--------------+----------+---------------+
| hostname       | port | comment      | Uptime_s | last_check_ms |
+----------------+------+--------------+----------+---------------+
| 192.168.20.203 | 6032 | proxysql-203 | 1266     | 15622         |
| 192.168.20.202 | 6032 | proxysql-202 | 1266     | 18806         |
+----------------+------+--------------+----------+---------------+
2 rows in set (0.00 sec)

mysql> select hostname,name,checksum,updated_at from stats_proxysql_servers_checksums;
+----------------+-------------------+--------------------+------------+
| hostname       | name              | checksum           | updated_at |
+----------------+-------------------+--------------------+------------+
| 192.168.20.203 | admin_variables   | 0xF8E25295F13135A0 | 1653448141 |
| 192.168.20.203 | mysql_query_rules | 0x0000000000000000 | 1653448141 |
| 192.168.20.203 | mysql_servers     | 0xE5A163C3AD6BD3A7 | 1653448141 |
| 192.168.20.203 | mysql_users       | 0x0000000000000000 | 1653448141 |
| 192.168.20.203 | mysql_variables   | 0x3ECA231EE02626C9 | 1653448141 |
| 192.168.20.203 | proxysql_servers  | 0x75C8DA71CAF992E0 | 1653448141 |
| 192.168.20.202 | admin_variables   | 0xF8E25295F13135A0 | 1653448141 |
| 192.168.20.202 | mysql_query_rules | 0x0000000000000000 | 1653448141 |
| 192.168.20.202 | mysql_servers     | 0xE5A163C3AD6BD3A7 | 1653448141 |
| 192.168.20.202 | mysql_users       | 0x0000000000000000 | 1653448141 |
| 192.168.20.202 | mysql_variables   | 0x3ECA231EE02626C9 | 1653448141 |
| 192.168.20.202 | proxysql_servers  | 0x75C8DA71CAF992E0 | 1653448141 |
+----------------+-------------------+--------------------+------------+
12 rows in set (0.00 sec)


# 觀察ProxySQL叢集中例項之間的資料同步

# 原有資料
mysql> select * from mysql_servers;
Empty set (0.00 sec)

# 新增一個後端MySQL主機資訊
mysql> insert into mysql_servers(hostgroup_id,hostname,port,comment) values (10,'192.168.20.200',3306,'master_mysql');

# 該主機上檢視
mysql> select * from mysql_servers;
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| hostgroup_id | hostname       | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment      |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| 10           | 192.168.20.200 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | master_mysql |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
1 row in set (0.00 sec)

# 此時到另一臺proxysql主機上檢視沒有這個後端MySQL主機資訊

# 持久化,並載入到執行環境中
mysql> save mysql servers to disk; 
mysql> load mysql servers to runtime; 

# 再次到另一臺proxysql主機上檢視,可以看到新插入的資料,發現有這個後端MySQL主機資訊,已經被更新到192.168.20.203例項中的memory和runtime環境中。
mysql> select * from mysql_servers;
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| hostgroup_id | hostname       | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment      |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| 10           | 192.168.20.200 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | master_mysql |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
1 row in set (0.00 sec)

mysql> select * from runtime_mysql_servers;
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| hostgroup_id | hostname       | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment      |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| 10           | 192.168.20.200 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | master_mysql |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
1 row in set (0.00 sec)

# 檢視另一臺proxysql主機日誌,會看到同步的具體資訊
# tail -n 30 /var/lib/proxysql/proxysql.log
2022-05-25 11:03:27 [INFO] Cluster: Loading to runtime MySQL Servers from peer 192.168.20.202:6032
2022-05-25 11:03:27 [INFO] Dumping mysql_servers_incoming
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| hostgroup_id | hostname       | port | gtid_port | weight | status | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment      |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| 10           | 192.168.20.200 | 3306 | 0         | 1      | 0      | 0           | 1000            | 0                   | 0       | 0              | master_mysql |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
2022-05-25 11:03:27 [INFO] Dumping mysql_servers LEFT JOIN mysql_servers_incoming
+-------------+--------------+----------+------+
| mem_pointer | hostgroup_id | hostname | port |
+-------------+--------------+----------+------+
+-------------+--------------+----------+------+
2022-05-25 11:03:27 [INFO] Dumping mysql_servers JOIN mysql_servers_incoming
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+-------------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| hostgroup_id | hostname       | port | gtid_port | weight | status | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment      | mem_pointer | gtid_port | weight | status | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment      |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+-------------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| 10           | 192.168.20.200 | 3306 | 0         | 1      | 0      | 0           | 1000            | 0                   | 0       | 0              | master_mysql | 0           | 0         | 1      | 0      | 0           | 1000            | 0                   | 0       | 0              | master_mysql |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+-------------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
2022-05-25 11:03:27 [INFO] Creating new server in HG 10 : 192.168.20.200:3306 , gtid_port=0, weight=1, status=0
2022-05-25 11:03:27 [INFO] New mysql_group_replication_hostgroups table
2022-05-25 11:03:27 [INFO] New mysql_galera_hostgroups table
2022-05-25 11:03:27 [INFO] New mysql_aws_aurora_hostgroups table
2022-05-25 11:03:27 [INFO] Checksum for table mysql_servers is 6785001030445135624
2022-05-25 11:03:27 [INFO] MySQL_HostGroups_Manager::commit() locked for 1ms
2022-05-25 11:03:27 [INFO] Cluster: Saving to disk MySQL Servers from peer 192.168.20.202:6032
2022-05-25 11:03:27 [INFO] Cluster: checksum for admin_variables from peer 192.168.20.203:6032 matches with local checksum 0xF8E25295F13135A0, we won't sync.
2022-05-25 11:03:27 [INFO] Cluster: detected a new checksum for mysql_servers from peer 192.168.20.203:6032, version 2, epoch 1653447806, checksum 0xE5A163C3AD6BD3A7 . Not syncing yet ...
2022-05-25 11:03:27 [INFO] Cluster: checksum for mysql_servers from peer 192.168.20.203:6032 matches with local checksum 0xE5A163C3AD6BD3A7 , we won't sync.

注意:資料差異檢查是根據runtime進行檢查的,只對memory和disk進行更改,並不觸發同步操作。

此時,兩節點的proxysql cluster叢集搭建完畢

後續若需要加入第三個proxysql節點到叢集中的操作

這裡以安裝有MySQL master主機節點為例進行演示(也可以使用新主機)

192.168.20.200為全新的節點,使用conf檔案啟動,不使用更改global_variable的方式加入叢集(操作複雜且容器出錯)。

# 先安裝好proxysql軟體,不啟動(mysql客戶端已安裝,此時不用再安裝,若未安裝也需要安裝這個)

# 修改 /etc/proxysql.cnf (修改了admin_variables段、proxysql_servers段、mysql_variables段) 【要和proxysql cluster裡面的其他節點執行配置一樣,叢集名稱、各種賬號密碼要一致】

# cp /etc/proxysql.cnf /etc/proxysql.cnf.bak
# vim /etc/proxysql.cnf

admin_variables=
{
    admin_credentials="admin:admin;cluster_20X:123456"
    mysql_ifaces="0.0.0.0:6032"
    cluster_username="cluster_20X"
    cluster_password="123456"
    cluster_check_interval_ms=200
    cluster_check_status_frequency=100
    cluster_mysql_query_rules_save_to_disk=true
    cluster_mysql_servers_save_to_disk=true
    cluster_mysql_users_save_to_disk=true
    cluster_proxysql_servers_save_to_disk=true
    cluster_mysql_query_rules_diffs_before_sync=3
    cluster_mysql_servers_diffs_before_sync=3
    cluster_mysql_users_diffs_before_sync=3
    cluster_proxysql_servers_diffs_before_sync=3
}
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:6033"
    default_schema="information_schema"
    stacksize=1048576
    server_version="5.7.22"
    connect_timeout_server=3000
    monitor_username="proxysql"
    monitor_password="iD!^^EjU#Yxr5$p"
    monitor_history=600000
    monitor_connect_interval=60000
    monitor_ping_interval=10000
    monitor_read_only_interval=1500
    monitor_read_only_timeout=500
    ping_interval_server_msec=120000
    ping_timeout_server=500
    commands_stats=true
    sessions_sort=true
    connect_retries_on_failure=10
}
proxysql_servers =
(
    {
        hostname="192.168.20.202"
        port=6032
        comment="proxysql-202"
    },
    {
        hostname="192.168.20.203"
        port=6032
        comment="proxysql-203"
    }
)

# 其餘的配置資訊保持不動

# 在新節點裡啟動proxysql後, 可以看下 192.168.20.200 的 /var/lib/proxysql/proxysql.log 日誌裡面, 192.168.20.200 這個新加入的節點 會去其它節點拉取配置(但是其它節點不知道這個192.168.20.200到底是什麼身份的存在)。

# 然後,我們在老的proxysql的任一節點上,將 192.168.20.200 這個新節點加入到叢集環境:

# 插入一條proxysql_server的資訊
insert into proxysql_servers(hostname,port,comment ) values('192.168.20.200',6032,'bak proxysql') ;

# 載入到runtime,並把配置持久化
load proxysql servers to runtime;
save proxysql servers to disk;

# 查下結果是否正常

mysql> select * from proxysql_servers;
+----------------+------+--------+--------------+
| hostname       | port | weight | comment      |
+----------------+------+--------+--------------+
| 192.168.20.202 | 6032 | 0      | proxysql-202 |
| 192.168.20.203 | 6032 | 0      | proxysql-203 |
| 192.168.20.200 | 6032 | 0      | bak proxysql |
+----------------+------+--------+--------------+
3 rows in set (0.00 sec)

mysql> select * from runtime_proxysql_servers ;
+----------------+------+--------+--------------+
| hostname       | port | weight | comment      |
+----------------+------+--------+--------------+
| 192.168.20.200 | 6032 | 0      | bak proxysql |
| 192.168.20.203 | 6032 | 0      | proxysql-203 |
| 192.168.20.202 | 6032 | 0      | proxysql-202 |
+----------------+------+--------+--------------+
3 rows in set (0.00 sec)

經過上面的步驟後, 192.168.20.200 就完成加叢集的操作了。