mysql8.0主从配置

主库IP:172.16.0.33

主库IP:172.16.0.33

主库配置文件如下
[root@db-1 ~]# vim /etc/my.cnf
  
[mysqld]
bind-address=0.0.0.0
server-id=1
log-bin=master-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock

log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
character-set-server=utf8mb4
[client]
socket=/data/mysql/mysql.sock
default-character-set=utf8mb4

解释
bind-address=0.0.0.0
    监听地址,监听IP地址
    
server-id=1
    主从唯一ID标识,主从的ID不能一样

log-bin=master-bin
    binlog日志名称,建议主库设置为master-bin
   
配置好以后重启mysqld服务
    ystemctl restart mysqld
    
进入数据库,回车以后输入密码
    mysql -uroot -p

创建同步账号,账号名称为rep,密码123456,授权IP段为172.16.0.%
    create user rep@'172.16.0.%' identified with mysql_native_password by '123456';
    
    grant replication slave on *.* to rep@'172.16.0.%';

查看主库节点的binlog日志名称和Position备份开始点
    show master status;
    返回结果:
    +-------------------+----------+--------------+------------------+-------------------+
    | File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +-------------------+----------+--------------+------------------+-------------------+
    | master-bin.000002 |      156 |              |                  |                   |
    +-------------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)


从库IP:172.16.0.34

从库IP:172.16.0.35

从库配置文件如下
[root@db-1 ~]# vim /etc/my.cnf
  
[mysqld]
bind-address=0.0.0.0
server-id=2
log-bin=slave-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock

log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
character-set-server=utf8mb4
[client]
socket=/data/mysql/mysql.sock
default-character-set=utf8mb4

解释
bind-address=0.0.0.0
    监听地址,监听IP地址
    
server-id=
    主从唯一ID标识,主从的ID不能一样

log-bin=slave-bin
    binlog日志名称,建议主库设置为master-bin
   
配置好以后重启mysqld服务
    ystemctl restart mysqld
    
进入数据库,回车以后输入密码
    mysql -uroot -p
  
添加主库的IP,同步的账号密码和IP,还有bin-log的名称,和Position开始备份点
    change master to master_host='172.16.0.34',master_user='rep',master_password='123456',master_log_file='master-bin.000002',master_log_pos=156;
    解释
        master_host='172.16.0.34
            主库的IP地址
            
        master_user='rep'
            主库同步的账号
         
        master_password='123456'
            主库同步的设置的密码
            
        master_log_file='master-bin.000002'
            binlog名称
            
        master_log_pos=156;
           Position同步的起始点
 
启动从库同步
    start slave;  
    
查看同步状态是否成功
    show slave status \G
返回结果:
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.0.34
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000002
          Read_Master_Log_Pos: 156
               Relay_Log_File: db-2-relay-bin.000002
                Relay_Log_Pos: 325
        Relay_Master_Log_File: master-bin.000002
             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: 156
              Relay_Log_Space: 533
              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: 7129cc53-c2df-11f0-b757-bc2411b16047
             Master_Info_File: mysql.slave_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: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set (0.00 sec)

解释:如下两个都为yes说明同步成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes



Powered By Z-BlogPHP 1.7.4

 Theme By 优美模版

本博客为开源博客,本博客联系QQ:372097243