mysql主從複製讀寫分離之——proxysql應用

1、說明
ProxySQL是一個開源的MySQL代理服務器,這意味着它充當MySQL服務器和訪問其數據庫的應用程序之間的中介。ProxySQL能夠經過在多個數據庫服務器池之間分配流量來提升性能,而且若是一個或多個數據庫服務器發生故障,還能夠經過自動故障切換到備用數據庫來提升可用性。mysql

系統環境:master1:ubuntu16.04 mysql5.6 192.168.1.10 3307
master2:ubuntu16.04 mysql5.6 192.168.1.20 3307
slave1: ubuntu16.04 mysql5.6 192.168.1.10 3308
slave2: ubuntu16.04 mysql5.6 192.168.1.10 3309
slave3: ubuntu16.04 mysql5.6 192.168.1.20 3308
slave4: ubuntu16.04 mysql5.6 192.168.1.20 3309
【proxysql】:ubuntu16.04 mysql 192.168.1.30 3306nginx

master1與master2爲雙主雙從模式。
主從複製前邊文章和網上文章都特別詳細。這裏不作介紹。請參考https://blog.51cto.com/13120271/2140400。
第一步 - 安裝ProxySQL
ProxySQL的開發人員在他們的GitHub版本頁面上爲全部ProxySQL版本提供官方Ubuntu軟件包,所以咱們將從那裏下載最新的軟件包版本並進行安裝。git

您能夠在發佈列表中找到最新的軟件包。命名約定是proxysql_version-distribution.deb,其中version相似於版本1.4.4的1.4.4字符串,而且distribution是一個相似於64位Ubuntu 16.04的ubuntu16_amd64字符串。github

將最新的官方軟件包(編寫本文時爲1.4.4)下載到/tmp目錄中。sql

cd /tmp
curl -OL https://github.com/sysown/proxysql/releases/download/v1.4.4/proxysql_1.4.4-ubuntu16_amd64.deb數據庫

用dpkg安裝包,用於管理.deb軟件包。-i標誌表示咱們要從指定的文件安裝。ubuntu

sudo dpkg -i proxysql_*安全

此時,您再也不須要.deb文件,所以您能夠將其刪除。服務器

rm proxysql_*
接下來,咱們須要一個MySQL客戶端應用程序來鏈接到ProxySQL實例。這是由於ProxySQL內部使用一個MySQL兼容的接口來執行管理任務。咱們將使用mysql命令行工具,它是mysql-client Ubuntu存儲庫中可用包的一部分。session

更新軟件包存儲庫以確保得到最新的預捆綁版本,而後安裝mysql-client軟件包。

sudo apt-get update
sudo apt-get install mysql-client

您如今知足運行ProxySQL的全部要求,但該服務在安裝後不會自動啓動,所以請當即手動啓動。

sudo systemctl start proxysql

如今,ProxySQL應該以其默認配置運行。你能夠使用systemctl查看。

systemctl status proxysql

輸出看起來像這樣:

● proxysql.service - LSB: High Performance Advanced Proxy for MySQL
Loaded: loaded (/etc/init.d/proxysql; bad; vendor preset: enabled)
Active: active (running) since Thu 2017-12-21 19:19:20 UTC; 5s ago
Docs: man:systemd-sysv-generator(8)
Process: 12350 ExecStart=/etc/init.d/proxysql start (code=exited, status=0/SUCCESS)
Tasks: 23
Memory: 30.9M
CPU: 86ms
CGroup: /system.slice/proxysql.service
├─12355 proxysql -c /etc/proxysql.cnf -D /var/lib/proxysql
└─12356 proxysql -c /etc/proxysql.cnf -D /var/lib/proxysql

Active (running)行表示ProxySQL已安裝並正在運行。

接下來,咱們將經過設置用於訪問ProxySQL管理界面的密碼來提升安全性。
第二步 - 設置ProxySQL管理員密碼
第一次啓動新的ProxySQL安裝時,它使用程序包提供的配置文件來初始化其全部配置變量的默認值。初始化後,ProxySQL將其配置存儲在數據庫中,您能夠經過命令行進行管理和修改。

要在ProxySQL中設置管理員密碼,咱們將鏈接到該配置數據庫並更新相應的變量。

首先,訪問管理界面。系統將提示您輸入密碼,在默認安裝時,密碼爲admin。

mysql -u admin -p -h 127.0.0.1 -P 6032 --prompt='ProxySQLAdmin> '

  • -u指定咱們要鏈接的用戶,這裏是admin,管理任務的默認用戶,例如更改配置設置。
  • -h 127.0.0.1告訴mysql鏈接到本地ProxySQL實例。咱們須要明肯定義它,由於ProxySQL不會監聽默認狀況下mysql假定的socket文件。
  • -P指定要鏈接的端口。ProxySQL的管理界面監聽6032。
  • --prompt是一個可選標誌,用於更改默認提示,一般是mysql>。在這裏,咱們將其更改成ProxySQLAdmin>顯示咱們已鏈接到ProxySQL管理界面。這將有助於避免之後在咱們也將鏈接到複製數據庫服務器上的MySQL接口時出現混淆。*
    鏈接後,您將看到ProxySQLAdmin>提示:

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.30 (ProxySQL Admin Module)

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.

ProxySQLAdmin>

經過更新(UPDATE)global_variables數據庫中的admin-admin_credentials配置變量來更改管理賬戶密碼。請住將如下命令的password更改成您選擇的強密碼。

UPDATE global_variables SET variable_value='admin:password' WHERE variable_name='admin-admin_credentials';
Query OK, 1 row affected (0.00 sec)

因爲ProxySQL的配置系統的工做方式,此更改不會當即生效。它由三個獨立的層組成:

內存,在從命令行界面進行修改時會被更改。
運行時,ProxySQL使用它做爲有效配置。
磁盤,用於使配置在從新啓動時保持不變。
如今,你所作的改變是在內存中。要使更改生效,您必須將內存設置複製到運行時領域,而後將它們保存到磁盤以使其保持不變。

ProxySQLAdmin> LOAD ADMIN VARIABLES TO RUNTIME;
ProxySQLAdmin> SAVE ADMIN VARIABLES TO DISK;

第三步-修改配置文件

egrep -v '(^$|^#)' /etc/proxysql.cnf

admin_credentials="admin:admin"
mysql_ifaces="127.0.0.1:6032;/tmp/proxysql_admin.sock"
}
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;/tmp/proxysql.sock"
default_schema="information_schema"
stacksize=1048576
server_version="5.5.30"
connect_timeout_server=3000
monitor_username="monitor"
monitor_password="monitor"
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
}
mysql_servers =
(
{
address = "192.168.1.20" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
port = 3307 # no default, required . If port is 0 , 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_connections = 200
},
{
address = "192.168.1.10" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
port = 3307 # no default, required . If port is 0 , 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_connections = 200
},
{
address = "192.168.1.20" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
port = 3308 # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
hostgroup = 2 # no default, required
status = "ONLINE" # default: ONLINE
weight = 1 # default: 1
compression = 0 # default: 0
max_connections = 1000
},
{
address = "192.168.1.20" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
port = 3309 # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
hostgroup = 2 # no default, required
status = "ONLINE" # default: ONLINE
weight = 1 # default: 1
compression = 0 # default: 0
max_connections = 1000
},
{
address = "192.168.1.10" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
port = 3308 # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
hostgroup = 2 # no default, required
status = "ONLINE" # default: ONLINE
weight = 1 # default: 1
compression = 0 # default: 0
max_connections = 1000
},
{
address = "192.168.1.10" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
port = 3309 # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
hostgroup = 2 # no default, required
status = "ONLINE" # default: ONLINE
weight = 1 # default: 1
compression = 0 # default: 0
max_connections = 1000
}
)
mysql_users:
(
{
username = "admin" # no default , required
password = "password" # default: ''
default_hostgroup = 1 # default: 0
max_connections=1000
default_schema="test"
active = 1 # default: 1
}
)
mysql_query_rules:
(
)
scheduler=
(
)
mysql_replication_hostgroups=
(
{
writer_hostgroup=1
reader_hostgroup=2
comment="test repl 1"
}
)

重新啓動proxysql。

sudo systemctl restart proxysql
而後咱們登陸上去驗證一下。
mysql -u admin -p -h 127.0.0.1 -P 6032 --prompt='ProxySQLAdmin> '
mysql > show databases;
mysql> show databases;
+--------------------+| Database |
+--------------------+| information_schema || mysql || performance_schema || study || sys |

以上stats,monitor,main都從配置文件中獲取的數據庫;能夠經過相似mysql運行時修改;而不須要重啓;更多命令行的配置請參考github;至此咱們基於proxysql主從複製讀寫分離架構已經完成;雙主或多主模型是無須實現讀寫分離,僅須要負載均衡:haproxy, nginx, lvs等;proxysql並無解決,當主數據巖機時的問題;此時就須要量MHA來解決 ;後續再介紹;

相關文章
相關標籤/搜索