MHA实现MySQL的高可性
实验环境: centos7.6
主机名 | IP地址 | MYSQL |
---|---|---|
Manager | 192.168.37.7 | MHA管理机 |
Master | 192.168.37.17 | 主服务器 |
Slave1 | 192.168.37.27 | 从服务器1 |
Slave2 | 192.168.37.37 | 从服务器2 |
此实验要保证所有机器时间同步,并且基于KEY验证
一
以Manager机器为时间服务器,开启chrony服务。并设置其他机器可以同步本机时间
[root@mananger ~]#vim /etc/chrony.conf
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift
# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync
# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *
# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2
# Allow NTP client access from local network.
allow 192.168.0.0/16 #允许同步的网段
# Serve time even if not synchronized to a time source.
local stratum 11 #开启时间服务
[root@mananger ~]#systemctl restart chronyd
Masetr,slave1,slave2机器设置时间服务器为192.168.37.7,启动chrony systemctl start chronyd
二
设置基于KEY验证,在Mananger主机
[root@mananger ~]#ssh-keygen
[root@mananger ~]#ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.37.7
[root@mananger ~]#scp -rp /root/.ssh 192.168.37.17:/root/
[root@mananger ~]#scp -rp /root/.ssh 192.168.37.27:/root/
[root@mananger ~]#scp -rp /root/.ssh 192.168.37.37:/root/
三
在管理节(mananger)点上安装MHA包
[root@mananger ~]#yum install -y mha4mysql-manager-0.56-0.el6.noarch.rpm
[root@mananger ~]#yum install -y mha4mysql-node-0.56-0.el6.noarch.rpm
在被管理节点(master,slave1,slave2)安装:
[root@master ~]#rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
[root@slave2 ~]#rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
[root@slave2 ~]#rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
四
manager服务器
[root@mananger ~]#mkdir /etc/mha
[root@mananger ~]#vim /etc/mha/app1.conf
[server default]
user=mhauser
password=centos
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=centos
ping_interval=1
[server1]
hostname=192.168.37.17
candidate_master=1
[server2]
hostname=192.168.37.27
candidate_master=1
[server3]
hostname=192.168.37.37
[root@mananger ~]#masterha_check_ssh --conf=/etc/mha/app1.conf #检查ssh,全部显示ok表示通过
[root@mananger ~]#masterha_check_repl --conf=/etc/mha/app1.conf #检查repl复制,正确设置最后显示会MySQL Replication Health is OK!
[root@mananger ~]#masterha_manager --conf=/etc/mha/app1.conf #检查都通过后开启MHA,默认前台执行
需要在主从设置成功后开启MHA
五
设置MYSQL主从服务器
主服务器master
[root@master ~]#vim /etc/my.cnf
[mysqld]
server_id=17 #添加ID
skip_name_resolve #
log-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
[root@master ~]#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)]> grant replication slave on *.* to repluser@'192.168.37.%' identified by 'centos';#添加用户,设置权限。用户repluse可以通过192.168.37.0网段同步,密码为centos
Query OK, 0 rows affected (0.03 sec)
MariaDB [(none)]> grant all on *.* to mhauser@'192.168.37.%' identified by 'centos';#添加用户,设置权限。用户mhauser可以通过192.168.37.0网段管理,密码为centos
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> quit
[root@master ~]#systemctl start mariadb
从服务器slave1,slave2
[root@slave1 ~]#vim /etc/my.cnf
[mysqld]
server_id=27 #设置ID ,SLAVE3设置为37
read-only #从服务器
log-bin #
relay_log_purge=0 #
skip_name_resolve #
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
[root@slave1 ~]#mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 29
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.37.17',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='centos',
-> MASTER_PORT=3306 ,
-> MASTER_LOG_FILE='mariadb-bin.000001',
-> MASTER_LOG_POS=245;
MariaDB [(none)]> START SLAVE
测试,关闭master后,自动提升slave1为新的主服务器