MHA,MYSQL高可用

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爲新的主服務器

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章