【MySQL】第十章:MariaDB MaxScale

wallet01(master)
[root@wallet01 ~]# mysql -uroot -pabcd.1234
mysql> show master status;
+------------------+-----------+--------------+------------------+
| File             | Position  | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+-----------+--------------+------------------+
| mysql-bin.000003 | 251166915 |              |                  |
+------------------+-----------+--------------+------------------+
1 row in set (0.00 sec)

mysql> create user monitor@'192.168.1.%' identified by "maxscale";
Query OK, 0 rows affected (0.08 sec)

mysql> grant replication slave, replication client on *.* to monitor@'192.168.1.%';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all on maxscale_schema.* TO 'monitor'@'192.168.1.%';
Query OK, 0 rows affected (0.02 sec)

mysql> create user router@'192.168.1.%' identified by "maxscale";
Query OK, 0 rows affected (0.00 sec)

mysql> grant select on mysql.user to router@'192.168.130.%';
Query OK, 0 rows affected (0.00 sec)

mysql> grant select on mysql.db to router@'192.168.1.%';
Query OK, 0 rows affected (0.00 sec)

mysql> grant select on mysql.tables_priv to router@'192.168.1.%';
Query OK, 0 rows affected (0.01 sec)

mysql> grant show databases on *.* to router@'192.168.1.%';
Query OK, 0 rows affected (0.01 sec)

wallet02(slave)
[root@wallet02 ~]# mysql -uroot -pabcd.1234
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.201
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 251188037
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 174038
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
            
[root@wallet03 ~]# curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | bash
[warning] Found existing file at /etc/yum.repos.d/mariadb.repo. Moving to /etc/yum.repos.d/mariadb.repo.old_1.
[info] Repository file successfully written to /etc/yum.repos.d/mariadb.repo.
[info] Adding trusted package signing keys...
[info] Succeessfully added trusted package signing keys.

[root@wallet03 ~]# yum install -y maxscale latest

[root@wallet03 ~]# maxkeys
Generating .secrets file in /var/lib/maxscale.

[root@king04 ~]# maxpasswd /var/lib/maxscale maxscale
5A23832649A7C09A5011B99813491542

[root@wallet03 ~]# vi /etc/maxscale.cnf
# MaxScale documentation:
# https://mariadb.com/kb/en/mariadb-enterprise/mariadb-maxscale-22/
# Global parameters 
#
# Complete list of configuration options:
# https://mariadb.com/kb/en/mariadb-enterprise/mariadb-maxscale-22-mariadb-maxscale-configuration-usage-scenarios/
#maxscale全局參數
[maxscale]
# 線程數量,默認爲1.設置爲auto與cpu核數相同
threads=auto 
ms_timestamp=1
# Server definitions 
#
# Set the address of the server to the network
# address of a MariaDB server.
#後端數據庫服務器
[server1]
type=server
address=192.168.1.201
port=3306
protocol=MariaDBBackend
[server2]
type=server
address=192.168.1.202
port=3306
protocol=MariaDBBackend
# Monitor for the servers
#
# This will keep MaxScale aware of the state of the servers.
# MariaDB Monitor documentation:
#  
#maxscale的數據庫監控模塊
[MariaDB-Monitor]
type=monitor
module=mariadbmon
#後端數據庫服務器列表
servers=server1,server2
#maxscale監控賬戶
user=monitor
passwd=5A23832649A7C09A5011B99813491542
每隔2s執行監控檢查
monitor_interval=2000
#檢查複製延遲
detect_replication_lag=true
#當全部slave都不可用時,select查詢請求會轉發到master。
detect_stale_master=true
# Service definitions
#
# Service Definition for a read-only service
#
# ReadConnRoute documentation:
# https://mariadb.com/kb/en/mariadb-enterprise/mariadb-maxscale-22-readconnroute/
# [Read-Only-Service]
# type=service
# router=readconnroute
# servers=server1
# user=myuser
# passwd=mypwd
# router_options=slave
#  a read/write splitting service.
# ReadWriteSplit documentation:
#  
#maxscale的讀寫分離服務
[Read-Write-Service]
type=service
router=readwritesplit
servers=server1,server2
#maxscale路由賬戶
user=router
passwd=5A23832649A7C09A5011B99813491542
#全部slave提供select查詢服務
max_slave_connections=100%
#slave超時5秒,請求轉發到其他slave
max_slave_replication_lag=5
#預先設置會話變量,例如 set character_set_connection=utf8,character_set_results=utf8,character_set_client=binary
默認是all,在master與slave上執行,設置爲master,將只會路由到master執行
use_sql_variables_in=all
#允許root用戶登錄
enable_root_user=1
# This service enables the use of the MaxAdmin interface
# MaxScale administration guide:
#  
#maxscale的管理服務
[MaxAdmin-Service]
type=service
router=cli
# Listener definitions for the services
#
# These listeners represent the ports the
# services will listen on.
#
# [Read-Only-Listener]
# type=listener
# service=Read-Only-Service
# protocol=MariaDBClient
# port=4008
#maxscale讀寫分離服務的監聽端口
[Read-Write-Listener]
type=listener
service=Read-Write-Service
protocol=MariaDBClient
port=4006
#maxscale管理服務的監聽端口
[MaxAdmin-Listener]
type=listener
service=MaxAdmin-Service
protocol=maxscaled
socket=default

[root@wallet03 ~]# /etc/init.d/maxscale start
Starting MaxScale: maxscale (pid 1500) is running...       [  OK  ]

[root@wallet03 ~]# /etc/init.d/maxscale status
Checking MaxScale status: MaxScale (pid 1500) is running..[  OK  ]

[root@wallet03 ~]# mysql -utpcc -ptpcc -P 4006 -h 192.168.1.200
mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| wallet02   |
+------------+

[root@wallet03 ~]# maxadmin -S /tmp/maxadmin.sock 
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server             | Address         | Port  | Connections | Status              
-------------------+-----------------+-------+-------------+--------------------
server1            | 192.168.1.201   |  3306 |           4 | Master, Running
server2            | 192.168.1.202   |  3306 |           4 | Slave, Running
-------------------+-----------------+-------+-------------+--------------------

延遲檢測:maxscale會對master和slave上replication_heartbeat表的master_timestamp時間戳進行對比,
相減得出差異。這個差異就是MySQL主從同步的延遲值。

[root@wallet01 ~]# mysql -uroot -pabcd.1234
mysql> select * from maxscale_schema.replication_heartbeat;
+-------------+------------------+------------------+
| maxscale_id | master_server_id | master_timestamp |
+-------------+------------------+------------------+
|           0 |                1 |       1544771935 |
+-------------+------------------+------------------+


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