您好,登錄后才能下訂單哦!
這篇文章主要介紹Linux下如何使用MaxScale實現數據庫讀寫分離,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
MaxScale是maridb開發的一個mysql數據中間件,其配置簡單,能夠實現讀寫分離,并且可以根據主從狀態實現寫庫的自動切換。
操作系統:CentOS Linux release 7.3.1611 (Core)
數據庫:MariaDB-10.2.6-linux-glibc_214-x86_64
MaxScale服務器:10.200.10.55
主服務器:172.16.8.56
從服務器:172.16.8.57
從服務器:172.16.8.58
1.maxscale的安裝方式有很多,例如源碼安裝、rpm、二進制構建等,我選擇二進制進行安裝。
根據場景需要下載相對應的版本,下載地址;https://mariadb.com/downloads/maxscale
[root@localhost ~]# groupadd maxscale [root@localhost ~]# useradd -g maxscale maxscale [root@localhost ~]# cd /usr/local [root@localhost local]# wget https://downloads.mariadb.com/MaxScale/2.1.3/centos/7server/x86_64/maxscale-2.1.3.centos.7.tar.gz [root@localhost local]# tar zxvf maxscale-2.1.3.centos.7.tar.gz [root@localhost local]# ln -s maxscale-2.1.3.centos.7 maxscale [root@localhost local]# cd maxscale [root@zhu56 maxscale]# chown -R maxscale var
建議創建軟連接,這樣有助于以后的版本升級及后期維護。
2.首次安裝maxscale需要創建日志相關目錄
[root@localhost ~]# mkdir /var/log/maxscale [root@localhost ~]# mkdir /var/lib/maxscale [root@localhost ~]# mkdir /var/run/maxscale [root@localhost ~]# mkdir /var/cache/maxscale
3.以下目錄必須具備maxscala用戶權限
[root@localhost ~]# chown maxscale /var/log/maxscale [root@localhost ~]# chown maxscale /var/lib/maxscale [root@localhost ~]# chown maxscale /var/run/maxscale [root@localhost ~]# chown maxscale /var/cache/maxscale
4.為了能讓Maxscale能順利啟動,還需要創建配置文件,在Maxscale目錄下有配置文件模板拷貝到etc下即可。
[root@localhost ~]# cp /usr/local/maxscale/etc/maxscale.cnf.template /etc/maxscale.cnf
5.在修改配置文件之前,需要在主服務器上創建一個用戶并給予授權,而這個用戶用于MySQL監控、路由功能
MariaDB [(none)]> create user 'jiankongdb'@'%' identified by 'jiankong123'; MariaDB [(none)]> grant SELECT on mysql.user to 'jiankongdb'@'%'; MariaDB [(none)]> GRANT SELECT ON mysql.db TO 'jiankongdb'@'%'; MariaDB [(none)]> GRANT SELECT ON mysql.tables_priv TO 'jiankongdb'@'%'; MariaDB [(none)]> GRANT SHOW DATABASES ON *.* TO 'jiankongdb'@'%'; MariaDB [(none)]> grant REPLICATION CLIENT on *.* to 'jiankongdb'@'%'; MariaDB [(none)]> GRANT replication slave, replication client,SELECT ON *.* TO jiankongdb@'%';
6.查看授權情況
MariaDB [(none)]> SHOW GRANTS FOR'jiankong'@'%';
7.接下來就開始修改maxscale.cnf配置文件,否則無法啟動。
[root@localhost ~]# vim /etc/maxscale.cnf # MaxScale documentation on GitHub: # https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Documentation-Contents.md # Global parameters # # Complete list of configuration options: # https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Getting-Started/Configuration-Guide.md #全局配置 [maxscale] threads=1 # Server definitions # # Set the address of the server to the network # address of a MySQL server. # [server1] type=server address=172.16.8.56 port=3306 protocol=MySQLBackend serv_weight=1 [server2] type=server address=172.16.8.57 port=3306 protocol=MySQLBackend serv_weight=3 [server3] type=server address=172.16.8.58 port=3306 protocol=MySQLBackend serv_weight=3 # Monitor for the servers # # This will keep MaxScale aware of the state of the servers. # MySQL Monitor documentation: # https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Monitors/MySQL-Monitor.md #MariaDB狀態監控 [MySQL Monitor] type=monitor module=mysqlmon servers=server1,server2,server3 user=jiankong passwd=jiankong123 monitor_interval=10000 detect_stale_master=true #即使從全掛掉,保證主擔任讀寫 # Service definitions # # Service Definition for a read-only service and # a read/write splitting service. # # ReadConnRoute documentation: # https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Routers/ReadConnRoute.md #讀 [Read-Only Service] type=service router=readconnroute servers=server1,server2,server3 user=jiankong passwd=jiankong123 router_options=slave enable_root_user=1 #允許root用戶登錄執行 weightby=serv_weight #主從權重 # ReadWriteSplit documentation: # https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Routers/ReadWriteSplit.md #寫 [Read-Write Service] type=service router=readwritesplit servers=server1,server2,server3 user=jiankong passwd=jiankong123 max_slave_connections=100% use_sql_variables_in=master #保證會話的一致性 enable_root_user=1 #允許root登錄 max_slave_replication_lag=3600 #允許從超出主的同步時間,超出則不路由 # This service enables the use of the MaxAdmin interface # MaxScale administration guide: # https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Reference/MaxAdmin.md [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=MySQLClient port=4008 [Read-Write Listener] type=listener service=Read-Write Service protocol=MySQLClient port=4006 [MaxAdmin Listener] type=listener service=MaxAdmin Service protocol=maxscaled socket=default
保存并退出。 8.下面創建啟動腳本
[root@localhost ~]# cp /usr/local/maxscale-2.1.3.centos.7/share/maxscale.service /usr/lib/systemd/system/ [root@localhost ~]# vim /usr/lib/systemd/system/maxscale.service
9.修改maxscale.service中的ExecStart=///bin/maxscale為ExecStart=/usr/local/maxscale/bin/maxscale
[root@localhost ~]# chmod 755 /usr/lib/systemd/system/maxscale.service [root@localhost ~]# systemctl enable maxscale [root@localhost ~]# systemctl daemon-reload [root@localhost ~]# systemctl start maxscale
10.添加變量值
[root@localhost ~]# vi /etc/profile //最后一行添加以下內容保存退出! PATH=$PATH:/usr/local/maxscale/bin export PATH [root@localhost ~]# source /etc/profile //使其變量立即生效
11.接下來就可以使用MaxAdmin進行管理。MaxAdmin是一個簡單的客戶端管理界面,可用于與MariaDB MaxScale服務器進行交互,可以顯示MariaDB MaxScale內部的統計信息狀態以及對MariaDB MaxScale操作的控制。詳情: https://mariadb.com/kb/en/mariadb-enterprise/maxadmin-admin-interface/
[root@localhost ~]# maxadmin //回車 MaxScale> list servers Servers. ---------------+--------------+-------+-------------+----------------- Server | Address | Port | Connections | Status ---------------+--------------+-------+-------------+----------------- server1 | 172.16.8.56 | 3306 | 0 | Master, Running server2 | 172.16.8.57 | 3306 | 0 | Slave, Running server2 | 172.16.8.58 | 3306 | 0 | Slave, Running ---------------+--------------+-------+-------------+-----------------
12.至此MaxScale已經配置完成。現在就可以使用客戶端連接Maxscale服務器端 端口為4006。
以上是“Linux下如何使用MaxScale實現數據庫讀寫分離”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。