MySQL-Replication(主从复制)

主从复制(也称 AB 复制)允许将来自一个MySQL数据库服务器(主服务器)的数据复制到一个或多个MySQL数据库服务器(从服务器)。

根据配置,您可以复制数据库中的所有数据库,所选数据库甚至选定的表。

MySQL主从复制的优点包括:

  • 横向扩展解决方案 - 在多个从站之间分配负载以提高性能。在此环境中,所有写入和更新都必须在主服务器上进行。但是,读取可以在一个或多个从设备上进行。该模型可以提高写入性能(因为主设备专用于更新),同时显着提高了越来越多的从设备的读取速度。

  • 数据安全性 - 因为数据被复制到从站,并且从站可以暂停复制过程,所以可以在从站上运行备份服务而不会破坏相应的主数据。

  • 分析 - 可以在主服务器上创建实时数据,而信息分析可以在从服务器上进行,而不会影响主服务器的性能。

  • 备份 - 可以使用从服务器数据进行备份,减轻主服务器的压力。

Replication的原理

前提是作为主服务器角色的数据库服务器必须开启二进制(binlog)日志

原理 主服务器上面的任何修改都会保存在二进制日志( Bin-log日志) 里面。 从服务器上面启动一个I/O线程,(5.5以后多线程) 连接到主服务器上面请求读取二进制(Bin-log)日志,然后把读取到的二进制日志写到本地的Realy-log(中继日志)里面。 从服务器上面同时开启一个SQL线程,读取Realy-log(中继日志),如果发现有更新立即把更新的内容在本机的数据库上面执行一遍。

配置Replication

配置步骤:

实验:
两台虚拟机(一主一从)
两台都配置hosts解析

# cat /etc/hosts

192.168.149.130 mysql-master
192.168.149.121 mysql-slave1

两端关闭防火墙,selinux

主服务器

  1. 在主服务器上,您必须启用二进制日志记录并配置唯一的服务器ID。需要重启服务器。

编辑主服务器的配置文件 my.cnf,添加如下内容

添加配置
[mysqld]

vim /etc/my.cnf
log-bin=/var/log/mysql/mysql-bin
server-id=163

创建日志目录并赋予权限

# 创建binlog日志存放路径
[root@master ~]# mkdir -p /data/binlog
# 修改权限
[root@master ~]# chown -R mysql.mysql /data/binlog
重启服务

systemctl restart mysqld

# 查询master状态
mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000002
         Position: 154
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.01 sec)
# 授权账号
mysql> grant replication slave on *.* to 'replication'@'%' identified by "Qianfeng@123456";
mysql> flush privileges;

从库

# 修改MySQL配置文件
[mysqld]
server-id = 164
# 重启MySQL
[root@slave ~]# systemctl restart mysqld
# 配置从库
mysql> change master to 
    -> master_host='master',
    -> master_port=3306,
    -> master_user='replication',
    -> master_password='Qianfeng@123456',
    -> master_log_file='mysql-bin.000001',
    -> master_log_pos=596;
# 启动slave
mysql> start slave;
# 查看slave状态
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: master
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 596
               Relay_Log_File: slave-relay-bin.000002
                Relay_Log_Pos: 762
        Relay_Master_Log_File: mysql-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: 596
              Relay_Log_Space: 969
              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: 163
                  Master_UUID: b7b92c69-65a4-11ee-9bd5-000c296b7615
             Master_Info_File: /var/lib/mysql/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: 

测试


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值