標籤:
一、主伺服器(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服務。
2、授權給從伺服器(slave)同步資料的帳號密碼
GRANT REPLICATION SLAVE ON *.*TO ‘root‘@‘192.168.174.131‘ IDENTIFIED BY ‘123456‘;
參數說明:
- root:slave串連master使用的帳號
- IDENTIFIED BY ‘123456‘ :slave串連master使用的密碼
- 192.168.174.130:slave IP
執行命令show master status\G;
注意結果中的File和Position,配置從伺服器(slave)時會用到。
二、從伺服器(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、設定串連主伺服器(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‘
參數說明:
- master_host:master IP
- master_user:master資料庫通過GRANT授權的帳號
- master_port:master資料庫使用的連接埠號碼
- master_password:master資料庫通過GRANT授權的密碼
- master_log_file:master資料庫中通過show master status\G顯示的File名稱
- master_log_pos:master資料庫中通過show master status\G顯示的Position資料
重啟MySql服務。
執行命令:start slave。
執行命令:show slave status\G。
當Slave_IO_Running與Slave_SQL_Running都為Yes時才算配置成功。
此時,master伺服器上test資料庫裡的資料就能同步到slave伺服器上的test資料庫中。
三、使用MySQL Proxy實現讀寫分離
在此使用設定檔的方式來進行配置。
設定檔mysql-proxy.conf中的內容主要包括:
[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
執行命令:
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
出現以上日誌資訊則表示MySQL Proxy啟動成功,此時便可以實現讀寫分離了。
注意:由於rw-splitting.lua中的min_idle_connections的預設值為4,即當會話數達到最小為4時,才會進行讀寫分離,在此我們將其改為1,則可直接進行讀寫分離。
MySQL:http://yunpan.cn/cfWp4tZDACvnp 提取碼 b0db
MySQL Proxy:http://yunpan.cn/cfWpikpQWCsxM 提取碼 ad1c
Windows作業系統下的MySQL主從複製及讀寫分離