Windows作業系統下的MySQL主從複製及讀寫分離

來源:互聯網
上載者:User

標籤:

一、主伺服器(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主從複製及讀寫分離

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.