zoukankan      html  css  js  c++  java
  • mysql 主从同步

    记录mysql主从同步的实验过程。

    环境:win10(主库) centos7(从库) 

    在win10上用Navicat访问centos的mysql,发现无法访问。

    先用ping命令发现可以ping通,telnet 3306端口不通,想到可能是防火墙的问题。

    centos7默认使用firewall,不是iptables

    查看firewall状态 $ firewall-cmd --state 结果:running

    关闭防火墙:systemctl stop firewalld.service

    禁止防火墙开机启动:systemctl disable firewalld.service

    关闭防火墙之后Navicat可以访问了。

    设置主库mysql配置文件 my.ini(我的路径是**/MySQL Server 5.7/my.ini)

    server-id=1  --这个指定的是server-id一定要唯一

    log-bin=mysql-bin  --开启二进制日志

    binlog-do-db = test  --要同步的库

    还有一个参数是binlog-ignore-db  --不同步哪些库,我这里没有用。

    设置完成后重启MySql服务

    MySql服务

    在主库上给从库建一个用来同步数据的用户

    CREATE USER 'synchro'@'192.168.3.109' IDENTIFIED BY 'slavepass';
    GRANT REPLICATION SLAVE ON *.* TO 'synchro'@'192.168.3.109';
    flush PRIVILEGES

    执行 SHOW MASTER STATUS 查看一下当前的二进制日志文件和文件内容的当前位置

    配置从库

    修改mysql配置文件my.cnf (我的路径是/etc/my.cnf)

    添加 server_id = 2

    重启mysql

    systemctl stop mysqld

    systemctl start mysqld

    登录到mysql 

    mysql -u root -p 输入密码后登录成功。

    mysql> CHANGE MASTER TO
        -> MASTER_HOST='192.168.3.109',MASTER_USER='synchro',MASTER_PASSWORD='slavepass',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=2122;

    MASTER_USER是在主库建的那个用户,MASTER_PASSWORD是登录密码,MASTER_LOG_FILE是上图中的File列的值,MASTER_LOG_POS=2122是上图中Position的值。

    启动主从同步

    START slave

    查看主从同步状态(不加G的话输出的内容会很乱)

    show slave statusG
    返回结果
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.3.109
                      Master_User: synchro
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000003
              Read_Master_Log_Pos: 2122
                   Relay_Log_File: localhost-relay-bin.000002
                    Relay_Log_Pos: 320
            Relay_Master_Log_File: mysql-bin.000003
                 Slave_IO_Running: Yes  //这两个都为YES时,才可以同步数据
                Slave_SQL_Running: Yes  //这两个都为YES时,才可以同步数据
                  Replicate_Do_DB: 
              Replicate_Ignore_DB: 
               Replicate_Do_Table: 
           Replicate_Ignore_Table: 
          Replicate_Wild_Do_Table: 
      Replicate_Wild_Ignore_Table: 
                       Last_Errno: 0
                       Last_Error: 
                     Skip_Counter: 0
              Exec_Master_Log_Pos: 2122
                  Relay_Log_Space: 531
                  Until_Condition: None
                   Until_Log_File: 
                    Until_Log_Pos: 0
               Master_SSL_Allowed: No
               Master_SSL_CA_File: 
               Master_SSL_CA_Path: 
                  Master_SSL_Cert: 
                Master_SSL_Cipher: 
                   Master_SSL_Key: 
            Seconds_Behind_Master: 0
    Master_SSL_Verify_Server_Cert: No
                    Last_IO_Errno: 0
                    Last_IO_Error: 
                   Last_SQL_Errno: 0
                   Last_SQL_Error: 
      Replicate_Ignore_Server_Ids: 
                 Master_Server_Id: 1
                      Master_UUID: eb2966eb-4184-11e8-a79a-4ccc6af9c3f6
                 Master_Info_File: /var/lib/mysql/master.info
                        SQL_Delay: 0
              SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
               Master_Retry_Count: 86400
                      Master_Bind: 
          Last_IO_Error_Timestamp: 
         Last_SQL_Error_Timestamp: 
                   Master_SSL_Crl: 
               Master_SSL_Crlpath: 
               Retrieved_Gtid_Set: 
                Executed_Gtid_Set: 
                    Auto_Position: 0
             Replicate_Rewrite_DB: 
                     Channel_Name: 
               Master_TLS_Version: 
    1 row in set (0.00 sec)
    Slave_IO_Running这个状态可能是Connecting。此时要仔细检查上边change master部分的参数有没有写错。

    此时可以尝试在主库插入一条数据,刷新从库看到插入的数据即为成功。

    测试:
    stop slave 关闭主从同步后,向主库插入数据。再启动主从同步,数据会同步,不会丢失。
  • 相关阅读:
    soap-学习
    Redis教程
    Java 不可变对象
    java volatile
    Go语言
    mongodb 基础语法
    Linux 命令大全
    Java json字符串对比
    sqlserver 数据库阻塞和死锁
    http 和 https 的区别
  • 原文地址:https://www.cnblogs.com/xushy/p/9179059.html
Copyright © 2011-2022 走看看