1这刷、作用
充分利用硬件資源胚想,提高數(shù)據(jù)庫服務效率于微。
因為數(shù)據(jù)庫的“寫”(寫10000條數(shù)據(jù)到oracle可能要3分鐘)操作是比較耗時的蹋嵌。 但是數(shù)據(jù)庫的“讀”(從oracle讀10000條數(shù)據(jù)可能只要5秒鐘)育瓜。
2、原理
多臺數(shù)據(jù)庫服務器栽烂,通過主從復制保存數(shù)據(jù)一致躏仇。這些服務器分別提供讀寫服務,均衡流量腺办。有mysql代理面向客戶端钙态,sql寫請求給主,讀請求給從菇晃,也可以作其他讀寫策略册倒,具體由服務設置。
3磺送、實現(xiàn)mysql讀寫分離驻子,拓撲圖如下:
4、實現(xiàn)mysql讀寫分離步驟
搭建主從
1)主庫192.168.4.10上面操作:
[root@master10~]#vim /etc/my.cnf
[mysqld]
server_id=10//指定服務器ID號
log-bin=master10//啟用binlog日志估灿,并指定文件名前綴
2)從庫192.168.4.20上面操作
[mysqld]
server_id=20//指定服務器ID號崇呵,不要與Master的相同
log-bin=slave20//啟動SQL日志,并指定文件名前綴
read_only=1//只讀模式
3)主庫授權一個用戶并查看master的狀態(tài)
mysql>grant all on*.*to'replicater'@'%'identified by'123456';
mysql>show master status;
4)從庫通過CHANGE MASTER語句指定MASTER服務器的IP地址馅袁、同步用戶名/密碼域慷、起始日志文件、偏移位置(參考MASTER上的狀態(tài)輸出)
mysql>change master to master_host='192.168.4.10',
->master_user='replicater',
->master_password='123456',
->master_log_file='master10.000002',
->master_log_pos=738;
mysql>start slave;
mysql>show? slave status\G;
實現(xiàn)mysql讀寫分離
1)配置數(shù)據(jù)讀寫分離服務器192.168.4.100汗销,安裝maxscale代理服務(MaxScale是maridb開發(fā)的一個MySQL數(shù)據(jù)中間件)
[root@maxscale mysql]# rpm -ivh maxscale-2.1.2-1.rhel.7.x86_64.rpm ?
2)配置maxscale
[root@maxscale mysql]#vim /etc/maxscale.cnf.template
[maxscale]
threads=auto ? ? ?//運行的線程的數(shù)量
[server1] ? ? ? ? ? ? //定義數(shù)據(jù)庫服務器
type=server
address=192.168.4.10 ? ? ? ? ?//數(shù)據(jù)庫服務器的ip
port=3306
protocol=MySQLBackend ? ? ? ? //后端數(shù)據(jù)庫
[server2]
type=server
address=192.168.4.20
port=3306
protocol=MySQLBackend
[MySQL Monitor] ? ? ? ? ? ? ? ?//定義監(jiān)控的數(shù)據(jù)庫服務器
type=monitor
module=mysqlmon
servers=server1,server2 ? ? ? ? ?//監(jiān)控的數(shù)據(jù)庫列表犹褒,不能寫ip
user=scalemon ? ? ? ? ? ? ? ? ? ? ? //監(jiān)視數(shù)據(jù)庫服務器時連接的用戶名scalemon
passwd=123456 ? ? ? ? ? ? ? ? ? ? ? ? //密碼123456
monitor_interval=10000 ? ? ? ? ? ? ?//監(jiān)視的頻率 單位為秒
#[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
user=maxscaled ? ? ? ? ? ? ? ? //用戶名 驗證連接代理服務時訪問數(shù)據(jù)庫服務器的用戶是否存在
passwd=123456//密碼
max_slave_connections=100%
[MaxAdmin Service] ? ? ? ? ? ? ? ?//定義管理服務
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=4099 ? ? ? ? ? //手動添加,不指定時使用的是默認端口在啟動服務以后可以知道默認端口是多少
3)根據(jù)配置文件的設置弛针,在數(shù)據(jù)庫服務器上添加授權用戶(主庫執(zhí)行叠骑,從庫查看)
mysql> grant replication slave,replication client on *.* to scalemon@'%' identified by "123456"; ? ////監(jiān)控數(shù)據(jù)庫服務器時,連接數(shù)據(jù)庫服務器的用戶
mysql>grant select on mysql.*to maxscaled@"%"identified by"123456"; ? ////驗證 訪問數(shù)據(jù)時削茁,連接數(shù)據(jù)庫服務器使用的用戶宙枷,是否在數(shù)據(jù)庫服務器上存在的,連接用戶
4)同時查看授權用戶
mysql>select user,host from mysql.user where userin("scalemon","maxscaled");
5)啟動服務
[root@maxscale ~]# maxscale -f /etc/maxscale.cnf
[root@maxscale ~]# netstat -antup | grep maxscale
6)測試茧跋,在本機訪問管理端口查看監(jiān)控狀態(tài)
[root@maxscale~]# maxadmin-P4099-uadmin-pmariadb
MaxScale>list servers
7)在客戶端訪問讀寫分離服務器(沒有mysql命令可以安裝)
mysql -h讀寫分離服務ip -P4006 -u用戶名 -p密碼
[root@slave53~]# mysql ?-h ?192.168.4.100 -P ?4006 ?-ureplicater ? -p123456
?mysql>select @@hostname;//查看當前主機名