公司网站搭建的架构

目录

简介

公司现阶段需要搭建一个技术论坛对外网提供服务,网站设计要求达到高可用,高负载,并且添加监控。

拓扑图

在这里插入图片描述

需求

1、使用LVS+ keeplive实现负载均衡
2、使用MHA搭建mysql集群
3、使用ceph集群实现web网站内容一致
4、搭建discuz论坛
5、搭建DNS解析网站域名
6、使用zabbix监控各个服务器硬件指标及服务端口
7、备份mysql数据库到ceph集群
8、使用ansble批量部署nginx、apache,nginx和apache必须为源码包安装。

首先先搭建好MHA集群

主机名 IP
mysql-01 192.168.1.2
mysql-02 192.168.1.3
mysql-03 192.168.1.4
mha 192.168.1.5

跟新主机时间

所有主机都需要根新

[root@mysql-01 ~]# ntpdate ntp1.aliyun.com
 6 Apr 15:34:50 ntpdate[1467]: step time server 120.25.115.20 offset -28798.923817 sec
[root@mysql-01 ~]# 

做个计划任务

[root@mysql-01 ~]# crontab -l
30 * * * * ntpdate ntp1.aliyun.com

修改主机名

[root@mysql ~]# hostnamectl set-hostname mysql-01
[root@mysql ~]# bash   #这里需要bash刷新一下环境,才会显示新修改的主机名
[root@mysql-01 ~]# 
[root@mysql ~]# hostnamectl set-hostname mysql-02
[root@mysql ~]# bash   #这里需要bash刷新一下环境,才会显示新修改的主机名
[root@mysql-02 ~]# 
[root@mysql ~]# hostnamectl set-hostname mysql-03
[root@mysql ~]# bash   #这里需要bash刷新一下环境,才会显示新修改的主机名
[root@mysql-03 ~]# 
[root@mysql ~]# hostnamectl set-hostname mha
[root@mysql ~]# bash   #这里需要bash刷新一下环境,才会显示新修改的主机名
[root@mha ~]# 

配置所有主机之间SSH无密码验证

所有主机之间要相互SSH无秘

[root@mysql-01 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #这里什么都不输入,直接回车
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): #这里什么都不输入,直接回车
Enter same passphrase again: #这里什么都不输入,直接回车
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:u1htfi6dAAP6Fx4plXeAfvGJfmANLO9Gcde8GflzpPk root@mysql-01
The key's randomart image is:
+---[RSA 2048]----+
|         +..   o.|
|      . = * o .o+|
|     . + = X o +=|
|    . . B B + o+o|
|     . oS@ .   .o|
|      . oo= .   E|
|       .o.o+ .   |
|       o +. +    |
|      . . .+.    |
+----[SHA256]-----+
[root@mysql-01 ~]# 

此时密钥对已经创建完成。

将私钥发送到所有主机(包括本机)

[root@mysql-01 ~]# for i in 2 3 4 5;do ssh-copy-id 192.168.1.$i;done

测试一下

[root@mysql-01 ~]# for i in 2 3 4 5;do ssh root@192.168.1.$i hostname;done
mysql-01
mysql-02
mysql-03
mha

显示正常

将下载好的软件包上传到主机

链接:https://pan.baidu.com/s/1hRiV4jF7w9WaG5brhdRRkA
提取码:agp6
–来自百度网盘超级会员V2的分享

[root@mysql-01 ~]# ls
auto_install_mysql_cpu4.sh  mha4mysql-manager-0.57-0.el7.noarch.rpm  mhapath.tar.gz  mysql-community-5.7.26-1.el7.src.rpm
boost_1_59_0                mha4mysql-node-0.57-0.el7.noarch.rpm     mysql-5.7.26    rpmbuild
[root@mysql-01 ~]# 

此时可以看见我们已经上传好了mha4mysql-manager-0.57-0.el7.noarch.rpm,mha4mysql-node-0.57-0.el7.noarch.rpm,mhapath.tar.gz这三个包

配置本地yum源

[root@mysql-01 ~]# vim /etc/yum.repos.d/mhapath.repo 

[mhapath]
name=mhapath
baseurl=file:///root/mhapath
enabled=1
gpgcheck=0

解压软件包

[root@mysql-01 ~]# tar -zxvf mhapath.tar.gz

将解压好的软件包和yum文件发送到其他主机上

[root@mysql-01 ~]# for i in 3 4 5;do scp -r /root/mhapath root@192.168.1.$i:~;done
[root@mysql-01 ~]# for i in 3 4 5;do scp -r /etc/yum.repos.d/ root@192.168.1.$i:/etc/;done
[root@mysql-01 ~]# for i in 3 4 5;do scp mha4mysql-node-0.57-0.el7.noarch.rpm root@192.168.1.$i:~;done
mha4mysql-node-0.57-0.el7.noarch.rpm                                                        100%   35KB  13.8MB/s   00:00    
mha4mysql-node-0.57-0.el7.noarch.rpm                                                        100%   35KB  14.6MB/s   00:00    
mha4mysql-node-0.57-0.el7.noarch.rpm                                                        100%   35KB  18.6MB/s   00:00

在manager主机和各个node节点安装软件依赖包

在所有主机上都要执行这两条命令

[root@mysql-01 ~]# yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager --skip-broken --nogpgcheck
[root@mysql-01 ~]# rpm -ivh mha4mysql-node-0.57-0.el7.noarch.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:mha4mysql-node-0.57-0.el7        ################################# [100%]
[root@mysql-01 ~]# 

安装MHA manager依赖的perl模块包

[root@mha ~]# yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN

安装MHA manager软件包

[root@mysql-01 ~]# scp mha4mysql-manager-0.57-0.el7.noarch.rpm root@192.168.1.5:~
mha4mysql-manager-0.57-0.el7.noarch.rpm 
[root@mha ~]# rpm -ivh mha4mysql-manager-0.57-0.el7.noarch.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:mha4mysql-manager-0.57-0.el7     ################################# [100%]
[root@mha ~]# 

搭建主从复制环境

首先要先在所有mysql主机上安装半同步插件,在关闭mysql-01的数据库并修改配置文件,在重启mysql主机

[root@mysql-01 ~]# mysql -uroot -p
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26 Source distribution

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected (0.01 sec)

mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.00 sec)

mysql> 

[root@mysql-01 ~]# systemctl stop mysql
[root@mysql-01 ~]# vim /etc/my.cnf

datadir=/data/mysql/data
port=3306
socket=/usr/local/mysql/mysql.sock
symbolic-links=0
character-set-server=utf8
log-error=/data/mysql/log/mysqld.log
pid-file=/usr/local/mysql/mysqld.pid
server-id=1  #从这里开始添加
log-bin=/data/mysql/log/mysql-bin
log-bin-index=/data/mysql/log/mysql-bin.index
binlog_format=mixed
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=10000
rpl_semi_sync_slave_enabled=1
relay_log_purge=0
relay-log=/data/mysql/log/relay-bin
relay-log-index=/data/mysql/log/slave-relay-bin.index
log_slave_updates=1

[root@mysql-01 ~]# systemctl restart mysql

登陆到mysql-01主机(创建一个测试库)

创建HA库并创建stu表,并插入数据

mysql> create database HA;
Query OK, 1 row affected (10.01 sec)

mysql> use HA;
Database changed
mysql> create table stu(id int,name varchar(20));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into stu values(1,'lisi');
Query OK, 1 row affected (0.02 sec)

mysql> 

授权

创建用于主从复制的用户,并赋予权限,之后刷新权限使其生效。

mysql> grant replication slave on *.* to hello@'192.168.1.%' identified by '1';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 

授权给manager主机

mysql> grant all privileges on *.* to manager@'192.168.1.%' identified by '1';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 

查看状态

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     1655 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> 

将数据导出并发送到其他两台mysql上

[root@mysql-01 ~]# mysqldump -uroot -p1 -B HA>HA.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@mysql-01 ~]# for i in 3 4;do scp HA.sql root@192.168.1.$i:~;done
HA.sql                                                                                        100% 1940     1.2MB/s   00:00    
HA.sql                                                                                        100% 1940     2.1MB/s   00:00    
[root@mysql-01 ~]# 

导入数据

分别将数据导入到两台mysql数据库里

[root@mysql-02 ~]# mysql -uroot -p1 < HA.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@mysql-02 ~]# mysql -uroot -p1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26 Source distribution

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> show tables;
ERROR 1046 (3D000): No database selected
mysql> use HA;
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;
+--------------+
| Tables_in_HA |
+--------------+
| stu          |
+--------------+
1 row in set (0.00 sec)

mysql> 

添加权限

分别在剩下的两台mysql上添加权限

mysql> grant replication slave on *.* to hello@'192.168.1.%' identified by '1';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all privileges on *.* to manager@'192.168.1.%' identified by '1';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 

修改配置文件(mysql-02和mysql-03是同样的步骤)

分别修改两台mysql的配置文件,首先需要停止mysql在修改配置文件,修改完成之后重启即可。

[root@mysql-02 ~]# systemctl stop mysql
[root@mysql-02 ~]# vim /etc/my.cnf

[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql/data
port=3306
socket=/usr/local/mysql/mysql.sock
symbolic-links=0
character-set-server=utf8
log-error=/data/mysql/log/mysqld.log
pid-file=/usr/local/mysql/mysqld.pid
server-id=2   #需要注意这里,三台mysql的id不能一致
log-bin=/data/mysql/log/mysql-bin
log-bin-index=/data/mysql/log/mysql-bin.index
binlog_format=mixed
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=10000
rpl_semi_sync_slave_enabled=1
relay_log_purge=0
relay-log=/data/mysql/log/relay-bin
relay-log-index=/data/mysql/log/slave-relay-bin.index
log_slave_updates=1

[root@mysql-02 ~]# systemctl restart mysql

如果这里报错,则表示上面的半同步插件没有在这台mysql上安装,需要吧配置文件里面新添加的先注释掉,然后启动mysql进到mysql中安装半同步插件,然后在把配置文件中的注释删掉,在重启mysql就可以了。

建立主从关系

首先进入到mysql中,然后关闭slave复制功能,指定主库的ip地址,指定主库用于复制的用户,指定主库用于复制用户的密码,指定主库的binlog日志文件,指定主库binlog文件的起始位置。
在开启slave复制

[root@mysql-02 ~]# mysql -uroot -p1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26-log Source distribution

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host='192.168.1.2',master_user='hello',master_password='1',master_log_file='mysql-bin.000001',master_log_pos=1655;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

查看主从是否设置成功

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.2
                  Master_User: hello
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1655
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes   #此时我们看见这里为yes表示,IO没有问题
            Slave_SQL_Running: Yes   #此时我们看见这里为yes表示,SQL没有问题
              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: 1655
              Relay_Log_Space: 521
              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
                  Master_UUID: f9cf2bb0-9f99-11ec-9e14-000c294a561e
             Master_Info_File: /data/mysql/data/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: 
1 row in set (0.00 sec)

mysql> 

如果IO和SQL为no时,我们需要重新设置用于复制的权限,重新指定主数据库。

两台slave服务器设置read_only

从库对外提供读服务,只所以没有写进配置文件,是因为slave随时会提升为master

mysql> set global read_only=1;
Query OK, 0 rows affected (0.00 sec)

到这里整个集群环境已经搭建完毕,剩下的就是配置MHA软件了。

配置MHA

创建MHA的相关工作目录,并创建相关的配置文件
[root@mha ~]# mkdir -p /var/log/masterha/app1
[root@mha ~]# mkdir -p /etc/masterha
编辑
[root@mha ~]# vim /etc/masterha/app1.cnf

[server default]
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/data/mysql/log
#master_ip_failover_script=/usr/bin/master_ip_failover
#master_ip_online_change_script=/usr/bin/master_ip_online_change

user=manager
password=1
ping_interval=1
remote_workdir=/tmp
repl_user=hello   #需要注意的是,这里的用户是用于复制的用户,也就是slave用户
repl_password=1   #这里是slave用户的密码
report_script=/usr/local/send_report
shutdown_script=""
ssh_user=root

[server1]
hostname=192.168.1.2
port=3306

[server2]
hostname=192.168.1.3
port=3306

[server3]
hostname=192.168.1.4
port=3306
检查SSH配置
[root@mha ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf
Wed Apr  6 19:46:50 2022 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Apr  6 19:46:50 2022 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Wed Apr  6 19:46:50 2022 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Wed Apr  6 19:46:50 2022 - [info] Starting SSH connection tests..
Wed Apr  6 19:46:51 2022 - [debug] 
Wed Apr  6 19:46:50 2022 - [debug]  Connecting via SSH from root@192.168.1.2(192.168.1.2:22) to root@192.168.1.3(192.168.1.3:22)..
Wed Apr  6 19:46:50 2022 - [debug]   ok.
Wed Apr  6 19:46:50 2022 - [debug]  Connecting via SSH from root@192.168.1.2(192.168.1.2:22) to root@192.168.1.4(192.168.1.4:22)..
Wed Apr  6 19:46:50 2022 - [debug]   ok.
Wed Apr  6 19:46:51 2022 - [debug] 
Wed Apr  6 19:46:51 2022 - [debug]  Connecting via SSH from root@192.168.1.3(192.168.1.3:22) to root@192.168.1.2(192.168.1.2:22)..
Wed Apr  6 19:46:51 2022 - [debug]   ok.
Wed Apr  6 19:46:51 2022 - [debug]  Connecting via SSH from root@192.168.1.3(192.168.1.3:22) to root@192.168.1.4(192.168.1.4:22)..
Wed Apr  6 19:46:51 2022 - [debug]   ok.
Wed Apr  6 19:46:52 2022 - [debug] 
Wed Apr  6 19:46:51 2022 - [debug]  Connecting via SSH from root@192.168.1.4(192.168.1.4:22) to root@192.168.1.2(192.168.1.2:22)..
Wed Apr  6 19:46:51 2022 - [debug]   ok.
Wed Apr  6 19:46:51 2022 - [debug]  Connecting via SSH from root@192.168.1.4(192.168.1.4:22) to root@192.168.1.3(192.168.1.3:22)..
Wed Apr  6 19:46:51 2022 - [debug]   ok.
Wed Apr  6 19:46:52 2022 - [info] All SSH connection tests passed successfully.

这里看见ALL SSH connection tests passed successfully就表示成功了

检查整个复制环境状态

[root@mha ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
Wed Apr  6 21:18:00 2022 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Apr  6 21:18:00 2022 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Wed Apr  6 21:18:00 2022 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Wed Apr  6 21:18:00 2022 - [info] MHA::MasterMonitor version 0.57.
Wed Apr  6 21:18:01 2022 - [info] GTID failover mode = 0
Wed Apr  6 21:18:01 2022 - [info] Dead Servers:
Wed Apr  6 21:18:01 2022 - [info] Alive Servers:
Wed Apr  6 21:18:01 2022 - [info]   192.168.1.2(192.168.1.2:3306)
Wed Apr  6 21:18:01 2022 - [info]   192.168.1.3(192.168.1.3:3306)
Wed Apr  6 21:18:01 2022 - [info]   192.168.1.4(192.168.1.4:3306)
Wed Apr  6 21:18:01 2022 - [info] Alive Slaves:
Wed Apr  6 21:18:01 2022 - [info]   192.168.1.3(192.168.1.3:3306)  Version=5.7.26-log (oldest major version between slaves) log-bin:enabled
Wed Apr  6 21:18:01 2022 - [info]     Replicating from 192.168.1.2(192.168.1.2:3306)
Wed Apr  6 21:18:01 2022 - [info]   192.168.1.4(192.168.1.4:3306)  Version=5.7.26-log (oldest major version between slaves) log-bin:enabled
Wed Apr  6 21:18:01 2022 - [info]     Replicating from 192.168.1.2(192.168.1.2:3306)
Wed Apr  6 21:18:01 2022 - [info] Current Alive Master: 192.168.1.2(192.168.1.2:3306)
Wed Apr  6 21:18:01 2022 - [info] Checking slave configurations..
Wed Apr  6 21:18:01 2022 - [info] Checking replication filtering settings..
Wed Apr  6 21:18:01 2022 - [info]  binlog_do_db= , binlog_ignore_db= 
Wed Apr  6 21:18:01 2022 - [info]  Replication filtering check ok.
Wed Apr  6 21:18:01 2022 - [info] GTID (with auto-pos) is not supported
Wed Apr  6 21:18:01 2022 - [info] Starting SSH connection tests..
Wed Apr  6 21:18:03 2022 - [info] All SSH connection tests passed successfully.
Wed Apr  6 21:18:03 2022 - [info] Checking MHA Node version..
Wed Apr  6 21:18:03 2022 - [info]  Version check ok.
Wed Apr  6 21:18:03 2022 - [info] Checking SSH publickey authentication settings on the current master..
Wed Apr  6 21:18:03 2022 - [info] HealthCheck: SSH to 192.168.1.2 is reachable.
Wed Apr  6 21:18:04 2022 - [info] Master MHA Node version is 0.57.
Wed Apr  6 21:18:04 2022 - [info] Checking recovery script configurations on 192.168.1.2(192.168.1.2:3306)..
Wed Apr  6 21:18:04 2022 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/data/mysql/log --output_file=/tmp/save_binary_logs_test --manager_version=0.57 --start_file=mysql-bin.000001 
Wed Apr  6 21:18:04 2022 - [info]   Connecting to root@192.168.1.2(192.168.1.2:22).. 
  Creating /tmp if not exists..    ok.
  Checking output directory is accessible or not..
   ok.
  Binlog found at /data/mysql/log, up to mysql-bin.000001
Wed Apr  6 21:18:04 2022 - [info] Binlog setting check done.
Wed Apr  6 21:18:04 2022 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Wed Apr  6 21:18:04 2022 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='manager' --slave_host=192.168.1.3 --slave_ip=192.168.1.3 --slave_port=3306 --workdir=/tmp --target_version=5.7.26-log --manager_version=0.57 --relay_log_info=/data/mysql/data/relay-log.info  --relay_dir=/data/mysql/data/  --slave_pass=xxx
Wed Apr  6 21:18:04 2022 - [info]   Connecting to root@192.168.1.3(192.168.1.3:22).. 
  Checking slave recovery environment settings..
    Opening /data/mysql/data/relay-log.info ... ok.
    Relay log found at /data/mysql/log, up to relay-bin.000002
    Temporary relay log file is /data/mysql/log/relay-bin.000002
    Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Wed Apr  6 21:18:04 2022 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='manager' --slave_host=192.168.1.4 --slave_ip=192.168.1.4 --slave_port=3306 --workdir=/tmp --target_version=5.7.26-log --manager_version=0.57 --relay_log_info=/data/mysql/data/relay-log.info  --relay_dir=/data/mysql/data/  --slave_pass=xxx
Wed Apr  6 21:18:04 2022 - [info]   Connecting to root@192.168.1.4(192.168.1.4:22).. 
  Checking slave recovery environment settings..
    Opening /data/mysql/data/relay-log.info ... ok.
    Relay log found at /data/mysql/log, up to relay-bin.000002
    Temporary relay log file is /data/mysql/log/relay-bin.000002
    Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Wed Apr  6 21:18:04 2022 - [info] Slaves settings check done.
Wed Apr  6 21:18:04 2022 - [info] 
192.168.1.2(192.168.1.2:3306) (current master)
 +--192.168.1.3(192.168.1.3:3306)
 +--192.168.1.4(192.168.1.4:3306)

Wed Apr  6 21:18:04 2022 - [info] Checking replication health on 192.168.1.3..
Wed Apr  6 21:18:04 2022 - [info]  ok.
Wed Apr  6 21:18:04 2022 - [info] Checking replication health on 192.168.1.4..
Wed Apr  6 21:18:04 2022 - [info]  ok.
Wed Apr  6 21:18:04 2022 - [warning] master_ip_failover_script is not defined.
Wed Apr  6 21:18:04 2022 - [warning] shutdown_script is not defined.
Wed Apr  6 21:18:04 2022 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

此时看见ok表示成功了。(如果显示为NOT ok表示失败了。可以尝试重新设置权限,并重新做主从)

检查MHA manager状态

开启MHA监控

[root@mha ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf \
>   --remove_dead_master_conf  --ignore_last_failover < /dev/null > \
>   /var/log/masterha/app1/manager.log 2>&1 &
[1] 5180
[root@mha ~]# masterha_check_status --conf=/etc/masterha/app1.cnf 
app1 (pid:5180) is running(0:PING_OK), master:192.168.1.2
[root@mha ~]# 

此时显示正常,并显示主库ip
注意:如果正常,会显示"PING_OK",否则会显示"NOT_RUNNING",这代表MHA监控没有开启。

查看启动日志

[root@mha ~]# tail -20 /var/log/masterha/app1/manager.log 
  Checking slave recovery environment settings..
    Opening /data/mysql/data/relay-log.info ... ok.
    Relay log found at /data/mysql/log, up to relay-bin.000002
    Temporary relay log file is /data/mysql/log/relay-bin.000002
    Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Wed Apr  6 21:22:57 2022 - [info] Slaves settings check done.
Wed Apr  6 21:22:57 2022 - [info] 
192.168.1.2(192.168.1.2:3306) (current master)
 +--192.168.1.3(192.168.1.3:3306)
 +--192.168.1.4(192.168.1.4:3306)

Wed Apr  6 21:22:57 2022 - [warning] master_ip_failover_script is not defined.
Wed Apr  6 21:22:57 2022 - [warning] shutdown_script is not defined.
Wed Apr  6 21:22:57 2022 - [info] Set master ping interval 1 seconds.
Wed Apr  6 21:22:57 2022 - [warning] secondary_check_script is not defined. It is highly recommended setting it to check master reachability from two or more routes.
Wed Apr  6 21:22:57 2022 - [info] Starting ping health check on 192.168.1.2(192.168.1.2:3306)..
Wed Apr  6 21:22:57 2022 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..
[root@mha ~]# 

其中"Ping(SELECT) succeeded, waiting until MySQL doesn’t respond…"说明整个系统已经开始监控了。
此时左右的MHA搭建完毕。现在需要创建vip

关闭监控

[root@mha ~]# masterha_stop --conf=/etc/masterha/app1.cnf 
Stopped app1 successfully.
[1]+  Exit 1                  nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1
[root@mha ~]# 

在主库上创建vip

创建vip并查看一下

[root@mysql-01 ~]# ifconfig ens33:1 192.168.1.200 netmask 255.255.255.0 up
[root@mysql-01 ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.2  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::8513:8f3a:aa86:c310  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4a:56:1e  txqueuelen 1000  (Ethernet)
        RX packets 17075  bytes 7135592 (6.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 25434  bytes 46457937 (44.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.200  netmask 255.255.255.0  broadcast 192.168.1.255
        ether 00:0c:29:4a:56:1e  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 193  bytes 38912 (38.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 193  bytes 38912 (38.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@mysql-01 ~]# 

在在主配置文件中开启脚本

[root@mha ~]# vim /etc/masterha/app1.cnf 

[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.log
master_binlog_dir=/data/mysql/log
master_ip_failover_script=/usr/bin/master_ip_failover  #将这一行的注释取消掉
#master_ip_online_change_script=/usr/bin/master_ip_online_change

user=manager
password=1
ping_interval=1
remote_workdir=/tmp
repl_user=hello
repl_password=1
report_script=/usr/local/send_report
shutdown_script=""
ssh_user=root

[server1]
hostname=192.168.1.2
port=3306

[server2]
hostname=192.168.1.3
port=3306

[server3]
hostname=192.168.1.4
port=3306
                           

编写脚本/usr/bin/master_ip_failover,要会perl脚本语言

[root@mha ~]# vim /usr/bin/master_ip_failover

#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';

use Getopt::Long;

my (
    $command,          $ssh_user,        $orig_master_host, $orig_master_ip,
    $orig_master_port, $new_master_host, $new_master_ip,    $new_master_port
);

my $vip = '192.168.1.200/24';    #这里的ip必须是刚才设置的vip
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$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,
);

exit &main();

sub main {
   
   

    print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";

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

        my $exit_code = 1;
        eval {
   
   
            print "Disabling the VIP on old master: $orig_master_host \n";
            &stop_vip();
            $exit_code = 0;
        };
        if ($@) {
   
   
            warn "Got Error: $@\n";
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "start" ) {
   
   

        my $exit_code = 10;
        eval {
   
   
            print "Enabling the VIP - $vip on the new master - $new_master_host \n";
            &start_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\@cluster1 \" $ssh_start_vip \"`;
    else {
   
   
        &usage();
        exit 1;
    }
}

# A simple system call that enable the VIP on the new master
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/bin/master_ip_failover

检查SSH配置

[root@mha ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf
Wed Apr  6 22:00:36 2022 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Apr  6 22:00:36 2022 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Wed Apr  6 22:00:36 2022 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Wed Apr  6 22:00:36 2022 - [info] Starting SSH connection tests..
Wed Apr  6 22:00:36 2022 - [debug] 
Wed Apr  6 22:00:36 2022 - [debug]  Connecting via SSH from root@192.168.1.2(192.168.1.2:22) to root@192.168.1.3(192.168.1.3:22)..
Wed Apr  6 22:00:36 2022 - [debug]   ok.
Wed Apr  6 22:00:36 2022 - [debug]  Connecting via SSH from root@192.168.1.2(192.168.1.2:22) to root@192.168.1.4(192.168.1.4:22)..
Wed Apr  6 22:00:36 2022 - [debug]   ok.
Wed Apr  6 22:00:37 2022 - [debug] 
Wed Apr  6 22:00:36 2022 - [debug]  Connecting via SSH from root@192.168.1.3(192.168.1.3:22) to root@192.168.1.2(192.168.1.2:22)..
Wed Apr  6 22:00:37 2022 - [debug]   ok.
Wed Apr  6 22:00:37 2022 - [debug]  Connecting via SSH from root@192.168.1.3(192.168.1.3:22) to root@192.168.1.4(192.168.1.4:22)..
Wed Apr  6 22:00:37 2022 - [debug]   ok.
Wed Apr  6 22:00:37 2022 - [debug] 
Wed Apr  6 22:00:37 2022 - [debug]  Connecting via SSH from root@192.168.1.4(192.168.1.4:22) to root@192.168.1.2(192.168.1.2:22)..
Wed Apr  6 22:00:37 2022 - [debug]   ok.
Wed Apr  6 22:00:37 2022 - [debug]  Connecting via SSH from root@192.168.1.4(192.168.1.4:22) to root@192.168.1.3(192.168.1.3:22)..
Wed Apr  6 22:00:37 2022 - [debug]   ok.
Wed Apr  6 22:00:37 2022 - [info] All SSH connection tests passed successfully.
[root@mha ~]# 

检查整个复制环节

[root@mha ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf 
Wed Apr  6 22:03:46 2022 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Apr  6 22:03:46 2022 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Wed Apr  6 22:03:46 2022 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Wed Apr  6 22:03:46 2022 - [info] MHA::MasterMonitor version 0.57.
Wed Apr  6 22:03:47 2022 - [info] GTID failover mode = 0
Wed Apr  6 22:03:47 2022 - [info] Dead Servers:
Wed Apr  6 22:03:47 2022 - [info] Alive Servers:
Wed Apr  6 22:03:47 2022 - [info]   192.168.1.2(192.168.1.2:3306)
Wed Apr  6 22:03:47 2022 - [info]   192.168.1.3(192.168.1.3:3306)
Wed Apr  6 22:03:47 2022 - [info]   192.168.1.4(192.168.1.4:3306)
Wed Apr  6 22:03:47 2022 - [info] Alive Slaves:
Wed Apr  6 22:03:47 2022 - [info]   192.168.1.3(192.168.1.3:3306)  Version=5.7.26-log (oldest major version between slaves) log-bin:enabled
Wed Apr  6</
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值