mysql+MHA高可用+atlas读写分离(成产环境运用场景)

准备四台服务器

第一台:c7m01 10.0.0.12  mysql-master

第二台:c7s02 10.0.0.13  mysql-slave(备用主)slave01

第三台:c7s03 10.0.0.14  mysql-slave  slave02

第四台:c7a04 10.0.0.15  mha+atlas

vip 10.0.0.100 绑定到mysql-master

四台服务器执行同样的操作,时间同步操作

[root@ localhost ~]# echo "*/5* * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1" >>/var/spool/cron/root

修改四台服务器的主机名

mysql-master主

[root@ localhost ~]# vim /etc/hostname

c7m01

mysql-slave从(备用主)

[root@ localhost ~]# vim /etc/hostname 

c7s02

mysql-slave从

[root@ localhost ~]# vim /etc/hostname 

c7s03

mha+atlas(读写分离)

[root@ localhost ~]# vim /etc/hostname 

c7a04

注:主机名修改完成后需要重启,否则不会生效

在mysql-master  mysql-slave上配置hosts解析(主机名字要和自己的主机名对应)

[root@ c7m01 ~]# vim /etc/hostEOF

c7m01  10.0.0.12
c7s02  10.0.0.13
c7s03  10.0.0.14
           

关闭防火墙和selinux  (四台服务器执行同样的操作)

systemctl stop firewalld

systemctl disable firewalld

setenforce 0

[root@ c7m01 ~]# sed -i ' /^SELINUX/s#enforcing#disabled#g' /etc/selinux/config

配置免密登录(四台服务器都要执行,这里我写了一个shell脚本)

[root@ c7a04 ~]# vim ssh.sh

#!/bin/bash
yum -y install sshpass &> /dev/null
read -p "请输入服务器密码:" passwd
UserName=root
IP="10.0.0."
#创建密钥
ssh-keygen -t dsa -f ~/.ssh/id_dsa -P "" &>/dev/null
#分发公钥
for i in 12 13 14 15   #这里的要改成自己机子的ip
  do
    sshpass -p "$passwd" ssh-copy-id -i ~/.ssh/id_dsa.pub -p 22 -o StrictHostKeyChecking=no $UserName@$IP$i &>/dev/null
done

执行脚本(四台服务器都要执行脚本),然后尝试连接其中一台服务器,如没有免密登录,即为成功

 mysql安装yum repo(三台服务器执行同样的操作c7m01、c7s02、c7s03)

[root@ c7m01 ~]# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

[root@ c7m01 ~]# rpm -ivh mysql-community-release-el7-5.noarch.rpm

[root@ c7m01 ~]# yum -y install mysql-server

启动mysql,c7m01、c7s02、c7s03这三台服务器执行同样的操作

[root@ c7m01 ~]# systemctl restart mysql

修改mysql密码,c7m01、c7s02、c7s03这三台服务器执行同样的操作

[root@ c7m01 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.6.47 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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> update mysql.user set password=password('123456') where user='root' and host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

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

mysql> 

在c7m01上执行操作,修改mysql的配置文件

vim /etc/my.cnf

[mysqld]
server-id=1
log-bin=mysql-bin
#禁止mysql自动删除relaylog工能
relay_log_purge = 0
#mysql5.6已上的特性,开启gtid,必须主从全开
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates = 1

更改完成后重启mysql
systemctl restart mysql

创建用户同步

[root@ c7m01 ~]# mysql -uroot -p123456
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.6.47-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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> grant replication slave on *.* to 'rep'@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

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

查看MySQL主库的master状态

mysql> show master statusG
*************************** 1. row ***************************
             File: mysql-bin.000002
         Position: 151
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

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

查看GTID状态

mysql> show global variables like '%gtid%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| binlog_gtid_simple_recovery     | OFF   |
| enforce_gtid_consistency        | ON    |
| gtid_executed                   |       |
| gtid_mode                       | ON    |
| gtid_owned                      |       |
| gtid_purged                     |       |
| simplified_binlog_gtid_recovery | OFF   |
+---------------------------------+-------+
7 rows in set (0.01 sec)

c7s02上执行操作

修改mysql配置文件

vim /etc/my.cnf

[mysqld]
server-id=2
log-bin=mysql-bin
#禁止mysql自动删除relaylog工能
relay_log_purge = 0
#mysql5.6已上的特性,开启gtid,必须主从全开
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates = 1

更改完成后重启mysql
systemctl restart mysql

创建同步用户

[root@ c7s02 ~]# mysql -uroot -p123456
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.6.47-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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> grant replication slave on *.* to 'rep'@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

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

关闭复制功能,配置指向master,开启服务器复制状态,检查复制状态,出现俩个yes状态表示成功

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

mysql> change master to
    -> master_host='10.0.0.12',
    -> master_user='rep',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000002',
    -> master_log_pos=151;

Query OK, 0 rows affected, 2 warnings (0.06 sec)

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

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

mysql> show slave statusG
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.12
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 151
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 314
        Relay_Master_Log_File: mysql-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: 151
              Relay_Log_Space: 519
              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: c9c03e71-57ba-11ea-a442-000c29d39f7e
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           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: 2c3c3e92-57bb-11ea-a445-000c2927e658:1-3
                Auto_Position: 0
1 row in set (0.00 sec)

在c7s03执行操作

修改mysql配置文件

vim /etc/my.cnf

[mysqld]
server-id=3
log-bin=mysql-bin
#禁止mysql自动删除relaylog工能
relay_log_purge = 0
#mysql5.6已上的特性,开启gtid,必须主从全开
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates = 1

更改完成后重启mysql
systemctl restart mysql

创建用户同步

[root@ c7s03 ~]# mysql -uroot -p123456
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.6.47-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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> grant replication slave on *.* to 'rep'@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

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

关闭复制功能,配置指向master,开启服务器复制状态,检查复制状态,出现俩个yes状态表示成功

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

mysql> change master to 
    -> master_host='10.0.0.12',
    -> master_user='rep',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000002',
    -> master_log_pos=151;
Query OK, 0 rows affected, 2 warnings (0.36 sec)

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

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

mysql> show slave statusG
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.12
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 151
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 314
        Relay_Master_Log_File: mysql-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: 151
              Relay_Log_Space: 519
              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: c9c03e71-57ba-11ea-a442-000c29d39f7e
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           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: 75841405-57bb-11ea-a447-000c298eeea7:1-3
                Auto_Position: 0
1 row in set (0.00 sec)

在四台服务器装MHA的依赖

yum -y install perl-dbd-mysql

yum -y install per-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForKManager perl-Time-HiRes

授权MHA管理用户(三台mysql服务器执行同样的操作)

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

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

四台服务器安装node结点

[root@ c7m01 ~]# ll
total 52
-rw-------. 1 root root  1277 2019-09-17 22:18 anaconda-ks.cfg
-rw-r--r--  1 root root 36328 2020-02-13 16:32 mha4mysql-node-0.58-0.el7.centos.noarch.rpm
-rw-r--r--  1 root root  6140 2015-11-12 15:58 mysql-community-release-el7-5.noarch.rpm
-rw-r--r--  1 root root   391 2020-02-25 18:24 ssh.sh
[root@ c7m01 ~]# rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm 
error: Failed dependencies:
    perl(DBD::mysql) is needed by mha4mysql-node-0.58-0.el7.centos.noarch
[root@ c7m01 ~]# 

在c7a04上安装MHA的管理结点

[root@ c7a04 ~]# ll
total 4972
-rw-------. 1 root root    1277 2019-09-17 22:18 anaconda-ks.cfg
-rw-r--r--  1 root root 4963681 2020-02-25 14:16 Atlas-2.2.1.el6.x86_64.rpm
-rw-r--r--  1 root root   81024 2020-02-13 16:32 mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
-rw-r--r--  1 root root   36328 2020-02-13 16:32 mha4mysql-node-0.58-0.el7.centos.noarch.rpm
-rw-r--r--  1 root root     391 2020-02-25 18:23 ssh.sh
[root@ c7a04 ~]# rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm 
error: Failed dependencies:
    mha4mysql-node >= 0.54 is needed by mha4mysql-manager-0.58-0.el7.centos.noarch
    perl(Config::Tiny) is needed by mha4mysql-manager-0.58-0.el7.centos.noarch
    perl(DBD::mysql) is needed by mha4mysql-manager-0.58-0.el7.centos.noarch
    perl(DBI) is needed by mha4mysql-manager-0.58-0.el7.centos.noarch
    perl(MHA::BinlogManager) is needed by mha4mysql-manager-0.58-0.el7.centos.noarch
    perl(MHA::NodeConst) is needed by mha4mysql-manager-0.58-0.el7.centos.noarch
    perl(MHA::NodeUtil) is needed by mha4mysql-manager-0.58-0.el7.centos.noarch
    perl(MHA::SlaveUtil) is needed by mha4mysql-manager-0.58-0.el7.centos.noarch
    perl(Parallel::ForkManager) is needed by mha4mysql-manager-0.58-0.el7.centos.noarch
[root@ c7a04 ~]# rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm --nodeps --force
Preparing...                          ################################# [100%]
Updating / installing...
   1:mha4mysql-manager-0.58-0.el7.cent################################# [100%]
[root@ c7a04 ~]# 

这里直接解压可能会出错,所以加一个--nodeps --force(这个东西可能后期会出现错误)

在c7a04上执行

配置MHA

[root@ c7a04 ~]# mkdir -p /etc/mha
[root@ c7a04 ~]# mkdir -p /var/log/mha/app1
[root@ c7a04 ~]# vim /etc/mha/app1.cnf

[server default]
manager_log=/var/log/mha/app1/manager.log
manager_workdir=/var/log/mha/app1
master_binlog_dir=/var/lib/mysql #binlog的目录,如果说miysql的环境不一样,binlog位置不同,每台服务器的binlog的位置写在server标签里面即可
user=mha
password=mha
ping_interval=2
repl_password=123456
repl_user=rep
ssh_user=root

[server1]
hostname=10.0.0.12
port=3306

[server2]
hostname=10.0.0.13
port=3306

[server3]
hostname=10.0.0.14
port=3306
ignore_fail=1  #如果这个节点挂了,mha将不可用,加上这个参数,slave挂了一样可以用
no_master=1  #从不将这台主机转换为master
#candidate_master=1 #如果候选master有延迟的话,relay日志超过100m,failover切换不能成功,加上此参数后会忽略延迟日志大小。
#check_repl_delay=0 #用防止master故障时,切换时slave有延迟,卡在那里切不过来

#注意这里的配置要把注释和空格全部删除

检查ssh

原文地址:https://www.cnblogs.com/Zrecret/p/12363116.html