percona ProxySQL

#################

源碼安裝說明:https://github.com/sysown/proxysql/blob/v2.x/INSTALL.md

源碼地址:https://github.com/sysown/proxysql

 安裝依賴:

Shortcut commands for installing pre-requisites:

# RHEL / CentOS < 7:
yum install -y automake bzip2 cmake make g++ gcc git openssl openssl-devel gnutls libtool patch libuuid-devel

# CentOS 7.4+ / Fedora 28+:
yum install -y automake bzip2 cmake make gcc-c++ gcc git openssl openssl-devel gnutls gnutls-devel libtool patch libuuid-devel

# Debian / Ubuntu Based:
apt-get install -y automake bzip2 cmake make g++ gcc git openssl libssl-dev libgnutls28-dev libtool patch uuid-dev

 

 下載源碼代碼:

git clone  https://github.com/sysown/proxysql

編譯安裝:

cd proxysql
make
make install

 

安裝後結果:

數據文件:datadir="/var/lib/proxysql"
錯誤日誌:errorlog="/var/lib/proxysql/proxysql.log"
配置文件:/etc/proxysql.cnf
賬號密碼:admin:admin
管理的端口號:6032

 

 

 

 

 wget https://downloads.percona.com/downloads/proxysql2/proxysql2-2.5.1/source/tarball/proxysql2-2.5.1.tar.gz

 

wget https://github.com/sysown/proxysql/archive/refs/tags/2.5.2.tar.gz

 

 

Percona ProxySQL是Percona公司基於ProxySQL二次開發的MySQL代理工具,相比於原生的ProxySQL,在功能和性能上都有一定的優勢:

  1. 支持更多的負載均衡算法:Percona ProxySQL支持5種負載均衡算法,包括了原生ProxySQL的round-robin、first-available、lowest-latency和random算法,還新增了自適應負載均衡算法。

  2. 更好的SQL過濾和轉發功能:Percona ProxySQL在SQL過濾和轉發功能方面更加強大,支持根據多種條件過濾和轉發SQL查詢,包括SQL語句、客戶端IP、用戶名、數據庫名、表名等條件。

  3. 更高的性能和穩定性:Percona ProxySQL在性能和穩定性方面做了一些優化和改進,支持更高的併發連接數和更快的查詢速度,並且在故障轉移和節點管理方面更加穩定可靠。

  4. 更好的監控和管理功能:Percona ProxySQL提供了更多的監控和管理功能,包括查詢日誌、慢查詢日誌、連接數統計、節點狀態監控等,可以更方便地對MySQL集羣進行管理和維護。

總之,Percona ProxySQL相比於原生的ProxySQL在功能和性能上都有一定的優勢,特別是在負載均衡算法、SQL過濾和轉發、性能和穩定性、監控和管理等方面更加強大和靈活。如果需要一個高性能、高可用性的MySQL代理工具,Percona ProxySQL是一個不錯的選擇。

ProxySQL是一款高性能、高可用性的SQL代理服務器,可以作爲數據庫中間件,在MySQL集羣中提供負載均衡、故障轉移、讀寫分離等功能。ProxySQL的配置包含多個方面,下面是一個簡單的ProxySQL配置示例,介紹了一些基本配置項:

 

# 啓用ProxySQL的日誌功能
log_level = debug

# 監聽的本地IP地址和端口
admin_variables =
{
    admin_credentials = 'admin:admin'
    mysql_ifaces = '0.0.0.0:6032'
}

# 集羣節點的配置
mysql_servers =
{
    # 讀寫分離的主節點
    {   
        hostgroup_id = 10
        hostname = 'master.example.com'
        port = 3306
        weight = 100
        max_connections = 1000
    },
    # 讀寫分離的從節點
    {
        hostgroup_id = 20
        hostname = 'slave.example.com'
        port = 3306
        weight = 100
        max_connections = 1000
    },
    # 只讀節點
    {
        hostgroup_id = 30
        hostname = 'readonly.example.com'
        port = 3306
        weight = 100
        max_connections = 1000
    }
}

# 配置SQL查詢規則
mysql_query_rules =
(
    # 根據查詢類型和表名選擇不同的節點
    {
        rule_id = 10
        active = 1
        match_pattern = '^SELECT .* FROM table1'
        destination_hostgroup = IFNULL(nullif(substring_index(user(),'@',-1),''),10)
        apply = 1
    },
    {
        rule_id = 20
        active = 1
        match_pattern = '^SELECT .* FROM table2'
        destination_hostgroup = IFNULL(nullif(substring_index(user(),'@',-1),''),20)
        apply = 1
    },
    # 根據客戶端IP地址選擇不同的節點
    {
        rule_id = 30
        active = 1
        match_pattern = '.*'
        destination_hostgroup = IF(client_ip IN ('192.168.1.1','192.168.1.2'), 30, 10)
        apply = 1
    }
)

 

這個配置文件包含了ProxySQL的一些基本配置項:

  1. log_level:指定ProxySQL的日誌級別,可以是debug、info、notice、warning、error、critical或alert。

  2. admin_variables:指定ProxySQL監聽的IP地址和端口,用於管理ProxySQL。這裏監聽了所有網卡的6032端口,並使用了默認的管理賬號admin/admin。

  3. mysql_servers:指定ProxySQL管理的MySQL節點,包括主節點、從節點和只讀節點,每個節點需要指定hostgroup_id、hostname、port、weight和max_connections等參數。

  4. mysql_query_rules:指定ProxySQL的SQL查詢規則,可以根據查詢類型、表名、客戶端IP地址等條件選擇不同的MySQL節點,包括rule_id、active、match_pattern、destination_hostgroup和apply等參數。

這只是一個簡單的ProxySQL配置

 

 

 

在ProxySQL的配置文件中,可以通過mysql_servers和mysql_replication_hostgroups參數來實現讀寫分離。

mysql_servers參數定義了所有MySQL節點的信息,包括它們的IP地址、端口、權重和狀態等。其中,權重用於指定節點在負載均衡時的權重值,狀態用於指定節點的可用性。例如:

 

mysql_servers =
{
    {   'hostgroup': 10,
        'hostname': 'db1.example.com',
        'port': 3306,
        'weight': 100,
        'max_connections': 1000,
        'status': 'ONLINE',
        'compression': 1
    },
    {   'hostgroup': 10,
        'hostname': 'db2.example.com',
        'port': 3306,
        'weight': 100,
        'max_connections': 1000,
        'status': 'ONLINE',
        'compression': 1
    },
    {   'hostgroup': 20,
        'hostname': 'db3.example.com',
        'port': 3306,
        'weight': 100,
        'max_connections': 1000,
        'status': 'ONLINE',
        'compression': 1
    },
    {   'hostgroup': 20,
        'hostname': 'db4.example.com',
        'port': 3306,
        'weight': 100,
        'max_connections': 1000,
        'status': 'ONLINE',
        'compression': 1
    }
}

mysql_replication_hostgroups參數定義了所有MySQL節點的複製拓撲結構,包括主節點和從節點的信息。其中,主節點的hostgroup_id爲10,從節點的hostgroup_id爲20。例如:

 

mysql_replication_hostgroups =
{
    '10': {'hostgroup': 10},
    '20': {'hostgroup': 20},
}

通過以上兩個參數的配合,可以實現讀寫分離。在ProxySQL中,讀操作使用SELECT語句,寫操作使用INSERT、UPDATE、DELETE語句。可以通過proxySQL規則來指定不同操作的處理方式。例如:

 

mysql_query_rules =
(
    {   'rule_id': 1,
        'active': 1,
        'match_pattern': '^SELECT.*FOR UPDATE|.*LOCK IN SHARE MODE$',
        'destination_hostgroup': 10,
        'apply': 1
    },
    {   'rule_id': 2,
        'active': 1,
        'match_pattern': '^SELECT.*',
        'destination_hostgroup': 20,
        'apply': 1
    },
    {   'rule_id': 3,
        'active': 1,
        'match_pattern': '^INSERT.*|.*UPDATE.*|.*DELETE.*',
        'destination_hostgroup': 10,
        'apply': 1
    }
)

上面的規則指定了SELECT語句使用hostgroup 20的節點來處理,INSERT、UPDATE和DELETE語句使用hostgroup 10的節點來處理,從而實現了讀寫分離的效果。在這個例子中,hostgroup 20的節點爲只讀節點,hostgroup 10的節點爲讀寫節點。

 

mysql_replication_hostgroups是ProxySQL中用於配置MySQL主從複製的組別,它是一個數組,每個元素都表示一個組。ProxySQL使用這些組別將讀取流量分發到正確的服務器。每個組別都包含一個或多個MySQL主庫服務器和一個或多個MySQL從庫服務器。通常,一個組只包含一個主服務器和多個從服務器。

每個組都有一個唯一的ID和名稱,以及其他屬性,例如:read_only、write_only、max_replication_lag等。

具體來說,mysql_replication_hostgroups在ProxySQL中的作用如下:

  1. 讀寫分離:通過將讀取流量分發到正確的服務器,可以實現讀寫分離。ProxySQL通過對mysql_replication_hostgroups的配置來控制讀寫請求的路由方式。

  2. 高可用性:使用mysql_replication_hostgroups可以在一個組中包含多個主從服務器,以實現高可用性。如果主服務器失效,則可以使用從服務器來替代。

  3. 負載均衡:ProxySQL使用mysql_replication_hostgroups對流量進行分發,以確保所有服務器都得到充分利用,以實現負載均衡。

下面是一個mysql_replication_hostgroups的示例配置:

 

mysql_replication_hostgroups=(
{
    writer_hostgroup=10,
    reader_hostgroup=20,
    comment="MySql Host Group 1",
    backup_writer_hostgroup=30,
    offline_hostgroup=40,
    max_writers=2,
    writer_is_also_reader=1,
    check_type="read_only",
    max_replication_lag=30,
    use_slave_connection=0,
    max_connections_behind=100
},
{
    writer_hostgroup=20,
    reader_hostgroup=10,
    comment="MySql Host Group 2",
    backup_writer_hostgroup=30,
    offline_hostgroup=40,
    max_writers=2,
    writer_is_also_reader=1,
    check_type="read_only",
    max_replication_lag=30,
    use_slave_connection=0,
    max_connections_behind=100
})

該配置中有兩個組別,每個組別都有一個讀服務器和一個寫服務器,組別10表示寫操作的主服務器,組別20表示讀操作的從服務器。

其中,writer_hostgroup指定了寫服務器的組別ID,reader_hostgroup指定了讀服務器的組別ID,comment是對該組的說明,backup_writer_hostgroup指定了備份寫服務器的組別ID,offline_hostgroup指定了該組宕機時應使用的組別ID,max_writers指定了在寫組中可以同時有多少個主服務器,writer_is_also_reader指定了在寫組中的主服務器是否也可以充當讀服務器,check_type指定了用於檢查從服務器的連接類型,max_replication_lag指定了從服務器的最大延遲時間,use_slave_connection指定了是否使用從服務器連接來檢查從服務器的健康狀況,max_connections_behind指定了從服務器可以落後於主服務器的最大連接數

 

ProxySQL是一個開源的高性能SQL代理,可以在數據庫和應用程序之間充當中間層。它提供了很多功能,包括負載均衡、故障轉移、自動故障檢測等。其中一個最常用的功能是讀寫分離,可以通過ProxySQL來優化數據庫的讀寫操作。

以下是一些最佳實踐,可以幫助您最佳地利用ProxySQL來進行讀寫分離:

  1. 配置ProxySQL的讀寫分離規則

ProxySQL使用規則來將讀請求路由到只讀節點,將寫請求路由到主節點。您可以使用ProxySQL的配置文件來設置這些規則,或者使用ProxySQL提供的命令行工具和API。

例如,您可以使用以下命令來添加一個規則,將所有SELECT語句路由到只讀節點:

INSERT INTO mysql_query_rules (rule_id, active, match_pattern, destination_hostgroup) VALUES (1, 1, '^SELECT.*', 2);
  1. 配置主節點和只讀節點的hostgroup

在ProxySQL中,您需要爲主節點和只讀節點配置hostgroup。主節點應該配置爲hostgroup 0,只讀節點可以配置爲hostgroup 1或更高的數字。

例如,您可以使用以下命令將主節點添加到hostgroup 0:

INSERT INTO mysql_servers (hostname, port, hostgroup_id, max_connections) VALUES ('master-db.example.com', 3306, 0, 1000);

然後,您可以使用以下命令將只讀節點添加到hostgroup 1:

INSERT INTO mysql_servers (hostname, port, hostgroup_id, max_connections) VALUES ('read-db1.example.com', 3306, 1, 500);
  1. 配置連接池和線程池

ProxySQL有兩個重要的配置參數:連接池和線程池。連接池定義了與數據庫建立的連接數,而線程池定義了同時處理的線程數。

您需要根據您的數據庫負載和硬件資源來配置這些參數。通常,您應該將連接池和線程池設置爲適當的值,以充分利用您的硬件資源,並防止過度使用數據庫服務器。

例如,您可以使用以下命令設置最大連接數和線程數:

UPDATE global_variables SET variable_value='10000' WHERE variable_name='mysql-max_connections';
UPDATE global_variables SET variable_value='500' WHERE variable_name='mysql-thread_pool_size';
  1. 監控ProxySQL

最後,您需要監控ProxySQL以確保它正常運行。您可以使用ProxySQL提供的各種工具來監視連接數、查詢速度、錯誤率等指標。

例如,您可以使用以下命令查看ProxySQL的當前連接數:

SELECT COUNT(*) FROM stats_mysql_connection_pool;

 

 

mysql> show databases;
+-----+---------------+-------------------------------------+
| seq | name          | file                                |
+-----+---------------+-------------------------------------+
| 0   | main          |                                     |
| 2   | disk          | /var/lib/proxysql/proxysql.db       |
| 3   | stats         |                                     |
| 4   | monitor       |                                     |
| 5   | stats_history | /var/lib/proxysql/proxysql_stats.db |
+-----+---------------+-------------------------------------+
5 rows in set (0.00 sec)

mysql> use main
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A    

Database changed
mysql> show tables;
+----------------------------------------------------+
| tables                                             |
+----------------------------------------------------+
| global_variables                                   |
| mysql_aws_aurora_hostgroups                        |
| mysql_collations                                   |
| mysql_firewall_whitelist_rules                     |
| mysql_firewall_whitelist_sqli_fingerprints         |
| mysql_firewall_whitelist_users                     |
| mysql_galera_hostgroups                            |
| mysql_group_replication_hostgroups                 |
| mysql_hostgroup_attributes                         |
| mysql_query_rules                                  |
| mysql_query_rules_fast_routing                     |
| mysql_replication_hostgroups                       |
| mysql_servers                                      |
| mysql_users                                        |
| proxysql_servers                                   |
| restapi_routes                                     |
| runtime_checksums_values                           |
| runtime_global_variables                           |
| runtime_mysql_aws_aurora_hostgroups                |
| runtime_mysql_firewall_whitelist_rules             |
| runtime_mysql_firewall_whitelist_sqli_fingerprints |
| runtime_mysql_firewall_whitelist_users             |
| runtime_mysql_galera_hostgroups                    |
| runtime_mysql_group_replication_hostgroups         |
| runtime_mysql_hostgroup_attributes                 |
| runtime_mysql_query_rules                          |
| runtime_mysql_query_rules_fast_routing             |
| runtime_mysql_replication_hostgroups               |
| runtime_mysql_servers                              |
| runtime_mysql_users                                |
| runtime_proxysql_servers                           |
| runtime_restapi_routes                             |
| runtime_scheduler                                  |
| scheduler                                          |
+----------------------------------------------------+
34 rows in set (0.00 sec)

 

 

 

 

#################################

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章