MySQL數(shù)據(jù)讀寫分離MaxScale相關(guān)配置
一、概念:
- MySQL數(shù)據(jù)讀寫分離是存儲數(shù)據(jù)的一種服務架構(gòu)
- 執(zhí)行select命令必須連接 slave角色服務器
- 執(zhí)行insert命令必須連接 maste角色服務器
- 提供數(shù)據(jù)讀寫分離功能的中間件軟件有: mysql-proxy maxscale mycat
- 拓撲架構(gòu)只支持一主一從或者一主多從架構(gòu)
二、實現(xiàn)讀寫分離的拓撲圖:
三、MaxScale相關(guān)配置:
指令/路徑/... | 說明 |
---|---|
maxscale-2.1.2-1.rhel.7.x86_64.rpm | 軟件包 |
/etc/maxscale.cnf | 主配置文件 |
maxscale /etc/maxscale.cnf | 啟動服務 |
/var/log/maxscale/maxscale.log | 日志路徑(可查看報錯信息) |
4006 | 讀寫分離服務使用端口號 |
4016 | 管理服務使用端口號 |
四、讀寫分離的配置流程:
- 配置Mysql服務器一主一從
- 配置代理服務器(讀寫分離服務器)
- 啟動讀寫分離服務
- 客戶機50測試配置讀寫分離服務的配置
五、實操:
第一步:配置Mysql服務器一主一從
- 把host61配置為master數(shù)據(jù)庫服務器
[root@host61 ~]# vim /etc/my.cnf [mysqld] Server_id = 61 log_bin=master61 :wq [root@host61 ~]# systemctl restart mysqld [root@host61 ~]# mysql -uroot –p123qqq...A Mysql> grant replication slave on *.* to repluser@"%" identified by "123qqq...A"; Mysql> show master status ; +-----------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-----------------+----------+--------------+------------------+-------------------+ | master61.000001 | 441 | | | | +-----------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
- 把host62 配置為slave數(shù)據(jù)庫服務器
[root@host62 ~]# vim /etc/my.cnf [mysqld] Server_id = 62 :wq [root@host62 ~]# systemctl restart mysqld [root@host62 ~]# mysql -uroot -p密碼 Mysql> change master to master_host="192.168.88.61" , Master_user="repluser" , Master_password="123qqq...A" , Master_log_file="master61.000001" , Master_log_pos=441 ; Mysql> start slave; Mysql> show slave status \G Slave_IO_Running: Yes Slave_SQL_Running: Yes
第二步:配置代理服務器(讀寫分離服務器)
- 安裝軟件
[root@host60 ~]# yum -y install maxscale-2.1.2-1.rhel.7.x86_64.rpm
- 修改主配置文件
[root@host60 ~]# cp /etc/maxscale.cnf /root/ 備份主配置文件 [root@host60 ~]# vim /etc/maxscale.cnf [maxscale] threads=auto # 服務啟動后線程的數(shù)量,根據(jù)CPU 核數(shù)創(chuàng)建 [server1] type=server address=192.168.88.61 # 指定第1臺數(shù)據(jù)庫服務器的ip地址 port=3306 protocol=MySQLBackend [server2] type=server address=192.168.88.62 # 指定第2臺數(shù)據(jù)庫服務器的ip地址 port=3306 protocol=MySQLBackend [MySQL Monitor] # 定義監(jiān)視的數(shù)據(jù)庫服務器 type=monitor module=mysqlmon servers=server1,server2 # 監(jiān)視server1和server2 user=mysqla # 監(jiān)控用戶賬號 passwd=123qqq...A # 監(jiān)控用戶連接密碼 monitor_interval=10000 #禁止只讀服務(注釋) #[Read-Only Service] #type=service #router=readconnroute #servers=server1 #user=myuser #passwd=mypwd #router_options=slave [Read-Write Service] # 啟用讀寫分離服務 type=service router=readwritesplit servers=server1,server2 # 讀寫分離在server1和server2服務器之間進行 user=mysqlb # 路由用戶 passwd=123qqq...A # 連接密碼 max_slave_connections=100% [MaxAdmin Service] # 管理服務(通過訪問管理服務可以查看監(jiān)控信息) type=service router=cli # 因為只讀服務沒有啟用 ,不需要定義服務使用的端口號(注釋) #[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 port=4016 # 端口號 :wq
- 配置數(shù)據(jù)庫服務器(在數(shù)據(jù)庫服務器上添加監(jiān)控用戶和路由用戶)
- 注意:因為是主從結(jié)構(gòu) ,所以只需要在主服務器添加,從服務器會自動同步
[root@host61 ~]# mysql -uroot -p123qqq...A # 添加監(jiān)控用戶 mysqla 用戶 mysql> grant replication slave , replication client on *.* to mysqla@"%" identified by "123qqq...A"; # 權(quán)限說明: # replication client 監(jiān)視數(shù)據(jù)庫服務的運行狀態(tài) # replication slave 數(shù)據(jù)庫服務器的主從角色 # 添加路由用戶 mysqlb 用戶 mysql> grant select on mysql.* to mysqlb@"%" identified by "123qqq...A"; # 對授權(quán)庫下的表有查詢權(quán)限 # 在從服務器查看用戶是否同步 [root@host62 ~]# mysql -uroot -p123qqq...A select user from mysql.user where user="mysqla"; select user from mysql.user where user="mysqlb";
第三步:啟動讀寫分離服務
- 驗證數(shù)據(jù)庫服務器的授權(quán)用戶 mysqla 和 mysqlb
# 安裝提供mysql命令的軟件 [root@host60 ~]# which mysql || yum -y install mariadb [root@host60 ~]# mysql -h192.168.88.61 -umysqla -p123qqq...A [root@host60 ~]# mysql -h192.168.88.62 -umysqla -p123qqq...A [root@host60 ~]# mysql -h192.168.88.61 -umysqlb -p123qqq...A [root@host60 ~]# mysql -h192.168.88.62 -umysqlb -p123qqq...A # 說明:能連接成功才是對的,如果連接失?。簣?zhí)行如下操作 # 在主數(shù)據(jù)庫服務器host61 把添加 mysqla用戶 和 mysqlb 用戶的命令再執(zhí)行一遍 # 啟動服務 [root@host60 ~]# maxscale /etc/maxscale.cnf # 查看日志文件 [root@host60 ~]# ls /var/log/maxscale/ maxscale.log # 查看讀寫分離服務端口號 [root@host60 ~]# netstat -utnlp | grep 4006 tcp6 0 0 :::4006 :::* LISTEN 1580/maxscale # 查看讀寫分離服務端口號 [root@host60 ~]# netstat -utnlp | grep 4016 tcp6 0 0 :::4016 :::* LISTEN 1580/maxscale #把服務殺死 再啟動 相當于重啟服務 (修改了配置文件后要重啟服務使其配置生效) # 通過殺進程的方式停止服務 [root@host60 ~]# killall -9 maxscale # 啟動服務 [root@host60 ~]# maxscale /etc/maxscale.cnf # 在host60本機訪問管理服務查看數(shù)據(jù)庫服務的監(jiān)控信息 [root@host60 ~]# maxadmin -uadmin -pmariadb -P4016 MaxScale> list servers Servers. -------------------+-----------------+-------+-------------+-------------------- Server | Address | Port | Connections | Status -------------------+-----------------+-------+-------------+-------------------- server1 | 192.168.88.61 | 3306 | 0 | Master, Running server2 | 192.168.88.62 | 3306 | 0 | Slave, Running -------------------+-----------------+-------+-------------+-------------------- MaxScale> exit
- 排錯方法 : 查看日志里的報錯信息 vim /var/log/maxscale/maxscale.log
第四步:測試配置讀寫分離服務的配置
- 客戶端能夠連接讀寫分離服務器訪問數(shù)據(jù)庫服務
# 首先在主數(shù)據(jù)庫服務器host61 添加客戶端連接使用的用戶 [root@host61 ~]# mysql -uroot -p密碼 create database bbsdb; create table bbsdb.a(id int); grant select,insert on bbsdb.* to yaya@"%" identified by "123qqq...A"; # 在從服務器host62查看存儲數(shù)據(jù)庫表和添加用戶 [root@host62 ~]# mysql -uroot -p密碼 desc bbsdb.a; select user from mysql.user where user="yaya"; # 客戶端host50連接讀寫分離服務器host60訪問數(shù)據(jù)庫服務 mysql -h讀寫分離服務器的ip -P讀寫分離服務的端口號 -u數(shù)據(jù)庫授權(quán)用戶名 -p密碼 [root@host50 ~]# mysql -h192.168.88.60 -P4006 -uyaya -p123qqq...A
- 連接讀寫分離服務后,可以對數(shù)據(jù)做查詢和存儲操作
mysql> select * from bbsdb.a; Empty set (0.00 sec) mysql> insert into bbsdb.a values(8888); Query OK, 1 row affected (0.06 sec) mysql> select * from bbsdb.a; +------+ | id | +------+ | 8888 | +------+ 1 row in set (0.00 sec)
第五步:驗證
- 怎么驗證查詢select 訪問就在host62從服務器獲取的數(shù)據(jù)呢?
- 在從服務本機向表里添加1條記錄(在從服務添加的新數(shù)據(jù)主服務器不會同步)
# 從服務器插入1條數(shù)據(jù) [root@host62 ~]# mysql -uroot -p123qqq...A -e 'insert into bbsdb.a values(6262)' [root@host62 ~]# mysql -uroot -p123qqq...A -e 'select * from bbsdb.a' mysql: [Warning] Using a password on the command line interface can be insecure. +------+ | id | +------+ | 8888 | | 6262 | +------+ # 主服務器查詢 [root@host11 ~]# mysql -uroot -p123qqq...a -e 'select * from bbsdb.a' mysql: [Warning] Using a password on the command line interface can be insecure. +------+ | id | +------+ | 8888 | +------+ # 客戶端訪問讀寫分離服務器查詢數(shù)據(jù)(查詢結(jié)果為從服務器數(shù)據(jù)源) [root@host50 ~]# mysql -h192.168.88.60 -P4006 -uyaya -p123qqq...A -e 'select * from bbsdb.a' mysql: [Warning] Using a password on the command line interface can be insecure. +------+ | id | +------+ | 8888 | | 6262 | +------+
- 怎么驗證存儲數(shù)據(jù)insert 訪問 就是存儲在了主機服務器host61上?
# 客戶端機插入數(shù)據(jù) [root@host50 ~]# mysql -h192.168.88.60 -P4006 -uyaya -p123qqq...A -e 'insert into bbsdb.a values(666)' # 在主服務器本機查看數(shù)據(jù) [root@host61 ~]# mysql -uroot -p123qqq...a -e 'select * from bbsdb.a' mysql: [Warning] Using a password on the command line interface can be insecure. +------+ | id | +------+ | 8888 | | 666 | +------+ [root@host50 ~]# mysql -h192.168.88.60 -P4006 -uyaya -p123qqq...A -e 'select * from bbsdb.a' mysql: [Warning] Using a password on the command line interface can be insecure. +------+ | id | +------+ | 8888 | | 6262 | | 666 | +------+
- 還可以通過查看主服務器的position是否在客戶端服務器插入數(shù)據(jù)后改動來確定是不是在主服務器中進行操作過數(shù)據(jù)
補充說明
- 如果主從結(jié)構(gòu)中的從服務器宕機了,就實現(xiàn)不了讀寫分離了,會把讀寫請求都給主服務器處理。
- 如果主從結(jié)構(gòu)中的主服務器宕機了,讀寫分離服務無法訪問
以上就是MySQL數(shù)據(jù)讀寫分離MaxScale相關(guān)配置的詳細內(nèi)容,更多關(guān)于MySQL數(shù)據(jù)讀寫分離MaxScale的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
快速解決MySQL服務無法啟動顯示:系統(tǒng)出錯,發(fā)生系統(tǒng)錯誤1067, 進程意外終止的兩種方法
本人因為phpstudy的MySQL數(shù)據(jù)庫與我的電腦上的MySQL數(shù)據(jù)庫發(fā)生沖突,當我將MySQL服務器的服務名從MySQL改為MySQL5后,啟動MySQL5服務后就報錯:系統(tǒng)出錯, 發(fā)生系統(tǒng)錯誤 1067, 進程意外終止,現(xiàn)在將這個解決方法分享給大家,需要的朋友可以參考下2024-06-06MySQL性能優(yōu)化之路---修改配置文件my.cnf
mysql數(shù)據(jù)庫的優(yōu)化,算是一個老生常談的問題了,網(wǎng)上也有很多關(guān)于各方面性能優(yōu)化的例子,今天我們要談的是MySQL 系統(tǒng)參數(shù)的優(yōu)化即優(yōu)化my.cnf文件2014-06-06MySQL中l(wèi)ower_case_table_names作用及使用小結(jié)
在使用DataEase連接外部數(shù)據(jù)庫時,可能會遇到啟動報錯的問題,官方文檔指出,修改數(shù)據(jù)庫配置文件中的lower_case_table_names=1參數(shù)可以解決此問題,此參數(shù)控制表名大小寫敏感性,感興趣的可以了解一下2024-09-09MySQL中獲取最大值MAX()函數(shù)和ORDER BY … LIMIT 1比較
mysql取最大值的的是max 和order by兩種方式,同時也大多數(shù)人人為max的效率更高,在本文中,我們將介紹MySQL中MAX()和ORDER BY … LIMIT 1兩種獲取最大值的方法以及它們性能上的差異,同時我們將探討這種性能差異的原因,并提供一些優(yōu)化建議2024-03-03解決MySQL讀寫分離導致insert后select不到數(shù)據(jù)的問題
這篇文章主要介紹了解決MySQL讀寫分離導致insert后select不到數(shù)據(jù)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12mysql數(shù)據(jù)庫基礎(chǔ)知識點與操作小結(jié)
這篇文章主要介紹了mysql數(shù)據(jù)庫基礎(chǔ)知識點與操作,總結(jié)分析了mysql數(shù)據(jù)庫修改數(shù)據(jù)表、增刪改查及數(shù)據(jù)庫函數(shù)基本功能,需要的朋友可以參考下2020-01-01