马哥博客作业第十四周

1、MariaDB主从复制原理

  主服务器数据更新,会将操作写入到二进制日志中,主服务器会为每个从服务器的I\O Thread启动一个dump线程,用于向其发送二进制日志。

  从服务器会通过I\O Thread向主服务器请求二进制日志事件,并保存在从服务器的中继日志中,从服务器会通过自身的SQL Thread从中继日志中读取二进制事件,在本地完成重放。

 

 

2、MariaDB一主一从架构构建:准备两台纯新的CentOS7.6服务器,其中主机称为node1的服务器地址为192.168.130.132,主机称为node2的服务器地址为192.168.130.133

  主节点:192.168.130.132

  1)、mariadb配置文件修改:   

    [root@node1 ~]#vim /etc/my.cnf

    [mysqld]
    server-id=13
    log-bin=mysql-bin

    启动服务:[root@node1 ~]#systemctl start mariadb.service

  2)、查看二进制文件和位置:

    MariaDB [(none)]> show master logs;

    +------------------+-----------+
    | Log_name | File_size |
    +------------------+-----------+
    | mysql-bin.000001 | 30331 |
    | mysql-bin.000002 | 1038814 |
    | mysql-bin.000003 | 245 |
    +------------------+-----------+

  3)、创建有复制权限的用户账号:MariaDB [(none)]> grant replication slave on *.* to repluser@'192.168.%.%' identified by 'password';

 

  从节点:192.168.130.133

  1)、mariadb配置文件修改:

    [root@node2 ~]#vim /etc/my.cnf

    [mysqld]
    server-id=14
    read-only=on
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock

  2)、使用有复制权限的用户账号连接到主服务器,并启动复制线程  

    MariaDB [(none)]> CHANGE MASTER TO
      -> MASTER_HOST='192.168.130.132',
      -> MASTER_USER='repluser',
      -> MASTER_PASSWORD='password',
      -> MASTER_PORT=3306,
      -> MASTER_LOG_FILE='mysql-bin.000003',
      -> MASTER_LOG_POS=245;

    MariaDB [(none)]> start slave;

 


3、MariaDB级联复制:上一节中我们完成了一主一从的MariaDB的主从复制架构,但是有的时候,我们期望有一个后备的MariaDB的节点,用只读备份存储数据,不需要对外提供服务。为了实现该功能,我们可以在之前的一主一从架构上进行调整,即将从节点的二进制文件打开,然后给他配置一个远程同步数据用户,接着使用一台新的服务器做为从服务器的从属,同步从服务器数据的即可,下面我们在原来的架构上添加一台新的CentOS7.6,作为节点3,IP地址为192.168.132.134

  1)、需要在中间的从节点启用以下配置,让中间从节点能将master的二进制日志在本机进行数据库更新,同时也更新本机的二进制日志,实现级联复制 

    [root@node2 ~]#vim /etc/my.cnf

    [mysqld]
    server-id=14
    read-only=on
    log-bin=mysql-bin
    log-slave-updates

    重启服务:[root@node2 ~]#systemctl restart mariadb.service 

  2)、查看中间从节点二进制文件和位置

    MariaDB [(none)]> show master logs;
    +------------------+-----------+
    | Log_name | File_size |
    +------------------+-----------+
    | mysql-bin.000001 | 245 |
    +------------------+-----------+

  3)、引入从节点3:192.168.132.134

    mariadb配置文件修改:    

    [root@localhost ~]#vim /etc/my.cnf

    [mysqld]
    server-id=15
    read-only=on

  4)、使用有复制权限的用户账号连接到中间从服务器,并启动复制线程 

    MariaDB [(none)]> CHANGE MASTER TO
      -> MASTER_HOST='192.168.130.133',
      -> MASTER_USER='repluser',
      -> MASTER_PASSWORD='password',
      -> MASTER_PORT=3306,
      -> MASTER_LOG_FILE='mysql-bin.000001',
      -> MASTER_LOG_POS=245;

    MariaDB [(none)]> start slave;

    


4、MariaDB半同步复制

  CentOS7实现MariaDB5.5.65的半同步复制

  主服务器配置:

    MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';

    MariaDB [(none)]> set global rpl_semi_sync_master_enabled=1;

    MariaDB [(none)]> set global rpl_semi_sync_master_timeout=3000;   

    MariaDB [(none)]> show global variables like '%semi%';
    +------------------------------------+-------+
    | Variable_name | Value |
    +------------------------------------+-------+
    | rpl_semi_sync_master_enabled | ON |
    | rpl_semi_sync_master_timeout | 3000 |
    | rpl_semi_sync_master_trace_level | 32 |
    | rpl_semi_sync_master_wait_no_slave | ON |
    +------------------------------------+-------+

  从服务器配置:

    MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';

  主节点上查看半同步复制状态:  

    MariaDB [(none)]> show global status like '%semi%';
    +--------------------------------------------+-------+
    | Variable_name | Value |
    +--------------------------------------------+-------+
    | Rpl_semi_sync_master_clients | 1 |
    | Rpl_semi_sync_master_net_avg_wait_time | 0 |
    | Rpl_semi_sync_master_net_wait_time | 0 |
    | Rpl_semi_sync_master_net_waits | 0 |
    | Rpl_semi_sync_master_no_times | 1 |
    | Rpl_semi_sync_master_no_tx | 1 |
    | Rpl_semi_sync_master_status | ON |
    | Rpl_semi_sync_master_timefunc_failures | 0 |
    | Rpl_semi_sync_master_tx_avg_wait_time | 0 |
    | Rpl_semi_sync_master_tx_wait_time | 0 |
    | Rpl_semi_sync_master_tx_waits | 0 |
    | Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
    | Rpl_semi_sync_master_wait_sessions | 0 |
    | Rpl_semi_sync_master_yes_tx | 0 |
    +--------------------------------------------+-------+

 

  #测试:

    1.在master实现创建数据库,立即成功

    MariaDB [(none)]> create database db1;
    Query OK, 1 row affected (0.00 sec)

    2.在所有slave节点上,停止复制线程  

    MariaDB [(none)]> stop slave;
    Query OK, 0 rows affected (0.01 sec)

    3.在master实现创建数据库,等待3秒后成功

    MariaDB [(none)]> create database db2;
    Query OK, 1 row affected (3.00 sec)

 


5、MariaDB高可用方案MHA:准备三台新安装的CentOS7.6服务器,并使用yum安装MariaDB10.2.23,主机名分别为node1,node2,node3,IP地址分别为10.0.0.213-215

  1)、在管理节点上安装两个包

    [root@manager ~]#yum -y install mha4mysql-manager-0.56-0.el6.noarch.rpm

    [root@manager ~]#yum -y install mha4mysql-node-0.56-0.el6.noarch.rpm

    在所有MariaDB服务器上安装一个包,例:

    [root@node1 ~]#yum -y install mha4mysql-node-0.56-0.el6.noarch.rpm

  2)、在所有节点实现相互ssh-key验证

    [root@manager ~]#ssh-keygen

    [root@manager ~]#ssh-copy-id 10.0.0.211

    [root@manager ~]#rsync -a .ssh 10.0.0.213:/root/

    [root@manager ~]#rsync -a .ssh 10.0.0.214:/root/

    [root@manager ~]#rsync -a .ssh 10.0.0.215:/root/

  3)、在管理节点上建立配置文件

    [root@manager ~]#mkdir /etc/mastermha   

    [root@manager ~]#vim /etc/mastermha/app1.cnf

    [server default]
    user=mhauser
    password=123456
    manager_workdir=/data/mastermha/app1/
    manager_log=/data/mastermha/app1/manager.log
    remote_workdir=/data/mastermha/app1/
    ssh_user=root
    repl_user=repluser
    repl_password=123456
    ping_interval=1
    master_ip_failover_script=/usr/local/bin/master_ip_failover
    report_script=/usr/local/bin/sendmail.sh
    check_repl_delay=0
    [server1]
    hostname=10.0.0.213
    [server2]
    hostname=10.0.0.214
    [server3]
    hostname=10.0.0.215
    candidate_master=1

  4)、实现master   

    [root@node1 ~]#vim /etc/my.cnf

    [mysqld]
    server-id=13
    log-bin=mysql-bin
    skip-name-resolve=1 

    MariaDB [(none)]> show master logs; 

    MariaDB [(none)]> grant replication slave on *.* to repluser@'10.0.0.%' identified by '123456';  

    MariaDB [(none)]> grant all on *.* to mhauser@'10.0.0.%' identified by '123456';

    #配置VIP   

    [root@node1 ~]#ifconfig eth0:1 10.0.0.100/24

  5)、实现slave   

    [root@node2 ~]#vim /etc/my.cnf

    [mysqld]
    server-id=14
    read-only=on
    log-bin=mysql-bin
    relay-log-purge=0
    skip-name-resolve=1

    MariaDB [(none)]> CHANGE MASTER TO
      -> MASTER_HOST='10.0.0.213',
      -> MASTER_USER='repluser',
      -> MASTER_PASSWORD='123456',
      -> MASTER_PORT=3306,
      -> MASTER_LOG_FILE='mysql-bin.000001',
      -> MASTER_LOG_POS=245;

    MariaDB [(none)]> start slave;

  6)、检查mha的环境

    masterha_check_ssh --conf=/etc/masterha/app1.cnf

    masterha_check_repl --conf=/etc/masterha/app1.cnf

  7)、启动mha

    nohup masterha_manager --conf=/etc/masterha/app1.cnf

    masterha_check_status --conf=/etc/masterha/app1.cnf

 

posted @ 2020-08-31 20:04  富儿代  阅读(112)  评论(0编辑  收藏  举报