---
title: "Proxysql 2.0 | grasys blog"
url: "https://blog.grasys.io/post/shimichan/proxysql_2_0"
description: "こんにちは。grasys清水です。 今回は ProxySQL 2.0 のお話。 以前、当社のブログであげられていた Proxysql の続編みたいなもの。 1.4系に比べると便利になったので、機能のご紹介です。 ProxySQL って？ 一言で言うと、Database Proxy の一種です。 Maxscale 的な…"
---

# Proxysql 2.0

-   ![](/_astro/noicon.CTHOhNiB_1HMs6A.webp)[shimichan](/authors/shimichan/)
-   公開日：2019年9月2日
-   カテゴリー：[Tech](/categories/tech/)
-   タグ：[#proxysql](/tags/proxysql/)[#proxysql-2-0](/tags/proxysql-2-0/)

![Proxysql 2.0](/_astro/og-image.DDSiu2Ny_XHpDd.webp)

こんにちは。grasys清水です。

今回は ProxySQL 2.0 のお話。 以前、当社のブログであげられていた [Proxysql](https://blog.grasys.io/post/dokuma/proxysql/) の続編みたいなもの。 1.4系に比べると便利になったので、機能のご紹介です。

## ProxySQL って？

一言で言うと、Database Proxy の一種です。  
Maxscale 的な感じで、R/W分散、DBの死活監視、自動F/Oなんかができたりします。

ダウンタイムなしでパラメータを変えられるのも魅力の１つです。

今回は基本的な使い方には触れませんが、詳しいことはリファレンスを参照ください。

公式: [ProxySQL](https://proxysql.com/)  
GitHub: [sysown/proxysql](https://github.com/sysown/proxysql/wiki/ProxySQL-Configuration)  
ざっくり: [ProxySQL - blog.grasys.io](https://blog.grasys.io/post/dokuma/proxysql/#configuration)

## version 2.0

さて、 1.4 → 2.0 へ大幅にアップグレードした機能をざっと見てみましょう。

### GTID tracking

Mysqlのレプリケーションを組んでいて、MysqlがMasterへ書き込みを終えた **直後** 、 **レプリケーションが完了する前に** Slaveへ読み込みを行なってしまうと、正しくないデータを参照することになってしまいます。

そんな事象を解決してくれる補助機能として、 _GTID_ という機能を使うんですが、その実行されたGTIDをトラックして、正しいslaveからデータを読み込みましょう、というのが、今回追加された機能です。

R/W split ルーティングをさせたい時は、ぜひ活用したい機能になります。

参照: [GTID consistent reads - ProxySQL](https://proxysql.com/blog/proxysql-gtid-causal-reads)

### Native Galera Support

注目したい機能です。

今回のバージョンから Galera Cluster がネイティブサポートされました。

今までは、Proxysqlのスケジューラ機能を使って外部スクリプトを使い、F/Oを実現していましたが、今後はそれらが不要になります。

PXCを使うのであれば、必ず選択肢の１つに挙げたいソリューションになります。

参照: [Native Galera Support In ProxySQL](https://proxysql.com/blog/proxysql-native-galera-support)

### SSL Encryption

今までは、バックエンドに対してだけだったSSL暗号化通信が、対フロントエンドにも使えるようになりました。

参照: [SSL Encryption at ProxySQL Part 1](https://proxysql.com/blog/ssl-at-proxysql-part1)

参照: [SSL configuration for frontends](https://github.com/sysown/proxysql/wiki/SSL-Support#ssl-configuration-for-frontends)

### errorlog variable

ちょっとしたことですが、 2.0 から、ログのlocationを変更できるようになりました。

今まで、データディレクトリ配下にのみ作成されていたのですが、今回からは任意の場所に吐かせることが可能です。インフラ的には大変嬉しい機能です。

参照: [General variables - ProxySQL](https://github.com/sysown/proxysql/wiki/Configuration-file#general-variables)

## 例の Fail Over 機能を使う

今回はGaleraをネイティブサポートしたというPorxySQLの機能を使ってみます。

### mysql\_galera\_hostgroups

今回は、`mysql_galera_hostgroups` というコンポーネントを使います。  
使い方は、mysql\_replication\_hostgroups と似た感じです。  
shimichan-db01/02/03 は PXC で構築されたバックエンドです。

Plain textcontent\_copy

```
mysql_servers =
(
        { address="10.140.0.43" , port=3306 , hostgroup=500 , weight=1000 , comment="shimichan-db01" },
        { address="10.140.0.44" , port=3306 , hostgroup=500 , weight=500 , comment="shimichan-db02" },
        { address="10.140.0.45" , port=3306 , hostgroup=500 , weight=100 , comment="shimichan-db03" }
)

mysql_galera_hostgroups =
(
       {
                writer_hostgroup=500
                backup_writer_hostgroup=8500
                reader_hostgroup=501
                offline_hostgroup=9401
                max_writers=1
                writer_is_also_reader=1
                max_transactions_behind=16
                active=1
       }
)
```

msyql\_servers はすべてのグループを設定する必要はありません。  
writer\_hostgroup, reader\_hostgroup などを mysql\_servers と対応するように設定してあげます。  
私はPXCの場合は、突発的なメンテナンスなどを除いて、基本的に CONF = DISK = RUNTIME で運用するのが好みなので、CONFからLOADしてます。

(もちろん今回は、admin interface を使ってインタラクティブにパラメータを変更してもOKです)

Plain textcontent\_copy

```
--------------
SELECT * FROM runtime_mysql_galera_hostgroups
--------------

+------------------+-------------------------+------------------+-------------------+--------+-------------+-----------------------+-------------------------+---------+
| writer_hostgroup | backup_writer_hostgroup | reader_hostgroup | offline_hostgroup | active | max_writers | writer_is_also_reader | max_transactions_behind | comment |
+------------------+-------------------------+------------------+-------------------+--------+-------------+-----------------------+-------------------------+---------+
| 500              | 8500                    | 501              | 9401              | 1      | 1           | 1                     | 16                      |         |
+------------------+-------------------------+------------------+-------------------+--------+-------------+-----------------------+-------------------------+---------+
--------------
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        |
+--------------+-------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+----------------+
| 500          | 10.140.0.43 | 3306 | 0         | ONLINE | 1000   | 0           | 1000            | 0                   | 0       | 0              | shimichan-db01 |
| 8500         | 10.140.0.44 | 3306 | 0         | ONLINE | 500    | 0           | 1000            | 0                   | 0       | 0              | shimichan-db02 |
| 8500         | 10.140.0.45 | 3306 | 0         | ONLINE | 100    | 0           | 1000            | 0                   | 0       | 0              | shimichan-db03 |
| 501          | 10.140.0.43 | 3306 | 0         | ONLINE | 1000   | 0           | 1000            | 0                   | 0       | 0              | shimichan-db01 |
| 501          | 10.140.0.44 | 3306 | 0         | ONLINE | 500    | 0           | 1000            | 0                   | 0       | 0              | shimichan-db02 |
| 501          | 10.140.0.45 | 3306 | 0         | ONLINE | 100    | 0           | 1000            | 0                   | 0       | 0              | shimichan-db03 |
+--------------+-------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+----------------+
```

さて、下準備もできたので、F/O を試してみます。  
shimichan-db01 の mysql を停止します。

Plain textcontent\_copy

```
systemctl stop mysql
```

さて、どうなってるでしょうか。  
admin inteface に入って、パラメータを見ます。

Plain textcontent\_copy

```
--------------
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        |
+--------------+-------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+----------------+
| 500          | 10.140.0.44 | 3306 | 0         | ONLINE | 500    | 0           | 1000            | 0                   | 0       | 0              | shimichan-db02 |
| 8500         | 10.140.0.45 | 3306 | 0         | ONLINE | 100    | 0           | 1000            | 0                   | 0       | 0              | shimichan-db03 |
| 9401         | 10.140.0.43 | 3306 | 0         | ONLINE | 1000   | 0           | 1000            | 0                   | 0       | 0              | shimichan-db01 |
| 501          | 10.140.0.44 | 3306 | 0         | ONLINE | 500    | 0           | 1000            | 0                   | 0       | 0              | shimichan-db02 |
| 501          | 10.140.0.45 | 3306 | 0         | ONLINE | 100    | 0           | 1000            | 0                   | 0       | 0              | shimichan-db03 |
+--------------+-------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+----------------+
```

パラメータ上はうまくいってそうです。  
実際に ProxySQL 経由でアクセスしてみて、確認します

Plain textcontent\_copy

```
mysql> select @@hostname;
+------------------+
| @@hostname       |
+------------------+
|  shimichan-db02  |
+------------------+
1 row in set (0.00 sec)
```

確認できたら、 shimichan-db01 のプロセスを復帰させてみます

Plain textcontent\_copy

```
systemctl start mysql
```

確認。

Plain textcontent\_copy

```
--------------
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        |
+--------------+-------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+----------------+
| 500          | 10.140.0.43 | 3306 | 0         | ONLINE | 1000   | 0           | 1000            | 0                   | 0       | 0              | shimichan-db01 |
| 8500         | 10.140.0.44 | 3306 | 0         | ONLINE | 500    | 0           | 1000            | 0                   | 0       | 0              | shimichan-db02 |
| 8500         | 10.140.0.45 | 3306 | 0         | ONLINE | 100    | 0           | 1000            | 0                   | 0       | 0              | shimichan-db03 |
| 501          | 10.140.0.43 | 3306 | 0         | ONLINE | 1000   | 0           | 1000            | 0                   | 0       | 0              | shimichan-db01 |
| 501          | 10.140.0.44 | 3306 | 0         | ONLINE | 500    | 0           | 1000            | 0                   | 0       | 0              | shimichan-db02 |
| 501          | 10.140.0.45 | 3306 | 0         | ONLINE | 100    | 0           | 1000            | 0                   | 0       | 0              | shimichan-db03 |
+--------------+-------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+----------------+
```

ここまで！

### まとめ

メジャーバージョンが変わっただけあって、機能が充実して、使いやすくなった印象です。  
実は、 Amazon Aurora も `mysql_aws_aurora_hostgroups` のコンポーネントを使えば、 R/W 分散や F/O が ProxySQL で実現できてしまったりします。  
たまに、Maxscale が特定の言語やライブラリと相性が悪い場合があるようなので、そんな時はProxySQLを試してみるといいかもです。  
ぜひぜひ、選択肢の１つとして検討してみてください。

## この記事を書いた人

[![](/_astro/shimichan.B6KTYu8c_Z1NDayG.webp)](/authors/shimichan/)

### [shimichan](/authors/shimichan/)

shimichanのプロフィールと執筆記事をご覧いただけます。

[プロフィールと記事一覧](/authors/shimichan/)