MySQL數(shù)據(jù)讀寫(xiě)分離(MaxScale)

一、概念:

  • MySQL數(shù)據(jù)讀寫(xiě)分離是存儲(chǔ)數(shù)據(jù)的一種服務(wù)架構(gòu)
  • 執(zhí)行select命令必須連接 slave角色服務(wù)器
  • 執(zhí)行insert命令必須連接 maste角色服務(wù)器
  • 提供數(shù)據(jù)讀寫(xiě)分離功能的中間件軟件有: mysql-proxy maxscale mycat
  • 拓?fù)浼軜?gòu)只支持一主一從或者一主多從架構(gòu)

二、實(shí)現(xiàn)讀寫(xiě)分離的拓?fù)鋱D:

讀寫(xiě)分離拓?fù)鋱D.png

三、MaxScale相關(guān)配置:

指令/路徑/... 說(shuō)明
maxscale-2.1.2-1.rhel.7.x86_64.rpm 軟件包
/etc/maxscale.cnf 主配置文件
maxscale /etc/maxscale.cnf 啟動(dòng)服務(wù)
/var/log/maxscale/maxscale.log 日志路徑(可查看報(bào)錯(cuò)信息)
4006 讀寫(xiě)分離服務(wù)使用端口號(hào)
4016 管理服務(wù)使用端口號(hào)

四秸妥、讀寫(xiě)分離的配置流程:

  • 配置Mysql服務(wù)器一主一從
  • 配置代理服務(wù)器(讀寫(xiě)分離服務(wù)器)
  • 啟動(dòng)讀寫(xiě)分離服務(wù)
  • 客戶機(jī)50測(cè)試配置讀寫(xiě)分離服務(wù)的配置

五、實(shí)操:

第一步:配置Mysql服務(wù)器一主一從

  • 把host61配置為master數(shù)據(jù)庫(kù)服務(wù)器
[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ù)庫(kù)服務(wù)器
[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

第二步:配置代理服務(wù)器(讀寫(xiě)分離服務(wù)器)

  • 安裝軟件
[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 # 服務(wù)啟動(dòng)后線程的數(shù)量,根據(jù)CPU 核數(shù)創(chuàng)建

[server1]    
type=server
address=192.168.88.61 # 指定第1臺(tái)數(shù)據(jù)庫(kù)服務(wù)器的ip地址
port=3306
protocol=MySQLBackend

[server2]   
type=server
address=192.168.88.62 # 指定第2臺(tái)數(shù)據(jù)庫(kù)服務(wù)器的ip地址
port=3306
protocol=MySQLBackend

[MySQL Monitor]   # 定義監(jiān)視的數(shù)據(jù)庫(kù)服務(wù)器
type=monitor
module=mysqlmon
servers=server1,server2    # 監(jiān)視server1和server2
user=mysqla      # 監(jiān)控用戶賬號(hào)
passwd=123qqq...A  # 監(jiān)控用戶連接密碼
monitor_interval=10000

#禁止只讀服務(wù)(注釋)
#[Read-Only Service]
#type=service
#router=readconnroute
#servers=server1
#user=myuser
#passwd=mypwd
#router_options=slave

[Read-Write Service]   # 啟用讀寫(xiě)分離服務(wù)
type=service
router=readwritesplit
servers=server1,server2   # 讀寫(xiě)分離在server1和server2服務(wù)器之間進(jìn)行
user=mysqlb   # 路由用戶
passwd=123qqq...A  # 連接密碼
max_slave_connections=100%

[MaxAdmin Service]   # 管理服務(wù)(通過(guò)訪問(wèn)管理服務(wù)可以查看監(jiān)控信息)
type=service
router=cli

# 因?yàn)橹蛔x服務(wù)沒(méi)有啟用 逼庞,不需要定義服務(wù)使用的端口號(hào)(注釋)
#[Read-Only Listener]
#type=listener
#service=Read-Only Service
#protocol=MySQLClient
#port=4008

[Read-Write Listener]   # 定義讀寫(xiě)分離服務(wù)使用端口號(hào)
type=listener
service=Read-Write Service
protocol=MySQLClient
port=4006    # 端口號(hào)

[MaxAdmin Listener]   # 定義管理服務(wù)使用端口號(hào)
type=listener
service=MaxAdmin Service
protocol=maxscaled
socket=default
port=4016  # 端口號(hào)
:wq
  • 配置數(shù)據(jù)庫(kù)服務(wù)器(在數(shù)據(jù)庫(kù)服務(wù)器上添加監(jiān)控用戶和路由用戶)
  • 注意:因?yàn)槭侵鲝慕Y(jié)構(gòu) 崖叫,所以只需要在主服務(wù)器添加宰翅,從服務(wù)器會(huì)自動(dòng)同步
[root@host61 ~]# mysql -uroot -p123qqq...A                     

# 添加監(jiān)控用戶 mysqla 用戶
mysql> grant  replication   slave , replication  client  on  *.*  to  
mysqla@"%" identified by "123qqq...A";
                
# 權(quán)限說(shuō)明:
# replication client   監(jiān)視數(shù)據(jù)庫(kù)服務(wù)的運(yùn)行狀態(tài)  
# replication slave      數(shù)據(jù)庫(kù)服務(wù)器的主從角色    

# 添加路由用戶 mysqlb 用戶
mysql> grant  select on  mysql.*  to  mysqlb@"%" identified by "123qqq...A";  # 對(duì)授權(quán)庫(kù)下的表有查詢權(quán)限

# 在從服務(wù)器查看用戶是否同步
[root@host62 ~]# mysql -uroot -p123qqq...A 
select user from mysql.user where user="mysqla";
select user from mysql.user where user="mysqlb";

第三步:?jiǎn)?dòng)讀寫(xiě)分離服務(wù)

  • 驗(yàn)證數(shù)據(jù)庫(kù)服務(wù)器的授權(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
# 說(shuō)明:能連接成功才是對(duì)的,如果連接失敗:執(zhí)行如下操作
# 在主數(shù)據(jù)庫(kù)服務(wù)器host61 把添加 mysqla用戶 和 mysqlb 用戶的命令再執(zhí)行一遍

# 啟動(dòng)服務(wù) 
[root@host60 ~]# maxscale   /etc/maxscale.cnf     
# 查看日志文件
[root@host60 ~]# ls /var/log/maxscale/   
maxscale.log 
# 查看讀寫(xiě)分離服務(wù)端口號(hào)
[root@host60 ~]# netstat  -utnlp  | grep 4006     
tcp6       0      0 :::4006       :::*            LISTEN      1580/maxscale       
# 查看讀寫(xiě)分離服務(wù)端口號(hào)
[root@host60 ~]# netstat  -utnlp  | grep 4016  
tcp6       0      0 :::4016       :::*        LISTEN      1580/maxscale       


#把服務(wù)殺死 再啟動(dòng)  相當(dāng)于重啟服務(wù) (修改了配置文件后要重啟服務(wù)使其配置生效)
# 通過(guò)殺進(jìn)程的方式停止服務(wù) 
[root@host60 ~]# killall -9 maxscale   
# 啟動(dòng)服務(wù)
[root@host60 ~]# maxscale  /etc/maxscale.cnf     
# 在host60本機(jī)訪問(wèn)管理服務(wù)查看數(shù)據(jù)庫(kù)服務(wù)的監(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      
  • 排錯(cuò)方法 : 查看日志里的報(bào)錯(cuò)信息 vim /var/log/maxscale/maxscale.log

第四步:測(cè)試配置讀寫(xiě)分離服務(wù)的配置

  • 客戶端能夠連接讀寫(xiě)分離服務(wù)器訪問(wèn)數(shù)據(jù)庫(kù)服務(wù)
# 首先在主數(shù)據(jù)庫(kù)服務(wù)器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";

# 在從服務(wù)器host62查看存儲(chǔ)數(shù)據(jù)庫(kù)表和添加用戶
[root@host62 ~]# mysql -uroot -p密碼
desc  bbsdb.a;
select  user from mysql.user where user="yaya";
                    
# 客戶端host50連接讀寫(xiě)分離服務(wù)器host60訪問(wèn)數(shù)據(jù)庫(kù)服務(wù)
mysql -h讀寫(xiě)分離服務(wù)器的ip   -P讀寫(xiě)分離服務(wù)的端口號(hào)  -u數(shù)據(jù)庫(kù)授權(quán)用戶名  -p密碼 
[root@host50 ~]# mysql -h192.168.88.60 -P4006 -uyaya  -p123qqq...A  
  • 連接讀寫(xiě)分離服務(wù)后薄腻,可以對(duì)數(shù)據(jù)做查詢和存儲(chǔ)操作
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)

第五步:驗(yàn)證

  • 怎么驗(yàn)證查詢select 訪問(wèn)就在host62從服務(wù)器獲取的數(shù)據(jù)呢?
  • 在從服務(wù)本機(jī)向表里添加1條記錄(在從服務(wù)添加的新數(shù)據(jù)主服務(wù)器不會(huì)同步)
# 從服務(wù)器插入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 |
+------+

# 主服務(wù)器查詢
[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 |
+------+

# 客戶端訪問(wèn)讀寫(xiě)分離服務(wù)器查詢數(shù)據(jù)(查詢結(jié)果為從服務(wù)器數(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 |
+------+
  • 怎么驗(yàn)證存儲(chǔ)數(shù)據(jù)insert 訪問(wèn) 就是存儲(chǔ)在了主機(jī)服務(wù)器host61上?
# 客戶端機(jī)插入數(shù)據(jù)
[root@host50 ~]# mysql -h192.168.88.60 -P4006 -uyaya  -p123qqq...A -e 'insert into bbsdb.a values(666)' 

# 在主服務(wù)器本機(jī)查看數(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 |
+------+
  • 還可以通過(guò)查看主服務(wù)器的position是否在客戶端服務(wù)器插入數(shù)據(jù)后改動(dòng)來(lái)確定是不是在主服務(wù)器中進(jìn)行操作過(guò)數(shù)據(jù)

補(bǔ)充說(shuō)明

  • 如果主從結(jié)構(gòu)中的從服務(wù)器宕機(jī)了挖胃,就實(shí)現(xiàn)不了讀寫(xiě)分離了,會(huì)把讀寫(xiě)請(qǐng)求都給主服務(wù)器處理凹髓。
  • 如果主從結(jié)構(gòu)中的主服務(wù)器宕機(jī)了烁登,讀寫(xiě)分離服務(wù)無(wú)法訪問(wèn)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市蔚舀,隨后出現(xiàn)的幾起案子饵沧,更是在濱河造成了極大的恐慌,老刑警劉巖赌躺,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件狼牺,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡礼患,警方通過(guò)查閱死者的電腦和手機(jī)是钥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)掠归,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人悄泥,你說(shuō)我怎么就攤上這事虏冻。” “怎么了弹囚?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵厨相,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我余寥,道長(zhǎng)领铐,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任宋舷,我火速辦了婚禮绪撵,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘祝蝠。我一直安慰自己音诈,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布绎狭。 她就那樣靜靜地躺著细溅,像睡著了一般。 火紅的嫁衣襯著肌膚如雪儡嘶。 梳的紋絲不亂的頭發(fā)上喇聊,一...
    開(kāi)封第一講書(shū)人閱讀 51,125評(píng)論 1 297
  • 那天,我揣著相機(jī)與錄音蹦狂,去河邊找鬼誓篱。 笑死,一個(gè)胖子當(dāng)著我的面吹牛凯楔,可吹牛的內(nèi)容都是我干的窜骄。 我是一名探鬼主播,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼摆屯,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼邻遏!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起虐骑,我...
    開(kāi)封第一講書(shū)人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤准验,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后廷没,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體沟娱,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年腕柜,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡盏缤,死狀恐怖砰蠢,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情唉铜,我是刑警寧澤台舱,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站潭流,受9級(jí)特大地震影響竞惋,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜灰嫉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一拆宛、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧讼撒,春花似錦浑厚、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至炎滞,卻和暖如春敢艰,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背册赛。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來(lái)泰國(guó)打工钠导, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人击奶。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓辈双,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親柜砾。 傳聞我的和親對(duì)象是個(gè)殘疾皇子湃望,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容