在Centos7玩轉Mysql半同步和keepalived+MHA(一)

網上這類教程還是有很多的,但是多半不能拿過來自己配置,比如my.cnf裏面"-"和"_"有些博文寫錯導致mysql無法啓動,排查起來費時費力,本文是網上的資料的集合也是填了幾個基本會遇到的坑。

玩轉Mysql半同步和keepalived+MHA(一)只涉及mysql半同步的內容,keepalived+MHA我放在第二章。

本次配置採用虛擬機環境

角色IPOSMySQLMHA
mha-manager192.168.1.92CentOS 7mha-manager mha-node
mysql-master192.168.1.151CentOS 7mysql-mastermha-node
mysql-slave01192.168.1.152CentOS 7mysql-slavemha-node
mysql-slave02192.168.1.153CentOS 7mysql-slavemha-node

分別在3臺Centos7安裝percona5.6

wget https://www.percona.com/redir/downloads/percona-release/redhat/latest/percona-release-0.1-4.noarch.rpm
rpm -ivh ./percona-release-0.1-4.noarch.rpm
yum install Percona-Server-server-56.x86_64 -y

192.168.1.151下面/etc/my.cnf的內容:

server-id=151
port=3306
log-bin=master-bin.log
log-bin-index=master-bin.index
expire-logs-days=14
#character-set-server = utf8mb4 #根據自己需要
#default-storage-engine=myisam #根據自己需要

192.168.1.152/3下面/etc/my.cnf的內容:

log-bin=mysql-bin
relay-log = slave-relay-bin
server-id=152  #另外一臺改下這裏server-id=153
relay-log-index = slave-relay-bin.index
#replicate-wild-ignore-table = mysql.% #看需求
#default-storage-engine=myisam #看需求

啓動mysql

systemctl restart mysql

啓動不了請認真檢查my.cnf和查看日誌。

進入主數據庫得到binlog日誌文件名和偏移量

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

注意:如果是在使用數據庫需要使用讀鎖

mysql>flush tables with read lock;

備份主庫數據庫,並使用source恢復到從機。

mysqldump test > test.sql

然後解鎖主庫

mysql>unlock tables;

由於本文是新建虛擬機沒有數據,這步可以略過。

記錄好binlogfile和position之後,在主庫創建用戶

grant replication slave on *.* to 'repl'@'192.168.1.152' identified by 'repl';
grant replication slave on *.* to 'repl'@'192.168.1.153' identified by 'repl';

分別進入152和153的mysql執行:

change master to master_host='192.168.1.151',master_user='repl',master_password='repl',master_port=3306,master_log_file='master-bin.000001',master_log_pos=1216;

安裝插件,分別進入3臺mysql執行:

mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so'; 
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
mysql> set global rpl_semi_sync_master_enabled=on ;
注意 從庫最後一行執行set global rpl_semi_sync_slave_enabled=on ;

注意:在啓動slave之前,如果你使用虛擬機克隆功能或者直接copy,要刪除/var/lib/mysql/auto.cnf,否則會提示同步失敗。

啓動slave,分別進入152和153的mysql執行:

mysql> start slave;
mysql> set global read_only=1;

在從庫查看show slave status\G;

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

看到以上兩個字段帶兵同步成功。

在主庫可以使用

show global status like 'rpl_semi%'; 

查看同步情況。

以上mysql半同步配置基本完成。

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