声明:本站文章均为作者个人原创,图片均为实际截图。如有需要请收藏网站,禁止转载,谢谢配合!!!

准备服务器

下载附件

使用前两台进行配置

主库:43.137.7.105 root 81dd340517a86f21
从库:1.13.21.205

两台服务器、均新建一个数据库 badianboke, 新建数据表 user, 结构如下

下载附件

1、主库操作

server-id = 1
log-bin=mysql-bin
binlog_do_db=badianboke

如图所示

下载附件

进入mysql终端

mysql uroot -p

授权从服务器账号密码

grant replication slave on *.* to ‘主库账号’@’从库IP地址’ identified by ‘主库密码’;

grant replication slave on *.*  to 'root'@'1.13.21.205' identified by '81dd340517a86f21';

刷新配置

flush privileges;

查看主库状态

show master status;

如图

下载附件

2、从库操作

与主库区别是 replicate_do_db

server-id = 2
log-bin=mysql-bin
replicate_do_db=badianboke

如图所示

下载附件

进入mysql终端

mysql uroot -p

配置从库
master_log_file master_log_pos 这两项在上面的主库状态中可以看到
change master to master_host=’主库ip地址’,master_port=3306,master_user=’主库账号’,master_password=’主库密码’,master_log_file=’mysql-bin.000006’,master_log_pos=1317;

change master to master_host='43.137.7.105',master_port=3306,master_user='root',master_password='81dd340517a86f21',master_log_file='mysql-bin.000006',master_log_pos=1317;

如图所示

下载附件

详细信息

 Slave_IO_State: Waiting for master to send event
                  Master_Host: 43.137.7.105
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000006
          Read_Master_Log_Pos: 1872
               Relay_Log_File: VM-16-6-centos-relay-bin.000002
                Relay_Log_Pos: 838
        Relay_Master_Log_File: mysql-bin.000006
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: badianboke
          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: 1872
              Relay_Log_Space: 1020
              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: 8758eeef-b282-11ed-8439-5254004e1e11
             Master_Info_File: /www/server/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           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

开启从库

start slave;

此时在主库中新增两条数据,就可以在从库中看到了

下载附件

参考链接

https://blog.csdn.net/qq15577969/article/details/123028302