1. 创建数据库

  • 主数据库:192.168.21.100
  • 从数据库:192.168.21.101

2. 修改配置文件/etc/my.cnf

  • 主数据库:
    [mysqld]
    log-bin=mysql-bin
    server-id=100
    
  • 从数据库:
    [mysqld]
    server-id=101
    

修改完配置文件后,重启mysql服务使配置生效

3. 主库创建同步用户

  1. 在主数据库中使用如下sql语句创建从数据库使用的用户
create user 'slave'@'%' identified by '123456';
alter user 'slave'@'%' identified with mysql_native_password by '123456';
grant all privileges on *.* to 'slave'@'%' with grant option;
  1. 查看主库二进制日志(binary log)的位置
show master status;

在这里插入图片描述

4. 从库进行配置

  1. 注册从节点,依次执行下列语句
stop slave;
reset slave;
change master to master_host='192.168.21.100',master_port=3306,master_user='slave',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=1358;
start slave;
  1. 查看从库状态
 show slave status\G;

结果:

*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.21.100
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 1358
               Relay_Log_File: 5930c0943457-relay-bin.000002
                Relay_Log_Pos: 535
        Relay_Master_Log_File: mysql-bin.000003
             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: 1358
              Relay_Log_Space: 751
              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: 3306
                  Master_UUID: 7cbc0fd6-179d-11ed-99db-0242ac110003
             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: Yes
  • Slave_SQL_Running: Yes

两项为Yes,则配置成功


版权声明:本文为onoeono原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/onoeono/article/details/126245788