Windows操做系統下的MySQL主從複製及讀寫分離

1、主服務器(master)配置php

一、修改MySQL配置文件my.inihtml

[mysqld]mysql

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_schemasql

配置完成後重啓MySQL服務。數據庫

二、受權給從服務器(slave)同步數據的帳號密碼服務器

GRANT REPLICATION SLAVE ON *.*TO 'root'@'192.168.174.131' IDENTIFIED BY '123456';ui

參數說明:lua

  • root:slave鏈接master使用的帳號
  • IDENTIFIED BY '123456' :slave鏈接master使用的密碼
  • 192.168.174.130:slave IP

執行命令show master status/G;url

注意結果中的File和Position,配置從服務器(slave)時會用到。spa

 

2、從服務器(slave)配置

一、修改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

二、設置鏈接主服務器(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數據庫中。

 

3、使用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

相關文章
相關標籤/搜索