zoukankan      html  css  js  c++  java
  • mysql replication 互为主从的安装及配置,以及数据同步

    一,mysql replication是什么,干什么用的

    MySQL replication提供了数据库复制的功能,可以实现多个数据库实时同步,具体如果下图:


    mysql replication 数据同步

     

    我们为什么要用mysql replication呢,当你的网站一天的独立IP流量能达到100W时,全部流量能达到1000W时,单个服务器,根本无法满足现有需要,100W,1000W就是举个例子。尽管你做了许多的技术上面的措施,比如内存缓存(如memcache),文件缓存啊,对于大数据量表进行分表啊等等,还是网站还是挺慢的(这可能有多方面原因),在这儿只是以MYSQL为例。

    二,mysql replication 的安装和配置

    mysql 3.23.15以后,装mysql的时候,就自带有mysql replication。我四年前做开发的,那个时候,我接触的mysql4,如果现在还有用mysql3的,那我就表示无语了。

    1)安装mysql

    下个mysql,安装一下,导入表和内容。我就不在这儿详细说了。

    2)在192.168.1.91上修改my.ini

    a,192.168.1.91上修改mysql的配置文件my.ini,master是windows系统

    1 server-id=1
    2 binlog-do-db=test1
    3 auto_increment_offset= 2
    4 log-bin=d:/wamp/mysql/master-bin.log
    5 log-bin-index = d:/wamp/mysql/master-log-bin.index
    6 log-error = d:/wamp/mysql/master-error.log
    7 relay-log = d:/wamp/mysql/slave-relay.log
    8 relay-log-info-file = d:/wamp/mysql/slave-relay-log.info
    9 relay-log-index = d:/wamp/mysql/slave-relay-log.index
    10 set-variable=max_connections=500
      

    b, 其次仍然在该区域修改配置为以该机为slave

    1 master-host=192.168.1.5
    2 master-user=xtajmd
    3 master-password=xtajmd
    4 master-port=3306
    5 master-connect-retry=60
    6 replicate-do-db=test1
    7 log-slave-updates
    解释:

    server-id=1表示是本机的序号为1,一般来讲就是master的意思.

    binlog-do-db=test1表示需要备份的数据库是test这个数据库,

    replicate-do-db=test1 表示同步test数据库;

    如果需要备份多个数据库,那么应该写多行,如下所示:

    binlog-do-db=test1

    binlog-do-db=test2

    binlog-do-db=test3

    log-bin表示打开binlog,打开该选项才可以通过I/O写到Slave的relay-log,也是可以进行replication的前提;

    auto_increment_increment定义下一次AUTO_INCREMENT的步长

    auto_increment_offset= 1定义AUTO_INCREMENT的起点值

    这两个参数用于避免多master情况下,多个master同时存取字段类型为AUTO_INCREMENT的冲突。其它几个字段为日志文件配置信息

    set-variable=max_connections=500 设定数据库最大连接数为500

    master-host=192.168.1.91 表示本机做slave时的master为192.168.1.91;

    master-user=xtajmd 这里表示master(192.168.1.91)上开放的一个有权限的用户,使其可以从slave连接到master并进行复制; 建议两台主机的授权用户和密码完全相同。

    master-password=xtajmd 表示授权用户的密码;

    master-port=3306 表示master(192.168.1.91)上MySQL服务Listen3306端口;

    master-connect-retry=60 同步间隔时间;

    log-slave-updates:从master读到的更新操作都记录到slave二进制日志中

    3)在192.168.1.5上修改my.cnf

    vi my.cnf

    找到

    # The MySQL server
    [mysqld]

    在mysqld里面加上以下部分

    1 server-id=2 #表示本机序号,不要和主机相同
    2 binlog-do-db=test1
    3 log-bin=/usr/local/mysql/master-bin.log
    4 log-bin-index = /usr/local/mysql/master-log-bin.index
    5 log-error = /usr/local/mysql/master-error.log
    6 relay-log = /usr/local/mysql/slave-relay.log
    7 relay-log-info-file = /usr/local/mysql/slave-relay-log.info
    8 relay-log-index = /usr/local/mysql/slave-relay-log.index
    9 set-variable=max_connections=500
    10 master-host=192.168.1.91
    11 master-user=xtajmd
    12 master-password=xtajmd
    13 master-port=3306
    14 master-connect-retry=60
    15 replicate-do-db=test1
    16 log-slave-updates
    4),准备二个一样的数据库

    从一台机器上面的mysql中导出一个数据库,什么数据库都行,然后导入到另一台机器的数据中。

    方法请参考:http://blog.51yip.com/mysql/139.html

    5) 建立账户,分配权限

    在192.168.1.91和192.168.1.5上使用mysql登陆,分别操作如下操作:

    GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO  xtajmd@’%’ IDENTIFIED BY ‘xtajmd’;

    flush

    在192.168.1.91和192.168.1.5上分别测试所添加用户能否正常登陆另外一台服务器。

    在192.168.1.5上:mysql -u xtajmd -p -h 192.168.1.91

    mysql -u xtajmd -p -h 192.168.1.91
    Enter password:
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 11
    Server version: 5.0.27-community-nt-log MySQL Community Edition (GPL)

    Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

    mysql>

    在192.168.1.91上:mysql -u xtajmd -p -h 192.168.1.5

    注意:

    如果二台linux系统间的数据同步的话,请确认两台服务器上iptables的3306端口已经开通,以保证通信正常。

    如果是windows和linux系统间的数据同步的话,确保linux服务器上iptables的3306端口已经开通,windows下面防火墙,杀毒软件是不是允许外界通过3306来连接本地数据库

    6) 二台服务器都重起mysql,查master,slave状态,以及注意问题

    在192.168.1.91上

    mysql> show master status;
    +——————-+———-+————–+——————+
    | File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +——————-+———-+————–+——————+
    master-bin.000072 |       98 | test1        |                  |
    +——————-+———-+————–+——————+
    1 row in set (0.00 sec)

    在192.168.1.5上

    mysql> show slave status\G;
    *************************** 1. row ***************************
    Slave_IO_State: Waiting for master to send event
    Master_Host: 192.168.1.91
    Master_User: xtajmd
    Master_Port: 3306
    Connect_Retry: 60
    Master_Log_File: master-bin.000072
    Read_Master_Log_Pos: 98
    Relay_Log_File: slave-relay.000330
    Relay_Log_Pos: 244
    Relay_Master_Log_File: master-bin.000072
    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes
    Replicate_Do_DB: test1
    Replicate_Ignore_DB:
    Replicate_Do_Table:
    Replicate_Ignore_Table:
    Replicate_Wild_Do_Table:
    Replicate_Wild_Ignore_Table:

    注意:在这里Slave_IO_State:后面有可能没有任何参数,或者是Waitting to connect

    我们一定要确保,master里面File和Master_Log_File内容是一样的,position和Read_Log_File_Pos要一样的。

    反过来也要一样。如果不一样怎么办呢?如下

    解决步骤:
    重启master库:
    mysql> show master status;
    +——————+———-+————–+——————+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +——————+———-+————–+——————+
    | mysql-bin.000072 |       98 |      test1        |                  |
    +——————+———-+————–+——————+
    mysql> slave stop;
    mysql> change master to Master_Log_File=’mysql-bin.000072′,Master_Log_Pos=98;
    mysql> slave start;
    mysql> show slave status\G

    到这里基本上算是配置好了,现在测试一下吧,在91上的数据库test1里面,加张表试一下,看看5上面有没有。哈哈。

    转:http://blog.51yip.com/mysql/357.html

  • 相关阅读:
    Tomcat 下载安装与配置
    mysql explain用法和结果的含义
    xampp 配置多域名工程 解决 Access forbidden!
    谷歌地图 API 开发之获取坐标以及街道详情
    MySQL数学函数简明总结
    SQLSTATE[HY000]: General error: 1267 Illegal mix of collations (utf8_general_ci,IMPLICIT) and (gb2312_chinese_ci,COERCIBLE) for operation '='
    报错:illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation '='
    VS-Visual Studio-IIS Express 支持局域网访问(用于进入断点)
    Dispatcher类介绍
    this.Dispatcher.BeginInvoke((Action)(() =>的作用
  • 原文地址:https://www.cnblogs.com/shuaixf/p/2036056.html
Copyright © 2011-2022 走看看