mysql高可用架構之mysql-mmm配置詳解

實驗系統:CentOS 6.6_x86_64mysql

實驗前提:防火牆和selinux都關閉linux

實驗說明:本實驗共有5臺主機,IP分配如拓撲sql

實驗軟件:mariadb-10.0.20  mysql-mmm-2.2.1  mysql-mmm-monitor-2.2.1  mysql-mmm-agent-2.2.1數據庫

下載地址:http://pan.baidu.com/s/1bnnYiMrvim

實驗拓撲:服務器

    

1、準備工做網絡

  1.根據下表,將各主機名進行修改:ssh

    

  2.修改hosts文件,添加以下內容:測試

vim /etc/hosts
------------------------------------------->
192.168.19.21   mon
192.168.19.66   db1
192.168.19.74   db2
192.168.19.76   db3 
192.168.19.79   db4

  3.規劃虛擬ip,列表以下:ui

    

2、安裝mariadb並配置

  1.在db1-4上安裝:

tar xf mariadb-10.0.20-linux-x86_64.tar.gz  -C /usr/local/
cd /usr/local/
ln -sv mariadb-10.0.20-linux-x86_64 mysql
useradd -r mysql
mkdir -pv /mydata/data
chown -R mysql.mysql /mydata/data/
cd mysql/
chown -R root.mysql .
scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
cp support-files/my-large.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on

  2.編輯配置文件:

    db1:

vim /etc/my.cnf
--------------------------------------------->
[mysqld]
server-id = 1
datadir = /mydata/data
log-bin = /mydata/data/mysql1-bin
binlog_format = ROW
relay_log = /mydata/data/relay-log
auto-increment-increment = 2
auto-increment-offset = 1
sync_binlog = 1
sync_master_info = 1
sync_relay_log = 1
sync_relay_log_info = 1
max_binlog_size  = 100M
log_slave_updates   = 1
<---------------------------------------------
service mysqld start

    db2:

vim /etc/my.cnf
--------------------------------------------->
[mysqld]
server-id = 2
datadir = /mydata/data
log-bin = /mydata/data/mysql2-bin
binlog_format = ROW
relay_log = /mydata/data/relay-log
auto-increment-increment = 2
auto-increment-offset = 2
sync_binlog = 1
sync_master_info = 1
sync_relay_log = 1
sync_relay_log_info = 1
max_binlog_size  = 100M
log_slave_updates   = 1
<---------------------------------------------
service mysqld start

    db3:

vim /etc/my.cnf
--------------------------------------------->
[mysqld]
server-id = 3
datadir = /mydata/data
log-bin = /mydata/data/mysql3-bin
binlog_format = ROW
relay_log = /mydata/data/relay-log
sync_binlog = 1
sync_master_info = 1
sync_relay_log = 1
sync_relay_log_info = 1
max_binlog_size  = 100M
log_slave_updates   = 1
<---------------------------------------------
service mysqld start

  db4:

vim /etc/my.cnf
--------------------------------------------->
[mysqld]
server-id = 4
datadir = /mydata/data
log-bin = /mydata/data/mysql4-bin
binlog_format = ROW
relay_log = /mydata/data/relay-log
sync_binlog = 1
sync_master_info = 1
sync_relay_log = 1
sync_relay_log_info = 1
max_binlog_size  = 100M
log_slave_updates   = 1
<---------------------------------------------
service mysqld start

  3.在db1上建立用戶:

     這裏須要建立三個用戶,以下表:

    

/usr/local/mysql/bin/mysql
------------------------------------------------->
GRANT REPLICATION CLIENT ON *.* TO 'mmm_monitor'@'192.168.19.%' IDENTIFIED BY '123456';
GRANT SUPER,REPLICATION CLIENT,PROCESS ON *.* TO 'mmm_agent'@'192.168.19.%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.19.%' IDENTIFIED BY '123456'

  4.查看二進制日誌位置:

FLUSH TABLES WITH READ LOCK;          //施加鎖
SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| mysql1-bin.000004 |      936 |              |                  |
+-------------------+----------+--------------+------------------+

  5.不要關閉這個mysql進程鏈接,避免鎖失效,咱們另起一個ssh鏈接db1服務器,進行數據庫備份:

/usr/local/mysql/bin/mysqldump --all-databases > /tmp/database-backup.sql

  6.回到剛纔mysql進程,進行解鎖:

UNLOCK TABLES;

  7.將database-backup.sql文件複製到其餘db節點:

scp /tmp/database-backup.sql db2:/tmp/
scp /tmp/database-backup.sql db3:/tmp/
scp /tmp/database-backup.sql db4:/tmp/

  8.db2-4主機導入sql文件,並刷新權限:

/usr/local/mysql/bin/mysql < /tmp/database-backup.sql
/usr/local/mysql/bin/mysql
------------------------------------------------->
FLUSH PRIVILEGES;

3、設置複製

  1.在db2-4上操做,將db1設置爲db2-4的主:

CHANGE MASTER TO MASTER_HOST='192.168.19.66',MASTER_USER='replication',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql1-bin.000004',MASTER_LOG_POS=936;
START SLAVE;

  2.查看狀態:

SHOW SLAVE STATUS\G

    

    

    

  3.查看db2的master日誌位置:

SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| mysql2-bin.000001 |      313 |              |                  |
+-------------------+----------+--------------+------------------+

  4.在db1上操做,將db2設置爲db1的主:

CHANGE MASTER TO MASTER_HOST='192.168.19.74',MASTER_USER='replication',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql2-bin.000001',MASTER_LOG_POS=313;
START SLAVE;
SHOW SLAVE STATUS\G

    

4、安裝MMM

  1.建立用戶:

useradd -s /sbin/nologin mmmd

  2.在mon上安裝:

yum -y install mysql-mmm-monitor

  3.在db1-4上安裝:

yum -y install mysql-mmm-agent

  4.編寫配置文件,五臺主機必須一致:

vim /etc/mysql-mmm/mmm_common.conf
-------------------------------------------------------->
active_master_role      writer

<host default>
    cluster_interface       eth0
    pid_path                /var/run/mysql-mmm/mmm_agentd.pid
    bin_path                /usr/libexec/mysql-mmm/
    replication_user        replication           //用於複製的用戶
    replication_password    123456                //複製用戶的密碼
    agent_user              mmm_agent             //用於改變模式的用戶
    agent_password          123456                //改變模式用戶的密碼
</host>

<host db1>
    ip      192.168.19.66
    mode    master
    peer    db2                                   //須要監視的「同伴」
</host>

<host db2>
    ip      192.168.19.74
    mode    master
    peer    db1
</host>

<host db3>
    ip      192.168.19.76
    mode    slave
</host>

<host db4>
    ip      192.168.19.79
    mode    slave
</host>

<role writer>
    hosts   db1, db2                             //可寫節點
    ips     192.168.19.150                       //寫操做使用的VIP
    mode    exclusive                            //排他模式,此處資源同一時間只能分配給一個主機
</role>

<role reader>
    hosts   db1, db2, db3, db4                  //可讀節點
    ips     192.168.19.151, 192.168.19.152, 192.168.19.153, 192.168.19.154    //讀操做使用的VIP
    mode    balanced                            //平衡模式
</role>

  5.在db1-4上修改mmm_agent.conf,只須要修改db1這裏,是哪臺就改爲哪臺,這裏只給出db1的:

vim /etc/mysql-mmm/mmm_agent.conf
------------------------------------------------->
include mmm_common.conf

# The 'this' variable refers to this server.  Proper operation requires 
# that 'this' server (db1 by default), as well as all other servers, have the 
# proper IP addresses set in mmm_common.conf.
this db1             //只改這裏

  6.配置mon上的mmm_mon.conf

vim /etc/mysql-mmm/mmm_mon.conf
---------------------------------------------------->
include mmm_common.conf

<monitor>
    ip                  127.0.0.1
    pid_path            /var/run/mysql-mmm/mmm_mond.pid
    bin_path            /usr/libexec/mysql-mmm
    status_path         /var/lib/mysql-mmm/mmm_mond.status
    ping_ips           192.168.19.50, 192.168.19.66, 192.168.19.74, 192.168.19.76, 192.168.19.79
//用於測試網絡可用性的IP地址,其中有一個地址能ping通,就表明網絡正常,不要寫入本機的ip地址
auto_set_online 60 //是否設置自動上線,若是該值大於0,抖動的主機在抖動的時間範圍事後,則設置自動上線 # The kill_host_bin does not exist by default, though the monitor will # throw a warning about it missing. See the section 5.10 "Kill Host # Functionality" in the PDF documentation. # # kill_host_bin /usr/libexec/mysql-mmm/monitor/kill_host # </monitor> <host default> monitor_user mmm_monitor //用於監控的用戶 monitor_password 123456 //監控用戶的密碼 </host> debug 0

5、啓動測試:

  1.在db1-4上啓動agents:

chkconfig mysql-mmm-agent on
service mysql-mmm-agent start

  2.在mon上啓動monitor:

vim /etc/default/mysql-mmm-monitor
--------------------------------------------------->
ENABLED=1
<---------------------------------------------------
service mysql-mmm-monitor start

  3.檢查集羣狀態:

mmm_control show

    

    若是服務器狀態不是ONLINE,能夠用以下命令將服務器上線,例如:

mmm_control set_online db1

  4.從剛纔圖片能夠看到,寫請求的VIP在db1上,全部從節點也都把db1當作主節點。下面將db1停掉:

service mysqld stop          //停掉db1的mysql服務

    能夠看到寫請求的VIP已經轉移到db2上了,且從節點的主都指向了db2:

    

    

  5.最後開啓db三、db4的只讀:

vim /etc/my.cnf
------------------------------->
[mysqld]
read_only = 1
<-------------------------------
service mysqld restart

   到此爲止,全部的配置都完成了。一個writer的VIP和四個reader的VIP都已經正常分配,你們能夠建立一個遠程登陸帳號進行測試,這裏就再也不給出具體的過程了。因爲時間緊迫,且經驗有限,配置過程當中可能會出現紕漏,若有發現請及時聯繫我,歡迎你們的指正,謝謝! 聯繫QQ:82800542

相關文章
相關標籤/搜索