- 論壇徽章:
- 1
|
一、主服務(wù)器(master)配置
1、修改MySQL配置文件my.ini
[mysqld]
log-bin=mysql-bin
log-bin-index=mysql-bin.index
server-id=1
sync_binlog=1
binlog_format=mixed
binlog-do-db=test
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
binlog-ignore-db=information_schema
配置完成后重啟MySQL服務(wù)。
2、授權(quán)給從服務(wù)器(slave)同步數(shù)據(jù)的賬號(hào)密碼
GRANT REPLICATION SLAVE ON *.*TO 'root'@'192.168.174.131' IDENTIFIED BY '123456';
參數(shù)說明:
root:slave連接master使用的賬號(hào)
IDENTIFIED BY '123456' :slave連接master使用的密碼
192.168.174.130:slave IP
執(zhí)行命令show master status\G;
注意結(jié)果中的File和Position,配置從服務(wù)器(slave)時(shí)會(huì)用到。
二、從服務(wù)器(slave)配置
1、修改MySQL配置文件my.ini
[mysqld]
server-id=2
log-bin=mysql-bin
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin
sync_master_info=1
sync_relay_log=1
sync_relay_log_info=1
2、設(shè)置連接主服務(wù)器(master)的信息
change master to master_host='192.168.174.130',master_user='root',master_port=3306,master_password='root',master_log_file='mysql-bin.000008',master_log_pos='170'
參數(shù)說明:
master_host:master IP
master_user:master數(shù)據(jù)庫(kù)通過GRANT授權(quán)的賬號(hào)
master_port:master數(shù)據(jù)庫(kù)使用的端口號(hào)
master_password:master數(shù)據(jù)庫(kù)通過GRANT授權(quán)的密碼
master_log_file:master數(shù)據(jù)庫(kù)中通過show master status\G顯示的File名稱
master_log_pos:master數(shù)據(jù)庫(kù)中通過show master status\G顯示的Position數(shù)據(jù)
重啟MySql服務(wù)。
執(zhí)行命令:start slave。
執(zhí)行命令:show slave status\G。
當(dāng)Slave_IO_Running與Slave_SQL_Running都為Yes時(shí)才算配置成功。
此時(shí),master服務(wù)器上test數(shù)據(jù)庫(kù)里的數(shù)據(jù)就能同步到slave服務(wù)器上的test數(shù)據(jù)庫(kù)中。
三、使用MySQL Proxy實(shí)現(xiàn)讀寫分離
在此使用配置文件的方式來進(jìn)行配置。
配置文件mysql-proxy.conf中的內(nèi)容主要包括:
[mysql-proxy]
admin-username=root
admin-password=123456
admin-lua-script=C:/mysql-proxy/lib/mysql-proxy/lua/admin.lua
proxy-backend-addresses=192.168.174.130:3306
proxy-read-only-backend-addresses=192.168.174.131:3306
proxy-lua-script=C:/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
log-file=C:/mysql-proxy/log/mysql-proxy.log
log-level=debug
daemon=true
keepalive=true
執(zhí)行命令:
mysql-proxy -P 192.168.174.133:4040 --defaults-file=C:/mysql-proxy/bin/mysql-proxy.conf
查看日志文件mysql-proxy.log:
2014-12-19 16:27:40: (critical) plugin proxy 0.8.5 started
2014-12-19 16:27:40: (debug) max open file-descriptors = 512
2014-12-19 16:27:40: (message) proxy listening on port 192.168.174.133:4040
2014-12-19 16:27:40: (message) added read/write backend: 192.168.174.130:3306
2014-12-19 16:27:40: (message) added read-only backend: 192.168.174.131:3306
出現(xiàn)以上日志信息則表示MySQL Proxy啟動(dòng)成功,此時(shí)便可以實(shí)現(xiàn)讀寫分離了。
注意:由于rw-splitting.lua中的min_idle_connections的默認(rèn)值為4,即當(dāng)會(huì)話數(shù)達(dá)到最小為4時(shí),才會(huì)進(jìn)行讀寫分離,在此我們將其改為1,則可直接進(jìn)行讀寫分離。 |
|