mysql主从复制+半同步+mycat读写分离+MHA高可用

mysql主从复制

1、新数据库主从复制

数据库是新建的使用,已经使用过一段时间的数据库不适合
建议从节点也开启二进制日志(如果主节点出现故障,从节点提升为主节点后,才会有二进制日志,就行复制)
主节点需要创建用户并授权
更改server_id,默认为1会导致日志冲突
查二进制位置
主节点:
(1)server_id
(2)二进制开启
(3)查二进制位置
(4)创建用户账号
从节点:
(1)server_id
(2)用户,位置
(3)开启线程

主节点配置
#安装mysql数据库
[root@Rokey8 ~]#yum -y install mysql-server
#设置主节点配置
[root@Rokey8 ~]#vim /etc/my.cnf
[mysqld]
server-id=8
log-bin=/data/mysql/logbin/mysql-bin #二进制日志路径

[root@Rokey8 ~]#mkdir -pv /data/mysql/logbin/
[root@Rokey8 ~]#chown -R mysql.mysql /data/mysql/
[root@Rokey8 ~]#systemctl enable --now mysqld
[root@Rokey8 ~]#ll /data/mysql/logbin/
total 8
-rw-r----- 1 mysql mysql 156 Aug 24 22:39 mysql-bin.000001
-rw-r----- 1 mysql mysql  36 Aug 24 22:39 mysql-bin.index
[root@Rokey8 ~]#mysql
#查看二进制位置
mysql> show master logs;
+------------------+-----------+-----------+
| Log_name         | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000001 |       156 | No        |
+------------------+-----------+-----------+
1 row in set (0.00 sec)
#创建用户和授权
mysql> create user repluser@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to repluser@'10.0.0.%';
Query OK, 0 rows affected (0.00 sec)

从节点配置
#安装mysql
[root@Rokey8 ~]#yum -y install mysql-server
#从节点配置文件
[root@Rokey8 ~]#vim /etc/my.cnf
[mysqld]
server_id=18
read-only
log-bin=/data/mysql/logbin/mysql-bin
[root@Rokey8 ~]#mkdir -pv /data/mysql/logbin/
[root@Rokey8 ~]#chown -R mysql.mysql /data/mysql/
[root@Rokey8 ~]#systemctl enable --now mysqld
[root@Rocky8 ~]# ll /data/mysql/logbin/
total 12
-rw-r----- 1 mysql mysql 179 Aug 24 22:52 mysql-bin.000001
-rw-r----- 1 mysql mysql 156 Aug 24 22:52 mysql-bin.000002
-rw-r----- 1 mysql mysql  72 Aug 24 22:52 mysql-bin.index

#设置change master to
#查看change master to的格式
mysql> help change master to
......
CHANGE MASTER TO
  MASTER_HOST='source2.example.com',
  MASTER_USER='replication',
  MASTER_PASSWORD='password',
  MASTER_PORT=3306,
  MASTER_LOG_FILE='source2-bin.001',
  MASTER_LOG_POS=4,
  MASTER_CONNECT_RETRY=10;
......
mysql> show slave status;
Empty set, 1 warning (0.00 sec)
#设置change master to
mysql> CHANGE MASTER TO
    ->   MASTER_HOST='10.0.0.8',
    ->   MASTER_USER='repluser',
    ->   MASTER_PASSWORD='123456',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='mysql-bin.000006',
    ->   MASTER_LOG_POS=156,
    ->   MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected, 10 warnings (0.02 sec)
#开启slave线程
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
#查看slave状态
mysql> show slave status\G;
#查看用户是否复制到从节点
mysql> select user,host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| repluser         | 10.0.0.%  |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

CHANGE MASTER TO
MASTER_HOST='10.0.0.8',
MASTER_USER='repluser',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000007',
MASTER_LOG_POS=156;

2、旧数据库的主从复制

将已有的MySQL8.0单机架构变成主从复制架构
主节点
#修改master主节点的配置
[root@centos8 ~]#vim /etc/my.cnf
[mysqld]
server-id=8
log-bin=/data/mysql/logbin/mysql-bin

[root@Rokey8 ~]#mkdir -pv /data/mysql/logbin/
[root@Rokey8 ~]#chown -R mysql.mysql /data/mysql/
[root@Rokey8 ~]#systemctl restart mysqld

#完全备份
[root@centos8 ~]#mysqldump -A -F --master-data=1 --single-transaction > /data/all.sql
#创建复制用户并授权
mysql>create user repluser@"10.0.0.%" identified by "123456"
mysql>grant replication slave on *.* to repluser@"10.0.0.%"";
#查看二进制位置
mysql> show master logs;
+------------------+-----------+-----------+
| Log_name         | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000002 |       156 | No        |
+------------------+-----------+-----------+
1 row in set (0.00 sec)
#将备份复制到从节点
[root@centos8 ~]#scp /data/all.sql 10.0.0.18:/data
#配置从节点
[root@centos8 ~]#vim /etc/my.cnf
[mysqld]
server-id=18
read-only
[root@centos8 ~]#systemctl restart mysqld
#从节点修改备份文件
[root@centos8 ~]#vim /data/all.sql 
......
CHANGE MASTER TO
  MASTER_HOST='10.0.0.8',
  MASTER_USER='repluser',
  MASTER_PASSWORD='123456',
  MASTER_PORT=3306,
  MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=156; 
......
#从节点还原备份
mysql> set sql_log_bin=0;
mysql> source /data/all.sql;
mysql>set sql_log_bin=1; 
#从节点开始复制
mysql> start slave;

 半同步

前提:已经实现主从复制10.0.0.8主;10.0.0.18/10.0.0.28从;
安装半同步插件 --> 配置/etc/my.cnf文件(开启插件、设置超时时间) --> 重启mysql服务

主节点
#主节点安装半同步插件(先安装插件,再/etc/my.cnf配置文件中设置,再重启)
mysql>INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
mysql>SHOW GLOBAL VARIABLES LIKE '%semi%';
#设置配置文件
[root@master ~]#vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
server-id=8
log-bin
rpl_semi_sync_master_enabled=ON     #修改此行,需要先安装semisync_master.so插件后,再重启,否则无法启动
rpl_semi_sync_master_timeout=3000   #设置3s内无法同步,也将返回成功信息给客户端
#重新启动mysql
systemctl restart mysql
#从节点的rsyc是否加入
mysql> SHOW GLOBAL STATUS LIKE '%semi%'; 
+--------------------------------------------+-------+
| Variable_name                             | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 2     |

从节点:
#从节点安装半同步插件
mysql>INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
mysql>SHOW GLOBAL VARIABLES LIKE '%semi%';
#设置配置文件
[root@slave1 ~]#vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
server-id=18
rpl_semi_sync_slave_enabled=ON #修改此行,需要先安装semisync_slave.so插件后,再重启,否则无法启动
#重新启动mysql
systemctl restart mysql
mysql>SHOW GLOBAL VARIABLES LIKE '%semi%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled     | ON   |
| rpl_semi_sync_slave_trace_level | 32   |
+---------------------------------+-------+ 
2 rows in set (0.00 sec)
#删除从节点change master to 
mysql> reset slave all

 mycat读写分离

安装Java --> 安装mycat --> 修改配置文件server.xml;schema.xml --> 重启mycat --> 后端主服务器创建用户并对mycat授权 --> #主从服务器上启用通用日志确认实现读写分离

mycat服务器10.0.0.38配置
#下载mycat包
http://dl.mycat.org.cn/1.6.7.6/20220524101549/Mycat-server-1.6.7.6-release-20220524173810-linux.tar.gz
#安装Java
[root@Rocky8 ~]# yum -y install java
[root@Rocky8 ~]# mkdir /app/
#解压mycat到当前目录
[root@Rocky8 ~]# tar xf Mycat-server-1.6.7.6-release-20220524173810-linux.tar.gz
#移动mycat到/app/下
[root@Rocky8 ~]# mv mycat/ /app/
#设置PATH路径
[root@Rocky8 ~]# echo 'PATH=/app/mycat/bin:$PATH' > /etc/profile.d/mycat.sh
[root@Rocky8 ~]# source /etc/profile.d/mycat.sh
#开启mycat
[root@Rocky8 ~]# mycat start
Starting Mycat-server...
#8066为mycat的端口号
[root@Rocky8 ~]# ss -nlt
State   Recv-Q  Send-Q   Local Address:Port    Peer Address:Port Process  
LISTEN  0       128            0.0.0.0:22           0.0.0.0:*             
LISTEN  0       1            127.0.0.1:32000        0.0.0.0:*             
LISTEN  0       50                   *:38545              *:*             
LISTEN  0       128               [::]:22              [::]:*             
LISTEN  0       50                   *:46781              *:*             
LISTEN  0       50                   *:1984               *:*             
LISTEN  0       128                  *:8066               *:*             
LISTEN  0       128                  *:9066               *:*     
#查看mycat启动日志
[root@Rocky8 ~]# tail /app/mycat/logs/wrapper.log

#修改配置文件
--此文件修改完后端口从8066改为3306
[root@Rocky8 ~]# vim /app/mycat/conf/server.xml
...省略...
#修改下面行的8066改为3306复制到到独立非注释行
<property name="serverPort">3306</property>
<property name="handlelDistributedTransactions">0</property> #将上面行放在此行前面
<user name="root">                             #连接Mycat的用户名
   <property name="password">123456</property>  #连接Mycat的密码
   <property name="schemas">TESTDB</property>   #数据库名要和schema.xml相对应
</user>
</mycat:server>

--此文件配置账户和密码,设置数据库名
[root@Rocky8 ~]# vim /app/mycat/conf/schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
    <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
    </schema>
    <dataNode name="dn1" dataHost="localhost1" database="hellodb" />
    <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
              writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
        <heartbeat>select user()</heartbeat>
        <writeHost host="host1" url="10.0.0.8:3306" user="liu" password="123456">
         <readHost host="host2" url="10.0.0.18:3306" user="liu" password="123456" />
         <readHost host="host3" url="10.0.0.28:3306" user="liu" password="123456" />
        </writeHost>
    </dataHost>
</mycat:schema>

#重新启动mycat
[root@Rocky8 ~]# mycat restart
Starting Mycat-server...

后端主数据库设置10.0.0.8
#后端主服务器创建用户并对mycat授权
[root@Rokey8 ~]#mysql
mysql>create user 'liu'@'10.0.0.%' identified by '123456';
mysql>grant all on hellodb.* to 'liu'@'10.0.0.%';

#在客户端10.0.0.48上连接测试mycat服务器
[root@Rocky8 ~]# mysql -uroot -p123456 -h10.0.0.38
mysql> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB   |
+----------+
1 row in set (0.00 sec)
mysql> use TESTDB
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
mysql> select @@hostname;
+---------------------+
| @@hostname          |
+---------------------+
| Rocky8.5.magedu.org |
+---------------------+
1 row in set (0.02 sec)

#在主从服务器上启用通用日志确认实现读写分离
[root@Rokey8 ~]#cat /etc/my.cnf
[mysqld]
server-id=8
log-bin=/data/mysql/logbin/mysql-bin
rpl_semi_sync_master_enabled=ON
rpl_semi_sync_master_timeout=3000
general_log #开启通用日志
[root@Rokey8 ~]#tail -f /var/lib/mysql/Rokey8.log
2022-08-25T13:37:59.204951Z	   11 Query	select user()
2022-08-25T13:38:09.204926Z	   13 Query	select user()
2022-08-25T13:38:19.206545Z	   12 Query	select user()
2022-08-25T13:38:29.204214Z	    9 Query	select user()
mycat每十秒进行心跳检测,通过select user()判断后端主从是否出现故障

 MHA高可用

MHA安装mhamysql-manager和node --> 主从节点安装node包 --> 所有节点实现key验证 --> MHA设置app1.cnf文件 --> MHA设置邮件信息、报警脚本、Perl文件 --> 在主数据库上设置VIP且设置账户并授权 --> 检查MHA环境并启动 --> 停掉主数据库进行测试

环境:四台主机
10.0.0.7 CentOS7 MHA管理端
10.0.0.8 CentOS8 MySQL8.0 Master 
10.0.0.18 CentOS8 MySQL8.0 Slave1
10.0.0.28 CentOS8 MySQL8.0 Slave2

#MHA系统必须是centos7(10.0.0.7)
MHA节点安装(必须安装epel源)
[root@mha ~]# yum -y install mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
[root@mha ~]# yum -y install mha4mysql-node-0.58-0.el7.centos.noarch.rpm

#其他主从节点安装
[root@mha ~]# yum -y install mha4mysql-node-0.58-0.el7.centos.noarch.rpm

所有节点实现ssh key验证
[root@mha ~]# ssh-keygen
[root@mha ~]# ssh-copy-id 127.0.0.1
[root@mha ~]# rsync -av .ssh 10.0.0.8:/root/
[root@mha ~]# rsync -av .ssh 10.0.0.18:/root/
[root@mha ~]# rsync -av .ssh 10.0.0.28:/root/

MHA节点设置
[root@mha ~]# cat /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
master_binlog_dir=/data/mysql/logbin/

[server1]
hostname=10.0.0.8
candidate_master=1
[server2]
hostname=10.0.0.18
candidate_master=1
[server3]
hostname=10.0.0.28

#设置邮件信息
[root@mha ~]# yum -y install mailx
[root@mha ~]# cat /etc/mail.rc
......
set from=690892336@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=690892336@qq.com
set smtp-auth-password=pmzoljslmdribbcj
#设置报警脚本
[root@mha ~]# cat /usr/local/bin/sendmail.sh
#!/bin/bash
echo "MHA is failover!" | mail -s "MHA Warning" 690892336@qq.com
[root@mha ~]# chmod +x /usr/local/bin/sendmail.sh
#设置perl文件
[root@mha ~]# cat /usr/local/bin/master_ip_failover 
#!/usr/bin/env perl

#  Copyright (C) 2011 DeNA Co.,Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#  Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

## Note: This is a sample script and is not complete. Modify the script based on your environment.

use strict;
use warnings FATAL => 'all';

use Getopt::Long;
use MHA::DBHelper;

my (
  $command,        $ssh_user,         $orig_master_host,
  $orig_master_ip, $orig_master_port, $new_master_host,
  $new_master_ip,  $new_master_port,  $new_master_user,
  $new_master_password
);
my $vip = '10.0.0.100/24';
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";

GetOptions(
  'command=s'             => \$command,
  'ssh_user=s'            => \$ssh_user,
  'orig_master_host=s'    => \$orig_master_host,
  'orig_master_ip=s'      => \$orig_master_ip,
  'orig_master_port=i'    => \$orig_master_port,
  'new_master_host=s'     => \$new_master_host,
  'new_master_ip=s'       => \$new_master_ip,
  'new_master_port=i'     => \$new_master_port,
  'new_master_user=s'     => \$new_master_user,
  'new_master_password=s' => \$new_master_password,
);

exit &main();

sub main {
  if ( $command eq "stop" || $command eq "stopssh" ) {

    # $orig_master_host, $orig_master_ip, $orig_master_port are passed.
    # If you manage master ip address at global catalog database,
    # invalidate orig_master_ip here.
    my $exit_code = 1;
    eval {

      # updating global catalog, etc
      $exit_code = 0;
    };
    if ($@) {
      warn "Got Error: $@\n";
      exit $exit_code;
    }
    exit $exit_code;
  }
    elsif ( $command eq "start" ) {

        # all arguments are passed.
        # If you manage master ip address at global catalog database,
        # activate new_master_ip here.
        # You can also grant write access (create user, set read_only=0, etc) here.
        my $exit_code = 10;
        eval {
            print "Enabling the VIP - $vip on the new master - $new_master_host \n";
            &start_vip();
            &stop_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn $@;
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "status" ) {
        print "Checking the Status of the script.. OK \n";
        `ssh $ssh_user\@$orig_master_host \" $ssh_start_vip \"`;
        exit 0;
    }
    else {
        &usage();
        exit 1;
    }
}


sub start_vip() {
    `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master 
sub stop_vip() {
   `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}


sub usage {
  print
"Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}

[root@mha ~]# chmod +x /usr/local/bin/master_ip_failover

10.0.0.8上设置vip
[root@master ~]#ifconfig eth0:1 10.0.0.100/24
创建账户和授权
mysql> create user repluser@'10.0.0.%' identified by 'magedu';
mysql> grant replication slave on *.* to repluser@'10.0.0.%';
mysql> create user mhauser@'10.0.0.%' identified by 'magedu';
mysql> grant all on *.* to mhauser@'10.0.0.%';

10.0.0.7MHA
检查MHA的环境
[root@mha ~]# masterha_check_ssh --conf=/etc/mastermha/app1.cnf
[root@mha ~]# masterha_check_repl --conf=/etc/mastermha/app1.cnf
#查看状态
[root@mha ~]# masterha_check_status --conf=/etc/mastermha/app1.cnf

启动MHA
#开启MHA,默认是前台运行,生产环境一般为后台执行
nohup masterha_manager --conf=/etc/mastermha/app1.cnf --remove_dead_master_conf --ignore_last_failover &> /dev/null
#测试环境:
masterha_manager --conf=/etc/mastermha/app1.cnf --remove_dead_master_conf --ignore_last_failover

[root@mha ~]# masterha_manager --conf=/etc/mastermha/app1.cnf --remove_dead_master_conf --ignore_last_failover
Fri Aug 26 22:31:52 2022 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Aug 26 22:31:52 2022 - [info] Reading application default configuration from /etc/mastermha/app1.cnf..
Fri Aug 26 22:31:52 2022 - [info] Reading server configuration from /etc/mastermha/app1.cnf..
[root@mha ~]# tail -f /data/mastermha/app1/manager.log
 +--10.0.0.18(10.0.0.18:3306)


[root@master ~]#tail -f /var/lib/mysql/master.log 
2022-08-26T14:35:28.784228Z	   35 Query	SELECT 1 As Value
2022-08-26T14:35:29.785349Z	   35 Query	SELECT 1 As Value
2022-08-26T14:35:36.615647Z	   43 Query	select user()

#测试停掉主数据库,发现VIP10.0.0.100自动飘到10.0.0.18上,18从节点自动升为主节点,28节点为18的从节点;MHA只能执行一次,不能重复执行;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值