一、MySQL主从复制原理

 主从同步过程中主服务器有一个工作线程I/O dump thread,从服务器有两个工作线程I/O thread和SQL thread;

  主服务器:

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

  从服务器:

   I/O Thread:向Master请求二进制日志事件,并保存于中继日志中;

   SQL Thread:从中继日志中读取日志事件,在本地完成重放。

 主库把外界接收的SQL请求记录到自己的binlog日志中,从库的I/O thread去请求主库的binlog日志,并将binlog日志写到中继日志中,然后从库的SQL Thread重做中继日志的SQL语句,从而达到数据复制效果。

 与复制功能相关的文件:

  master.info:用于保存slave连接至master时的相关信息,例如账号、密码、服务器地址等;

  relay-log.info:保存在当前slave节点上已经复制的当前二进制日志和本地replay log日志的对应关系。

二、主从复制相关配置实验

1、一主一从

 (1) 基本配置

  两台服务器,初始化后就配置成一主一从服务器,实验中用到两台主机,一台当作主服务器(192.168.214.17),一台作为从服务器(192.168.214.18),系统为CentOS7.6,数据库用的mariadb-server 5.5.60(光盘yum源安装)。

  1). 两台主机都安装好mariadb-server数据库

   [root@centos7 ~]# yum install -y mariadb-server 

  2). 在主服务器上配置mariadb主配置文件 /etc/my.cnf,在 [mysqld] 下添加两项 server_id=1 和 log-bin (开启二进制日志功能)

[root@centos7-17 ~]# mkdir /data/logbin    #创建二进制日志log-bin的存放路径
[root@centos7-17 ~]# chown -R mysql.mysql /data/logbin/   #修改权限
[root@centos7-17 ~]# cat /etc/my.cnf
[mysqld]
server-id=1    #添加此项,需和从服务器不同
log-bin=/data/logbin/mariadb-bin  #添加此项
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

  3). 在主服务器上启动数据库服务,并创建用于同步的账号

[root@centos7-17 ~]# systemctl start mariadb
[root@centos7-17 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 MariaDB [(none)]> reset master;  #先清理一下日志

MariaDB [(none)]> grant replication slave on *.* to repluser@'192.168.214.%' identified by 'centos';
Query OK, 0 rows affected (0.00 sec)

  4). 在从服务器上配置mariadb主配置文件 /etc/my.cnf,在 [mysqld] 下添加两项 server_id=2 和 read-only

[root@centos7-18 ~]# vim /etc/my.cnf
[mysqld]
server-id=2
read-only
...以下省略

  5). 在从服务器上使用主服务上创建的具有复制权限的用户账号连接至主服务器,并启动复制线程

[root@centos7-18 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CHANGE MASTER TO   
    ->   MASTER_HOST='192.168.214.17',  #主服务器IP
    ->   MASTER_USER='repluser',    #用于复制的用户
    ->   MASTER_PASSWORD='centos',    #用户密码
    ->   MASTER_PORT=3306,     #端口   
    ->   MASTER_LOG_FILE='mariadb-bin.000001',    #主服务器日志
    ->   MASTER_LOG_POS=245;    #开始复制的日志位置
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> start slave;    #开启复制线程
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> show slave status\G;     #查看状态
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.214.17
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000002
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000003
                Relay_Log_Pos: 531
        Relay_Master_Log_File: mariadb-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: 245
              Relay_Log_Space: 1269
              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)

ERROR: No query specified
View Code

相关文章:

  • 2021-04-04
  • 2022-12-23
  • 2021-09-08
  • 2022-01-27
  • 2022-12-23
  • 2021-07-06
  • 2021-11-18
猜你喜欢
  • 2022-02-21
  • 2021-10-04
  • 2022-01-07
  • 2021-12-04
  • 2021-12-28
  • 2021-10-16
相关资源
相似解决方案