MySQL高可用架構之MHA(2)

三、部署MHA

1、環境準備

[root@server01 ~]# cat /etc/redhat-release CentOS release 6.8 (Final)[root@server01 ~]# uname -r2.6.32-642.el6.x86_64

2、安裝epel源

所有節點

#備份mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup#下載epel源wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo#生成緩存yum makecache

3、環境初始化

1)修改每台主機名

172.16.1.241 master172.16.1.242 slave01172.16.1.243 slave02172.16.1.244 manager

其中master對外提供寫服務,備選master(實際的slave,主機名slave01)提供讀服務,slave也提供相關的讀服務,一旦master宕機,將會把備選master提升為新的master,slave指向新的master。

2)主機名解析

#每台伺服器執行修改主機名解析

echo 172.16.1.241 master172.16.1.242 slave01172.16.1.243 slave02172.16.1.244 manager >>/etc/hosts

3)ssh無密碼登錄

使用key登錄,工作中常用,伺服器之間無需密碼驗證的。關於配置使用key登錄,一點需要注意:不能禁止 password 登陸,否則會出現錯誤

注意:所以全部機器都要相互做密鑰登錄。伺服器間,無密碼ssh登錄 #主機:master執行命令

[root@master ~]# ssh-keygen -t rsa[root@master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@manager[root@master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave01[root@master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave02

#主機:slave01執行命令

[root@slave01 ~]# ssh-keygen -t rsa[root@slave01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@manager[root@slave01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@master[root@slave01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave02

#主機: slave02執行命令

[root@slave02 ~]# ssh-keygen -t rsa[root@slave02 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@manager[root@slave02 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@master[root@slave02 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave01

#主機:manager執行命令

[root@manager ~]# ssh-keygen -t rsa[root@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@master[root@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave01[root@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave02

四、規劃mysql

1)安裝mysql

#master配置文件/etc/my.cnf 核心配置如下:

basedir = /application/mysqldatadir = /application/mysql/dataport = 3306server_id = 241socket = /tmp/mysql.socklog-bin=mysql-binlog-slave-updatesexpire_logs_days = 10

#slave01配置文件/etc/my.cnf 核心配置如下:

basedir = /application/mysqldatadir = /application/mysql/dataport = 3306server_id = 242socket = /tmp/mysql.socklog-bin=mysql-binlog-slave-updatesexpire_logs_days = 10

#slave02配置文件/etc/my.cnf 核心配置如下:

basedir = /application/mysqldatadir = /application/mysql/dataport = 3306server_id = 243socket = /tmp/mysql.socklog-bin=mysql-binlog-slave-updatesexpire_logs_days = 10read_only = 1

2)配置master、slave01和slave02之間的主從複製

注意:binlog-do-db 和 replicate-ignore-db 設置必須相同。 MHA 在啟動時候會檢測過濾規則,如果過濾規則不同,MHA 不啟動監控和故障轉移。

在MySQL5.6 的Replication配置中,master端同樣要開啟兩個重要的選項,server-id和log-bin,並且選項server-id在全局架構中並且唯一,不能被其它主機使用,這裡採用主機ip地址的最後一位充當server-id的值;slave端要開啟relay-log;

#主機: master執行命令

[root@master ~]# egrep "log-bin|server_id" /etc/my.cnf server_id = 241log-bin=mysql-bin

#主機: slave01執行命令

[root@slave01 ~]# egrep "log-bin|server_id" /etc/my.cnf server_id = 242log-bin=mysql-bin

#主機: slave02執行命令

[root@slave02 ~]# egrep "log-bin|server_id" /etc/my.cnf server_id = 243log-bin=mysql-bin

3)在master、slave01上創建主從同步的賬號。

slave01是備用master,這個也需要建立授權用戶。

#master[root@master ~]# mysql -e "grant replication slave on *.* to backup@172.16.1.% identified by backup;flush privileges;#slave01 [root@slave01 ~]# mysql -e "grant replication slave on *.* to backup@172.16.1.% identified by backup;flush privileges;"

4)在master上執行命令,查看master狀態信息

[root@master ~]# mysql -e show master status;+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000007 | 107 | | |+------------------+----------+--------------+------------------+

5)在slave01和slave02上執行主從同步

#slave01配置主從

[root@slave01 ~]# mysqlmysql> change master to master_host=172.16.1.241,master_user=backup,master_password=backup,master_port=3306,master_log_file=mysql-bin.000007,master_log_pos=107;Query OK, 0 rows affected (0.12 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: 172.16.1.241 Master_User: backup Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000007 Read_Master_Log_Pos: 107 Relay_Log_File: slave01-relay-bin.000002 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000007 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: 107 Relay_Log_Space: 411 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: 0Master_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: 2411 row in set (0.00 sec)

#slave02配置主從

[root@slave02 ~]# mysqlmysql> change master to master_host=172.16.1.241,master_user=backup,master_password=backup,master_port=3306,master_log_file=mysql-bin.000007,master_log_pos=107;Query OK, 0 rows affected (0.12 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: 172.16.1.241 Master_User: backup Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000007 Read_Master_Log_Pos: 107 Relay_Log_File: slave01-relay-bin.000002 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000007 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: 107 Relay_Log_Space: 411 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: 0Master_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: 2411 row in set (0.00 sec)

#至此主從已經配置完成!

推薦閱讀:

自助結帳,揭開新零售市場的龐大商機
調用第三方介面的架構優化
Windows Server 2008 智能 DNS Server 部署指南
藍圖系列(一):高並發、高可用、高性能、分散式系統架構

TAG:MySQL | 系統架構 | 高可用 |