zoukankan      html  css  js  c++  java
  • mysql5.6 主从同步

    主库IP:192.168.220.3

    从库IP:192.168.220.4


    1、主库配置编辑my.cnf:

    # For advice on how to change settings please see
    # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
    # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
    # *** default location during install, and will be replaced if you
    # *** upgrade to a newer version of MySQL.
    
    [mysqld]
    
    # Remove leading # and set to the amount of RAM for the most important data
    # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
    # innodb_buffer_pool_size = 128M
    
    # Remove leading # to turn on a very important data integrity option: logging
    # changes to the binary log between backups.
    # log_bin
    
    # These are commonly set, remove the # and set as required.
    basedir = /usr/mysql
    datadir = /usr/mysql/data
    port = 3306
    server_id = 1
    socket = /tmp/mysql.sock 
    lower_case_table_names = 1
    
    log-bin = mysql-bin  # 启用二进制日志
    server-id = 3        # 服务器唯一ID 取IP最后一段
    sync_binlog = 1      # 每次事务提交则将cache中的数据写入磁盘并同步执行同步指令,此时最安全但是性能损失最大
    binlog_format = mixed # binlog日志格式,mysql默认采用
    binlog-do-db = db1 # 同步数据库
    binlog-ignore-db = mysql # 忽略同步的数据库
    binlog-ignore-db = performance_schema
    binlog-ignore-db = information_schema
    binlog_checksum=NONE
    
    
    
    # Remove leading # to set options mainly useful for reporting servers.
    # The server defaults are faster for transactions and fast SELECTs.
    # Adjust sizes as needed, experiment to find the optimal values.
    # join_buffer_size = 128M
    # sort_buffer_size = 2M
    # read_rnd_buffer_size = 2M 
    
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

    2、主库mysql创建同步账号

    grant replication slave on *.* to slave@192.168.220.4 indentified by 'qwe123'

    flush privileges;

    3、主库状态

    mysql> show master status;


    +------------------+----------+--------------+---------------------------------------------+-------------------+
    | File                      | Position | Binlog_Do_DB | Binlog_Ignore_DB                                           | Executed_Gtid_Set |
    +------------------+----------+--------------+---------------------------------------------+-------------------+
    | mysql-bin.000002 |      402 | db1                 | mysql,performance_schema,information_schema |                   |
    +------------------+----------+--------------+---------------------------------------------+-------------------+

    记录下二进制日志文件名和位置


    4、从库配置文件配置

    # 启用二进制日志
    log-bin=mysql-bin   
    # 服务器唯一ID,一般取IP最后一段
    server-id=4
    
    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

    5、从库mysql中配置连接主库

    change master to master_host='192.168.220.3',master_user='slave',master_password='qwe123', master_log_file='mysql-bin.000002',master_log_pos=402;

    6、开始同步

    mysql> start slave;

    7、查看同步状态

    mysql> show slave statusG;

    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes


     备注:如果主从不同步处理方案

    先上Master库:

    mysql>show processlist;   查看下进程是否Sleep太多。发现很正常。

    show master status; 也正常。

    mysql> show master status;
    +-------------------+----------+--------------+-------------------------------+
    | File                        | Position | Binlog_Do_DB | Binlog_Ignore_DB              |
    +-------------------+----------+--------------+-------------------------------+
    | mysqld-bin.000001 |     3260 |                      | mysql,test,information_schema |
    +-------------------+----------+--------------+-------------------------------+
    1 row in set (0.00 sec)

    再到Slave上查看

    mysql> show slave statusG                                                

    Slave_IO_Running: Yes

    Slave_SQL_Running: No

    可见是Slave不同步

    下面介绍两种解决方法:

    方法一:忽略错误后,继续同步

    该方法适用于主从库数据相差不大,或者要求数据可以不完全统一的情况,数据要求不严格的情况

    解决: 

    stop slave;

    #表示跳过一步错误,后面的数字可变

    set global sql_slave_skip_counter =1;

    start slave;

    之后再用mysql> show slave statusG  查看:

    Slave_IO_Running: Yes

    Slave_SQL_Running: Yes

    ok,现在主从同步状态正常

    方式二:重新做主从,完全同步

    该方法适用于主从库数据相差较大,或者要求数据完全统一的情况

    解决步骤如下:

    1.先进入主库,进行锁表,防止数据写入

    使用命令:

    mysql> flush tables with read lock;

    注意:该处是锁定为只读状态,语句不区分大小写

    2.进行数据备份 

    #把数据备份到mysql.bak.sql文件

    [root@server01 mysql]#mysqldump -uroot -p -hlocalhost > mysql.bak.sql

    3.查看master 状态

    mysql> show master status;
    +-------------------+----------+--------------+-------------------------------+
    | File                        | Position | Binlog_Do_DB | Binlog_Ignore_DB              |
    +-------------------+----------+--------------+-------------------------------+
    | mysqld-bin.000001 |     3260 |                      | mysql,test,information_schema |
    +-------------------+----------+--------------+-------------------------------+
    1 row in set (0.00 sec)

    4.把mysql备份文件传到从库机器,进行数据恢复

    #使用scp命令

    [root@server01 mysql]# scp mysql.bak.sql root@192.168.220.4:/tmp/

    5.停止从库的状态

    mysql> stop slave;

    6.然后到从库执行mysql命令,导入数据备份

    mysql> source /tmp/mysql.bak.sql

    7.设置从库同步,注意该处的同步点,就是主库show master status信息里的| File| Position两项

    change master to master_host = '192.168.220.3', master_user = 'slave', master_port=3306, master_password='qwe123', master_log_file = 'mysqld-bin.000001', master_log_pos=3260;

    8.重新开启从同步

    mysql> start slave;

    9.查看同步状态

    mysql> show slave statusG  查看:

    Slave_IO_Running: Yes

    Slave_SQL_Running: Yes

  • 相关阅读:
    POI2012 (持续更新中)
    [BZOJ2797][Poi2012]Squarks
    [BZOJ2796][Poi2012]Fibonacci Representation
    [BZOJ2791][Poi2012]Rendezvous
    [BZOJ2795][Poi2012]A Horrible Poem
    [BZOJ2794][Poi2012]Cloakroom
    纸张概率/期望题
    2016-5-11授课
    bzoj4519: [Cqoi2016]不同的最小割
    poj3693 Maximum repetition substring
  • 原文地址:https://www.cnblogs.com/rigid/p/6247815.html
Copyright © 2011-2022 走看看