mysql 主从笔记

梦浪的小虾米
2021-12-16 / 1 评论 / 2,778 阅读 / 正在检测是否收录...

工作有几年了,可是还是还是在写着简单的 curd,这不行,得搞一下 mysql 主从

准备

两台带有 mysql 的服务器(意思就是不管你是linux还是window,只要有 mysql 就行)

两台服务器的 ip 地址为 192.168.123.57192.168.123.58

其中 192.168.123.58为主数据库

192.168.123.57为从数据库

本文中,主从环境均为 linux 下的 ubuntu 环境(为了方便学习,本文是使用的 docker 搭建的 mysql 8.0)

正文

  1. 配置主数据库

    • 找到 mysql 的配置 . 本文的配置文件名为 my.cnf
    • 打开配置文件,通过搜索找到 [mysqld],然后换行并添加如下配置:

      # 下文中 xxx 为文件目录,请自行替换. 推荐: /var/log/mysql
      server-id = 1  
      #mysql-bin 会自动生成 mysql-bin.000001
      log-bin = 'xxxx/mysql-bin' 
      # 错误日志 
      log-error= 'xxxx/mysql-error';
      # 主从同步忽略的数据库 
      binlog-ignore = mysql
      # 主从同步需要同步的数据库
      binlog-do-db = test
  • 给从数据库库授权 [ mysql 命令行中执行该命令]

    CREATE USER 'root'@'192.168.123.%' IDENTIFIED BY 'mytest';
    
    GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.123.%';
  • 刷新授权,让授权生效- 可有可无 [ mysql 命令行中执行该命令]

    FLUSH PRIVILEGES;
  • 重启数据库服务

    # 网上推荐这个重启 mysql 服务。我为了让格式好看,自然也就写了这个,其实我是 docker,所以不存在这种启动方式,我直接重启容器
    mysql.server restart
  • 查看 master 是否配置成功 [ mysql 命令行中执行该命令]

    show master status \G;
  • 查看 从数据库的信息 [ mysql 命令行中执行该命令]

    show master hosts \G;
  1. 配置从数据库

    • 从数据库的配置,文件名字:mysql.cnf
    # 下文中 xxx 为文件目录,请自行替换. 推荐: /var/log/mysql
    server-id = 1  
    #mysql-bin 会自动生成 mysql-bin.000001
    log-bin = 'xxxx/mysql-bin' 
    # 错误日志 
    log-error= 'xxxx/mysql-error';
    需要同步的数据库 
    replicate-do-db=test
    • 从数据库也需要授权 [ mysql 命令行中执行该命令]

      change master to
      master_host='192.168.123.58',
      master_user='root',
      master_password='mytest',
      master_log_file='mybin-log.000001' ,
      master_log_pos=323;  
      # 以上是一个完整的一行命令, master_host 为主数据库的地址,master_user 为主数据库的账户,master_password 为主数据库的密码,master_log_file 为主数据库的二进制文件的名字,master_log_pos 为主数据库log文件里面的行号,可以通过 <code>show master status \G</code>获取对应的行号
    • 从数据库也需要重启数据库服务,和主服务器的一致
    • 在从服务器上开启同步进程 [ mysql 命令行中执行该命令]

      start slave;
    • 查看同步状态 [ mysql 命令行中执行该命令]

      show slave status;
      
      # 会得到如下结果-仅供参考 
      mysql> show slave status \G;
      *************************** 1. row ***************************
                     Slave_IO_State: Waiting for source to send event
                        Master_Host: 192.168.123.58
                        Master_User: root
                        Master_Port: 3306
                      Connect_Retry: 60
                    Master_Log_File: mybin-log.000001
                Read_Master_Log_Pos: 632
                     Relay_Log_File: 044ebe077274-relay-bin.000004
                      Relay_Log_Pos: 633
              Relay_Master_Log_File: mybin-log.000001
                   Slave_IO_Running: Yes
                  Slave_SQL_Running: Yes
                    Replicate_Do_DB: test
                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: 632
                    Relay_Log_Space: 849
                    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: db775ba0-16f4-11ec-9463-0242ac150002
                   Master_Info_File: mysql.slave_master_info
                          SQL_Delay: 0
                SQL_Remaining_Delay: NULL
            Slave_SQL_Running_State: Replica 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: 
             Master_public_key_path: 
              Get_master_public_key: 0
                  Network_Namespace: 
      1 row in set, 1 warning (0.00 sec)
      

      此处需要注意:查看 Slave_IO_Running 和 Slave_SQL_Running 。必须让这两个值都为 yes 才算是成功。如果不是 yes。 可以查看 Last_io_Errno 上显示的错误,然后根据错误进行修改

  1. 在主数据库进行写操作测试,查看从数据库是否有相同的数据。
    如下所示:

0

评论 (1)

取消
  1. 头像
    姜辰Jcs.Moe
    Windows 10 · FireFox

    大佬这个不错啊,收藏学习下。咳~

    我好像知道老虎以前cn和net两个站怎么同步的了。

    回复