1. 实验环境:

    master1:172.17.0.2      远程登录信息:user:master1      password:master1

    master2:172.17.0.3      远程登录信息:user:master2      password:master2

  2. master1与master2主配置文件修改,在"[mysqld]"中添加一下内容:

    master1主配置文件:

    server-id=1
    log-bin=mysql-bin

    log-bin-index=mysql-bin.index

    relay-log=mysql-relay

    relay-log-index=mysql-relay.index

    expire-logs-days=10

    max-binlog-size=100
    binlog-ignore-db=mysql
    replicate-ignore-db=mysql
    log-slave-updates
    slave-skip-errors=all
    auto_increment_increment=2
    auto_increment_offset=1
    skip-name-resolve

    master2主配置文件:

    server-id=100
    log-bin=mysql-bin
    binlog-ignore-db=mysql

    log-bin-index=mysql-bin.index

    relay-log=mysql-relay

    relay-log-index=mysql-relay.index

    expire-logs-days=10

    max-binlog-size=100
    replicate-ignore-db=mysql
    log-slave-updates
    slave-skip-errors=all
    auto_increment_increment=2
    auto_increment_offset=2
    skip-name-resolve

    备注:

    log-slave-updates:为从库的写操作记录binlog

    auto_increment_increment:字段一次递增多少

    auto_increment_offset:自增字段起始值

  3. 使用"show master status;"命令分别获取master1与master2的"File"与"Position"值,并记录下来,假设数据如下:

                      File                                   Position

    maser1       mysql-bin.000003              255

    master2      mysql-bin.000003              255

  4. master1中执行以下命令:

    change master to master_host='172.17.0.3',master_user='master2',master_passwod='master2',master_log_file='mysql-bin.000003',master_log_pos=255;

    start slave;

    show slave status\G;                #确认"Slave_IO_Running"与"Slave_SQL_Running"是否为""Yes,以及是否有其他错误信息

  5. master2中执行以下命令:

    change master to master_host='172.17.0.2',master_user='master1',master_passwod='master1',master_log_file='mysql-bin.000003',master_log_pos=255;

    start slave;

    show slave status\G;                #确认"Slave_IO_Running"与"Slave_SQL_Running"是否为""Yes,以及是否有其他错误信息

  6. 测试:分别在master1与master2中创建数据库,表,以及插入数据进行测试