mysql主从复制架构及实现

MySQL主从复制架构及实现

1、原理:

复制功能及作用:

数据分布
负载均衡:进行读操作的负载均衡,适用于读密集型的应用
可以用于备份
高可用和故障切换
MySQL的升级测试

主从复制:

从服务器:
    IO线程:从master请求二进制日志信息,并保存至中继日志;
    SQL线程:从relay log中读取日志信息,在本地完成重放;

主节点:
    dump Thread:为每个Slave的I/O Thread启动一个dump线程,用于向其发送binary log events;

特点:
    异步模式:async
        1、从服务器落后于主服务器
        2、主从数据不一致现象比较常见

复制架构:
    M/S主从,M/M主主,环状复制

    一主多从:
    从服务器还可以再有从服务器

二进制日志的事件记录格式:SET datetime = now()
    1、基于行 ROW
    2、基于语句  STATEMENT
    3、混合    MIXED

2、配置过程:

主从

master
    (1)启动二进制日志;
        [mysqld]配置文件中添加
        log-bin=master-bin
    (2)设置一个当前群集中唯一的server-id;
        [mysqld]配置文件中添加
        server_id=#
    (3)创建一个有复制权限的账号(REPLICATION SLAVE,REPLICATION CLIENT);
        GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘@‘HOST‘ IDENTIFIED BY ‘replpass‘;
    进程:binlog dump

slave
    (1)启用中继日志;
        [mysqld]配置文件中添加
        relay_log=relay-log
        relay_log_index=relay-log.index
    (2)设置一个在当前集群中的唯一的server-id;
        [mysqld]配置文件中添加
        server_id=#
    (3)使用有复制权限用户账号连接至主服务器,并启动复制线程;
    进程:IO thread,SQL thread

实验:
架构  主服务器地址192.168.150.137   从服务器地址192.168.159.138
两台mariadb均通过yum安装,版本相同mariadb-5.5.52-1.el7.x86_64

主节点 
1、修改配置文档,添加参数
vim /etc/my.cnf
在[mysqld]中添加如下几行
log-bin=master-bin
server-id=1
innodb_file_per_table=ON
skip_name_resolve=ON

2、开启数据库,查看状态信息
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%log%‘;
log_bin                                   | ON 
MariaDB [(none)]> SHOW MASTER LOGS;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| master-bin.000001 |     30379 |
| master-bin.000002 |   1038814 |
| master-bin.000003 |       245 |
+-------------------+-----------+
3 rows in set (0.00 sec)

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%server%‘;
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| character_set_server | latin1            |
| collation_server     | latin1_swedish_ci |
| server_id            | 1                 |
+----------------------+-------------------+
3 rows in set (0.00 sec)    

3、授权账号
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘ @ 192.168.%.%‘ IDENTIFIED BY ‘replpass‘;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

从节点
1、修改配置文档,添加参数
vim /etc/my.cnf
在[mysqld]中添加如下几行
relay-log=relay-log
relay-log-index=relay-log.index
server-id=2
innodb_file_per_table=ON
skip_name_resolve=ON
2、开启数据库,查看状态
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%log%‘;
| relay_log                                 | relay-log                                   
                                                             || relay_log_index                           | relay-log.index       
3、从节点指定主节点,要注意bin日志和position,信息来自于主节点SHOW MASTER LOGS
CHANGE MASTER TO MASTER_HOST=‘192.168.150.137‘,MASTER_USER=‘repluser‘,MA
STER_PASSWORD=‘replpass‘,MASTER_LOG_FILE=‘master-bin.000003‘,MASTER_LOG_POS=245;

4、通过目录查看从节点状态,此时bin log已指定完成,Slave_IO_Running和Slave_SQL_Running进程还没有开启
MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.150.137
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 245
               Relay_Log_File: relay-log.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: master-bin.000003
             Slave_IO_Running: No
            Slave_SQL_Running: No
              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: 245
              Relay_Log_Space: 245
              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: NULL
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: 0
1 row in set (0.00 sec)

5、开启SLAVE进程,此时IO进程和SQL进程均为YES状态
MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.150.137
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 497
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 782
        Relay_Master_Log_File: master-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: 497
              Relay_Log_Space: 1070
              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
1 row in set (0.00 sec)

功能验证:
主节点进行测试库创建
MariaDB [(none)]> CREATE DATABASE mydb;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> SHOW MASTER LOGS;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| master-bin.000001 |     30379 |
| master-bin.000002 |   1038814 |
| master-bin.000003 |       580 |
+-------------------+-----------+
3 rows in set (0.00 sec)

从库进行同步验证:mydb库已经过来,状态信息中bin log也应用过来了
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

[(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.150.137
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 580
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 865
        Relay_Master_Log_File: master-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: 580
              Relay_Log_Space: 1153
              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
1 row in set (0.00 sec)

另:从库的数据文件夹中会记录一些信息,master.info会记录我连的主库的地址,bin log信息等
[[email protected] ~]# ls /var/lib/mysql/
aria_log.00000001  ib_logfile0  mydb        performance_schema  relay-log.index
aria_log_control   ib_logfile1  mysql       relay-log.000001    relay-log.info
ibdata1            master.info  mysql.sock  relay-log.000002    test
[[email protected] ~]# cd /var/lib/mysql/
[[email protected] mysql]# file master.info 
master.info: ASCII text
[[email protected] mysql]# cat master.info 
18
master-bin.000003
581
192.168.150.137
repluser
replpass
3306
60
0
时间: 2024-08-04 16:47:57

mysql主从复制架构及实现的相关文章

MySQL主从复制架构使用方法

一. 单个数据库服务器的缺点 数据库服务器存在单点问题 数据库服务器资源无法满足增长的读写请求 高峰时数据库连接数经常超过上限 二. 如何解决单点问题 增加额外的数据库服务器,组建数据库集群 同一集群中的数据库服务器需要具有相同的数据 集群中的任一服务器宕机后,其它服务器可以取代宕机服务器 三. MySQL主从复制架构 1. 主库将变更写入到主库的binlog中 一些MySQL版本并不会开启二进制日志,所以一定要检查是否开启 如果刚开始没有开启,后面再进行开启的话,需要重启数据库才能生效,而且数

【大型网站技术实践】初级篇:搭建MySQL主从复制经典架构 一、业务发展驱动数据发展

一.业务发展驱动数据发展 随着网站业务的不断发展,用户量的不断增加,数据量成倍地增长,数据库的访问量也呈线性地增长.特别是在用户访问高峰期间,并发访问量突然增大,数据库的负载压力也会增大,如果架构方案不够健壮,那么数据库服务器很有可能在高并发访问负载压力下宕机,造成数据访问服务的失效,从而导致网站的业务中断,给公司和用户造成双重损失.那么,有木有一种方案能够解决此问题,使得数据库不再因为负载压力过高而成为网站的瓶颈呢?答案肯定是有的. 目前,大部分的主流关系型数据库都提供了主从热备功能,通过配置

42-4 mysql主从复制

04 mysql主从复制架构及实现 实战:主主复制 [[email protected] ~]# systemctl stop mariadb.service  [[email protected] ~]# systemctl stop mariadb.service [[email protected] ~]# rm -rf /var/lib/mysql/* [[email protected] ~]# rm -rf /var/lib/mysql/* [[email protected] ~]

MySQL主从复制、读写分离、高可用集群搭建

MySQL主从复制.读写分离.高可用集群搭建  一.服务介绍   1.1 Keepalived     Keepalived,见名知意,即保持存活,其目的是解决单点故障,当一台服务器宕机或者故障时自动切换到其他的服务器中.Keepalived是基于VRRP协议实现的.VRRP协议是用于实现路由器冗余的协议,VRRP协议将两台或多台路由器设备虚拟成虚拟设备,可以对外提供虚拟路由器IP(一个或多个),即漂移IP(VIP). 1.2 ProxySQL ProxySQL是一个高性能,高可用性的MySQL

MySql主从复制简单案例实现

mysql的主从复制原理 在mysql主从复制架构中,有一台服务器作为MASTER服务器,该服务器负责所有的读请求和写请求.另外一台或多台作为slave服务器.当master上的某个应用程序发起写请求时,该请求会被内核响应并在内核中执行,然后在将其数据写入到磁盘中去.并且将此次的操作以事件的形式记录到二进制文件中去.此时master上的Binlog  dump thread等待slave上的I/O thread连接请求.一旦slave I/O thread连接上了master的Binlog du

MySQL主从复制:半同步、异步

MySQL主从复制:半同步.异步 大纲 前言 如何对MySQL进行扩展? MySQL Replication WorkFlow MySQL主从复制模式 实战演练 MySQL异步复制实现 MySQL半同步复制实现 实验中的思考 总结 前言 本篇我们介绍MySQL Replication的相关内容, 我们首先介绍MySQL CLuster的实现原理和如何一步步构建一个MySQL Replication Cluster 看懂本文需要了解: MySQL基本操作,MySQL日志类型及其作用 如何对MySQ

MySQL主从复制杂记(1)

mysql主从复制架构及实现 主从配置 0.master1为主,master2为从节点 1.开启主节点的二进制日志.serverID [[email protected] ~]# vim /etc/my.cnf [mysqld] log_bin=master-bin server-id=1 innodb-file-per-table=ON skip_name_resolve=ON systemctl start mariadb.service MariaDB [(none)]> SHOW GLO

MySQL主从复制杂记(2)

MySQL主从复制架构及实现 杂项 1.设置从节点为只读模式 MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE 'read_only'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | read_only | OFF | +---------------+-------+ 从节点: 查看复制的主节点信息文件. [[email protected]

Mysql中间件Proxysql实现mysql主从架构读写分离

实验主机 Master: 192.168.0.17 Slave: 192.168.0.20 Proxysql:192.168.0.30建议关闭防火墙建立Master: 192.168.0.17与Slave: 192.168.0.20的MySQL主从复制架构 Proxysql:192.168.0.30主机上基于yum安装proxysql [[email protected] ~ 20:21:05]#cat <<EOF | tee /etc/yum.repos.d/proxysql.repo &g