运维-2.主从复制

1.概述

在这里插入图片描述

2.原理

在这里插入图片描述

3.搭建主从复制结构

3.1 搭建

在这里插入图片描述

3.2 主库配置

vi /etc/my.cnf
server-id=1
read-only=0

在这里插入图片描述
在这里插入图片描述


mysql>  create user 'demo'@'%' IDENTIFIED WITH mysql_native_password by 'Demo@123456';
Query OK, 0 rows affected (0.01 sec)

mysql> grant replication slave on *.* to 'demo'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 |     1363 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.02 sec)

3.3 从库配置

在这里插入图片描述

server-id=2
read-only=1

在这里插入图片描述

mysql> change replication source to 
source_host='10.4.7.6',
source_user='demo',
source_password ='Demo@123456', 
source_log_file='binlog.000003',
source_log_pos=157;      
                                                     

STOP SLAVE;   
CHANGE MASTER TO  
  MASTER_HOST='10.4.7.6',  
  MASTER_USER='demo',  
  MASTER_PASSWORD='Demo@123456',  
  MASTER_LOG_FILE='binlog.000003',  
  MASTER_LOG_POS=157;  
  
START SLAVE;

在这里插入图片描述

4.开启同步操作

start replica; #8.0.22之前
start salve; #8.0.22之后

5.查看主从同步状态

show replica status; #8.0.22之前
show slave status; #8.0.22之后

Replica_IO_Running: Yes
Replica_SQL_Running: Yes

mysql> show replica status\G;
*************************** 1. row ***************************
             Replica_IO_State: Waiting for source to send event
                  Source_Host: 10.4.7.6
                  Source_User: demo
                  Source_Port: 3306
                Connect_Retry: 60
              Source_Log_File: binlog.000003
          Read_Source_Log_Pos: 157
               Relay_Log_File: k8smaster-relay-bin.000002
                Relay_Log_Pos: 323
        Relay_Source_Log_File: binlog.000003
           Replica_IO_Running: Yes
          Replica_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_Source_Log_Pos: 157
              Relay_Log_Space: 537
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Source_SSL_Allowed: No
           Source_SSL_CA_File:
           Source_SSL_CA_Path:
              Source_SSL_Cert:
            Source_SSL_Cipher:
               Source_SSL_Key:
        Seconds_Behind_Source: 0
Source_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Source_Server_Id: 1
                  Source_UUID: 4a28f2cd-5ae3-11ef-9766-000c2913ad31
             Source_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
    Replica_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Source_Retry_Count: 86400
                  Source_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Source_SSL_Crl:
           Source_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Source_TLS_Version:
       Source_public_key_path:
        Get_Source_public_key: 0
            Network_Namespace:
1 row in set (0.00 sec)

4.测试

在这里插入图片描述

create database db01;
use db01;
create table tb_user(
	id int primary key auto_increment ,
	name varchar(50) not null,
	sex varchar(1)
)engine=innodb default charset=utf8mb4;

insert into tb_user(id,name,sex) values(null,'Tom','1'),(null,'Lisa','0'),(null,'hahha','1');

总结

在这里插入图片描述

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值