MySQL——MMM高可用

MySQL-MMM優缺點:

優點:高可用性,擴展性好,出現故障自動切換,對於主主同步,在同一時間只提供一臺數據庫寫操作,保證的數據的一致性。

缺點:Monitor節點是單點,可以結合Keepalived實現高可用。

實驗環境:

master1服務器       192.168.13.167
master2服務器   192.168.13.151
slave1服務器        192.168.13.168
slave2服務器        192.168.13.145
monitor服務器   192.168.13.164

1、在master1\master2\slave1\slave2都需要安裝MYSQL數據庫

[[email protected] ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
##獲取源地址
[[email protected] ~]# yum -y install epel-release  ##安裝epel源
[[email protected] ~]# yum clean all && yum makecache  ##yum緩存清空
[[email protected] ~]# yum -y install mariadb-server mariadb   ##安裝mariadb數據庫
[[email protected] ~]# systemctl stop firewalld.service   ##關閉防火牆
[[email protected] ~]# setenforce 0
[[email protected] ~]# systemctl start mariadb.service    ##開啓數據庫

2、修改master1數據庫配置文件(使用scp同步到其他數據庫服務器上,monitor不需要)

[[email protected] ~]# vim /etc/my.cnf   ##修改配置文件
[mysqld]
log_error=/var/lib/mysql/mysql.err     ##錯誤日誌文件
log=/var/lib/mysql/mysql_log.log       ##主從日誌存放位置
log_slow_queries=/var/lib/mysql_slow_queris.log  ##man日誌
binlog-ignore-db=mysql,information_schema     ##二進制文件
character_set_server=utf8   ##字符集
log_bin=mysql_bin   ##二進制日誌文件
server_id=11    ##服務id(不能相同)
log_slave_updates=true   ##允許從服務器更新
sync_binlog=1   ##同步日誌
auto_increment_increment=2    ##自增列
auto_increment_offset=1           ##起始點
[[email protected] ~]# systemctl restart mariadb.service   ##重啓數據庫

使用scp複製數據庫配置文件到其他服務器上的時候,需要修改server-id不能一樣

[[email protected] ~]# scp /etc/my.cnf [email protected]:/etc/   (server-id=22)
[[email protected] ~]# scp /etc/my.cnf [email protected]:/etc/   (server-id=33)
[[email protected] ~]# scp /etc/my.cnf [email protected]:/etc/   (server-id=44)
[[email protected] ~]# netstat -anpt | grep 3306   ##查看端口3306
tcp      0   0 0.0.0.0:3306     0.0.0.0:*         LISTEN      4235/mysqld

3、配置主主複製(master1,master2)兩臺主服務器相互複製

//master1服務器//

[[email protected] ~]# mysql   ##進入數據庫

MariaDB [(none)]> show master status;   ##查看主服務器的狀態信息
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000001 |      245 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> grant replication slave on *.* to 'replication'@'192.168.13.%' identified by '123456';  
##授權給13段的網段複製的權限用戶名replication密碼123456
Query OK, 0 rows affected (0.00 sec)

//master2服務器//

MariaDB [(none)]> change master to master_host='192.168.13.167',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;
##在master2上同步master1服務器
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant replication slave on *.* to 'replication'@'192.168.13.%' identified by '123456';
##在master2上授權複製權限
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show master status;   ##查看master2的服務器的狀態信息
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000001 |      410 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> flush privileges;   ##刷新權限
Query OK, 0 rows affected (0.00 sec)

//master1服務器//

MariaDB [(none)]> change master to master_host='192.168.13.151',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=410; 
##master1同步master2服務器數據庫
Query OK, 0 rows affected (0.03 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

//master1,master2上開啓同步//

MariaDB [(none)]> start slave;    ##開啓同步
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G;    ##查看同步狀態信息
                         Slave_IO_Running: Yes
                        Slave_SQL_Running: Yes

4、在slave1,slave2做主從同步,注意日誌文件和位置參數的改變

MariaDB [(none)]> change master to master_host='192.168.13.167',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;
##在從服務器上同步master1主服務器
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;   ##刷新權限
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> start slave;    ##開啓同步
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G;    ##查看同步的狀態信息
                             Slave_IO_Running: Yes
                            Slave_SQL_Running: Yes

5、測試主主,主從的同步狀態

//主服務器master1//
MariaDB [(none)]> create database school;   ##創建數據庫
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;   ##查看數據庫
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| test               |
+--------------------+
5 rows in set (0.00 sec)

//從服務器上查看數據庫//
MariaDB [(none)]> show databases;   ##實現主主,主從同步
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| test               |
+--------------------+
5 rows in set (0.00 sec)

6、安裝MMM,在所有服務器上安裝,注意:epel源要配置好配置阿里雲源,然後安裝epel-release源。(monitor上需要先安裝源,清空緩存,然後安裝MMM)

##monitor服務器上
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

yum -y install epel-release

yum clean all && yum makecache

yum -y install mysql-mmm*

##其餘服務器都需要安裝mmm
[[email protected] ~]# yum -y install mysql-mmm*

7、安裝結束後,所有服務器上對mmm進行配置

[[email protected] ~]# vim /etc/mysql-mmm/mmm_common.conf   ##所有主機上都要配置,直接複製多份

……
<host default>
        cluster_interface       ens33    ##修改網卡
        …
        replication_user        replication   ##修改用戶名
        replication_password    123456   ##密碼
        agent_user              mmm_agent
        agent_password          123456     ##密碼

<host db1>
        ip      192.168.13.167   ##master1地址
        mode    master
        peer    db2
</host>

<host db2>
        ip      192.168.13.151   ##master2地址
        mode    master
        peer    db1
</host>

<host db3>
        ip      192.168.13.168   ##slave1地址
        mode    slave
</host>

<host db4>
        ip      192.168.13.145   ##slave2地址
        mode    slave
</host>

<role writer>
        hosts   db1, db2      ##寫服務器虛擬ip
        ips     192.168.13.250
        mode    exclusive
</role>

<role reader> 
        hosts   db3, db4    ##讀服務器虛擬ip
        ips     192.168.13.252, 192.168.13.251
        mode    balanced
</role>

##複製到其他的服務器中
[[email protected] ~]# scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/
[email protected]'s password: 
mmm_common.conf                               100%  836   267.1KB/s   00:00    
[[email protected] ~]# scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/
[email protected]'s password: 
mmm_common.conf                               100%  836   863.2KB/s   00:00    
[[email protected] ~]# scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/
[email protected]'s password: 
mmm_common.conf                               100%  836   904.7KB/s   00:00    
[[email protected] ~]# scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/

8、在monitor服務器上配置

[[email protected] ~]# vim /etc/mysql-mmm/mmm_mon.conf
<host default>
        monitor_user        mmm_monitor
        monitor_password    123456    ##修改monitor的密碼
</host>

9、在所有數據庫上爲mmm_agent代理授權,爲mmm_monitor授權監控

MariaDB [(none)]> grant super, replication client, process on *.* to 'mmm_agent'@'192.168.13.%' identified by '123456';
##授權代理
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant replication client on *.* to 'mmm_monitor'@'192.168.13.%' identified by '123456';
##授權監控
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;   ##刷新權限
Query OK, 0 rows affected (0.00 sec)

10、修改所有數據庫的mmm_agent.conf

[[email protected] ~]# vim /etc/mysql-mmm/mmm_agent.conf
this db1 //根據規劃進行逐一調整
[[email protected] ~]# vim /etc/mysql-mmm/mmm_agent.conf
this db2 //根據規劃進行逐一調整
[[email protected] ~]# vim /etc/mysql-mmm/mmm_agent.conf
this db3 //根據規劃進行逐一調整
[[email protected] ~]# vim /etc/mysql-mmm/mmm_agent.conf
this db4 //根據規劃進行逐一調整
##所有數據庫開啓
[[email protected] ~]systemctl start mysql-mmm-agent.service   ##開啓代理服務
[[email protected] ~]systemctl enable mysql-mmm-agent.service  ##加入開機自啓動

11、在monitor上配置

[[email protected] ~]# vim /etc/mysql-mmm/mmm_mon.conf
<monitor>
        ip                  127.0.0.1
        pid_path            /run/mysql-mmm-monitor.pid
        bin_path            /usr/libexec/mysql-mmm
        status_path         /var/lib/mysql-mmm/mmm_mond.status
        ping_ips            192.168.13.167,192.168.13.151,192.168.13.168,192.168.13.
145
        ##所有數據庫服務器地址
        auto_set_online     10    ##自動在線時間
[[email protected] ~]# systemctl stop firewalld.service    ##關閉防火牆
[[email protected] ~]# setenforce 0
[[email protected] ~]# systemctl start mysql-mmm-monitor.service    ##開啓監控服務
[[email protected] ~]# mmm_control show   ##查看主從的飄逸地址
    db1(192.168.13.167) master/ONLINE. Roles: writer(192.168.13.250)
    db2(192.168.13.151) master/ONLINE. Roles: 
    db3(192.168.13.168) slave/ONLINE. Roles: reader(192.168.13.252)
    db4(192.168.13.145) slave/ONLINE. Roles: reader(192.168.13.251)

12、測試漂移地址

[[email protected] ~]# systemctl stop mariadb.service  ##模擬停止master1服務器
//monitor服務器上查看//
[[email protected] ~]# mmm_control show
    db1(192.168.13.167) master/HARD_OFFLINE. Roles: 
    db2(192.168.13.151) master/ONLINE. Roles: writer(192.168.13.250)
    db3(192.168.13.168) slave/ONLINE. Roles: reader(192.168.13.252)
    db4(192.168.13.145) slave/ONLINE. Roles: reader(192.168.13.251)
(重啓master1數據庫服務,虛擬地址不會被搶佔到master1)
mmm_control checks all //需要各種OK
mmm_control move_role writer db1  ##可以切換虛擬地址

13、在monitor上安裝MySQL作爲測試機用虛擬ip登錄數據庫

[[email protected] ~]# yum install mysql -y

//master1服務器上授權monitor地址訪問//

MariaDB [(none)]> grant all on *.* to 'testdba'@'192.168.13.164' identified by '123456';
##授權monitor地址訪問
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> flush privileges;   ##刷新權限
Query OK, 0 rows affected (0.00 sec)

[[email protected] ~]# mysql -utestdba -p -h 192.168.13.250   ##使用虛擬地址即可登錄數據庫
Enter password: 

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