美文网首页数据库?mysql?oracle?程序员
windows 下 mysql5.7 的主从复制

windows 下 mysql5.7 的主从复制

作者: 忘川之曼殊沙华 | 来源:发表于2018-02-27 18:09 被阅读528次

    本次主库:192.168.100.18
    本次从库:192.168.100.45

    主库配置

    在主数据库中新建数据库:backup_test (如果不指定数据库则省略)
    编辑主数据库的my.ini文件
    在[mysqld]节点中增加如下内容:

    server_id=1 #指定唯一的ID,1至32,必须的
    log_bin=mysql-log-bin #指定二进制日志存放路径,必须的
    binlog_do_db=backup_test #指定要同步的数据库,建议加上,(如果省略,默认操作整个mysql)
    #binlog_ignore_db=mysql #指定不要同步的数据库,如果指定了binlog-do-db就不用再指定该项
    

    修改数据库之后需要重启数据库,才能使配置生效,然后建立一个备份账户,用于从库备份主库的数据。

    #1.使用cmd窗口,进入路径 %mysql%/bin,然后登陆数据库mysql -uroot -p,以下步骤都是在mysql中完成
    
    #2.运行以下命令,创建备份用户并赋予权限
      # ip的位置可以为%,代表所有ip都能连接
      # identified by 'slave' 是为备份账户设置密码
    grant replication slave on *.* to 'slave'@'192.168.100.45' identified by 'slave' ;
    
    #3.刷新权限
    flush privileges;
    
    #4.查询是否创建成功
    select user,host from mysql.user;
    +-----------+-----------+
    | user      | host      |
    +-----------+-----------+
    | root      | %         |
    | slave     | %         |
    | mysql.sys | localhost |
    | root      | localhost |
    +-----------+-----------+
    4 rows in set (0.00 sec)
    
    #5.显示主服务器的状态信息,并且找到File 和 Position 的值记录下来;
    show master status;
    +------------------+----------+--------------+------------------+-------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +------------------+----------+--------------+------------------+-------------------+
    | mysql-bin.000001 |      154 |              |                  |                   |
    +------------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)
    
    

    从库配置

    在从数据库中新建数据库:backup_test (如果不指定数据库则省略)
    然后编辑从数据库的my.ini文件
    在[mysqld]节点中增加如下内容:

    server_id=2 #指定唯一的ID,2至32,必须的,并且不能跟主数据库一样
    relay_log=relay-log  #需要设置,否则会报错
    replicate_do_db=rep_test  #指定要同步的数据库
    #replicate_ignore_db=mysql  #指定不要同步的数据库
    

    重启从数据库,让配置生效,设置登录主数据库的账号和密码等信息,然后启动slave

    #1.使用cmd窗口,进入从库路径 %mysql%/bin,然后登陆数据库mysql -uroot -p,以下步骤都是在mysql中完成
    #2.设置连接主库的信息,使用刚才新建的备份账户
      # master_host 主库ip
      # master_user 之前新建的备份用户
      # master_password 备份用户密码
      # master_log_file 对应主库状态信息中的File字段
      # master_log_pos 对应主库状态信息中的Position字段
    change master to master_host='192.168.100.18',master_user='slave',master_password='slave', master_log_file='mysql-bin.000001',master_log_pos=154;
    
    #3.接下来启动slave
    start slave;
    
    #4.查看从库状态信息
    show slave status \G;
    
      *************************** 1. row ***************************
                     Slave_IO_State: Waiting for master to send event
                        Master_Host: 192.168.100.18
                        Master_User: slave
                        Master_Port: 3306
                      Connect_Retry: 60
                    Master_Log_File: mysql-bin.000001
                Read_Master_Log_Pos: 154
                     Relay_Log_File: relay-log.000002
                      Relay_Log_Pos: 320
              Relay_Master_Log_File: mysql-bin.000001
                   Slave_IO_Running: Yes
                  Slave_SQL_Running: 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: 154
                    Relay_Log_Space: 521
                    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: 79727c64-1b7f-11e8-bc4c-509a4c0dbe20
                   Master_Info_File: D:\Program Files\MySQL\ProgramData\MySQL Server 5.7\Data\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)
    
      ERROR:
      No query specified
    **********************************************************************************************************
    以上两项: 
              Slave_IO_Running: Yes
             Slave_SQL_Running: Yes
    都为Yes,则证明配置成功
    

    测试主从复制是否有效果
    在主数据库中创建一个新的数据库(或者数据表),然后切换到从数据库查看是否同样多出同名的数据库,或者数据表。有则表示主从复制成功。

    遇到问题

    1.修改mysql配置文件时不生效

    查看修改的配置文件是否正确,我的mysql(5.7.12),安装生成的配置文件不在mysql安装目下,而是在mysql存放数据库的 /ProgramData 目录下面。另外,5.7的mysql中,不在使用“-”,而是使用“_”,注意编辑配置文件时不要出错。

    2.配置时正常,但是测试是否同步时不能成功,查看状态之后发现:

    Slave_SQL_Running: no
    报错:Last_SQL_Errno: 找不到数据库/数据表不存在
    这种情况是因为主库和从库不同步,或者可能是在从库上进行过写的操作(增删改),此时,删除主库和从库相关的数据库(为了保证清理干净),然后重新在主库中新建对应的数据库,再查看从库,同步成功。
    当Last_SQL_Errno: 0时,代表配置正确,主从复制正常。

    相关文章

      网友评论

        本文标题:windows 下 mysql5.7 的主从复制

        本文链接:https://www.haomeiwen.com/subject/sxsmxftx.html