• Windows操作系统下的MySQL主从复制及读写分离[转]


    mysql主从复制配置

     

    保证主库和从库数据库数据一致

    mysql主库MASTER配置(在my.cnf中加入以下配置):
    log-bin=master-bin
    binlog-do-db=test #需要同步的数据库名称
    server-id=11

    进行授权:
    grant replication slave on *.* to 'repl'@'192.168.1.110' identified by 'repl' with grant option;

    重启mysql

    查看master状态
    show master status;


    mysql从库配置(在my.cnf中加入配置):
    server-id=12

    master-host=192.168.1.104
    master-user=repl
    master-password=repl
    master-port=3306
    replicate-do-db=test #需要同步的数据库名称

    #停止slave
    stop slave

    #更改日志同步点
    CHANGE MASTER TO MASTER_HOST='192.168.1.104',MASTER_USER='repl', MASTER_PASSWORD='repl',MASTER_LOG_FILE='master-bin.000002',MASTER_POSITION=1134;

    #启动slave
    start slave

    #查看进程
    show processlist;
    #查看slave
    show slave status;

    --------------------------------------------------------------------------------

    一、主服务器(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 statusG;

    注意结果中的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 statusG显示的File名称
    • master_log_pos:master数据库中通过show master statusG显示的Position数据

    重启MySql服务。

    执行命令:start slave。

    执行命令:show slave statusG。

    当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

  • 相关阅读:
    sql处理数据库锁的存储过程
    SQL语句
    partial 函数
    map函数
    python命令行上下 退格,左右键不能用
    postgresql 在linux上的源码安装
    python字典操作
    根据key存不存在查询json
    精典博文
    python解析XML之ElementTree
  • 原文地址:https://www.cnblogs.com/fx2008/p/5072347.html
Copyright © 2020-2023  润新知