1. 程式人生 > >利用mha實現mysql的主從自動切換

利用mha實現mysql的主從自動切換

ssh免密連線:

[[email protected] etc]# ssh-keygen
[[email protected] etc]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]  #免密登入對方的ssh  這的ip是對方的ip
[[email protected] etc]# vim /etc/ssh/ssh_config  #關閉yes的詢問 
StrictHostKeyChecking no

**時間同步伺服器:**隨意找一臺伺服器作為時間的伺服器,時間正確與錯誤本次實驗沒有要求,但是要一致

[[email protected]
etc]# vim /etc/ntp.conf #時間伺服器 restrict 127.0.0.1 restrict ::1 restrict 192.168.127.0 mask 255.255.255.0 server 127.127.1.0

時間客戶端:參考

 [[email protected] etc]# vim /etc/ntp.conf    #該設定在下次啟動才生效
     server 192.168.153.7 iburst     
[[email protected] etc]# ntpdate 192.168.127.7  

**主伺服器的配置:**172.18.251.133

[[email protected] etc]# /vim/etc/my.cnf

[mysqld]
server_id=2
datadir=/mysql/data
log_bin=/mysql/logbin/log
innodb_file_per_table
binlog_format=row
skip_name_resolve=1
socket=/var/lib/mysql/mysql.sock

MariaDB [(none)]> show master status;
+------------+----------+--------------+------------------+
| File       | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------+----------+--------------+------------------+
| log.000003 |      245 |              |                  |
+------------+----------+--------------+------------------+
1 row in set (0.00 sec)
#建立主從同步的賬號
MariaDB [(none)]> grant replication slave on *.* to 
[email protected]
'%' identified by 'centos'; Query OK, 0 rows affected (0.00 sec) MariaDB [mysql]> grant all on *.* to [email protected]'%' identified by 'centos'; Query OK, 0 rows affected (0.00 sec)

**配置從伺服器:**172.18.254.228 172.18.254.174

[mysqld]
server_id=24
datadir=/mysql/data
log_bin=/mysql/logbin/log
innodb_file_per_table
read_only=1
relay_log_purge=0         
skip_name_resolve=1 
socket=/var/lib/mysql/mysql.sock

MariaDB [(none)]> CHANGE MASTER TO
    ->   MASTER_HOST='172.18.251.133',
    ->   MASTER_USER='repluser',
    ->   MASTER_PASSWORD='centos',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='log.000003',
    ->   MASTER_LOG_POS=245,
    ->   MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected (0.24 sec)
MariaDB [(none)]> start slave;

**mha伺服器:**server#這個順序決定這從伺服器變為主的次序

[[email protected] etc]# vim /etc/mha/app1.conf

[server default]
user=mhauser      #mha的賬號
password=centos
manager_workdir=/data/mastermha/app1/  
manager_log=/data/mastermha/app1/manager.log  
master_binlog_dir=/mysql/logbin  設定master 儲存binlog的位置,以便MHA可以找到master的日誌        
remote_workdir=/data/mastermha/app1/  
ssh_user=root
repl_user=repluser  
repl_password=centos
ping_interval=1

[server1]  
hostname=192.168.153.17
candidate_master=1  #設定為候選master,如果設定該引數以後,發生主從切換以後將會將此從庫提升為主庫,即使這個主庫不是叢集中事件最新的slave
[server2]  
hostname=192.168.153.27
candidate_master=1  
[server3]  
hostname=192.168.153.37

mha伺服器測試:

[[email protected] etc]# masterha_check_ssh --conf=/etc/mha/app1.conf
.......
Wed Oct 17 09:55:42 2018 - [info] All SSH connection tests passed successfully.
[[email protected] etc]# masterha_check_repl --conf=/etc/mha/app1.conf
......
MySQL Replication Health is OK.

當主伺服器損壞時,從伺服器會自動變為主,read_only也會關閉,當主伺服器再次修好時,會有主變為從其配置如下:

[[email protected] etc]# vim /etc/my.cnf
#增加以下兩行
read_only=ON
relay_log_purge=0

[[email protected] etc]# systemctl restart mariadb.service

Master [(none)]> CHANGE MASTER TO
    ->   MASTER_HOST='172.15.254.228',
    ->   MASTER_USER='repluser',
    ->   MASTER_PASSWORD='centos',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='slave-log.000003',
    ->   MASTER_LOG_POS=245,
    ->   MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected (0.05 sec)

Master [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)