部署mysql高可用集羣。
mysql MMM + mysql主從同步
實驗拓撲
監控主機pc9
-------> 從pc5(pc7從)
主pc8(pc7從) -----> 主pc7(pc8從)
-------> 從pc6(pc7從)
一,安裝MySQL
... ...
1.1,配置四臺服務器的主配置文件/etc/my.cnf
主pc8配置:
[root@pc8 ~]# vim /etc/my.cnf
[mysqld]
log-bin=master8 #開啓二進制log日誌
server_id=8
主pc7配置:
[root@pc7 ~]# vim /etc/my.cnf
[mysqld]
log-bin=master7
server_id=7
log-slave-updates 開啓級連複製功能
從pc6配置:
[root@pc6 ~]# cat /etc/my.cnf
[mysqld]
server_id=6
從pc5配置:
[root@pc5 ~]# cat /etc/my.cnf
[mysqld]
server_id=5
1.2,四臺服務器都要重啓mysql服務:
# /etc/init.d/mysql restart
3.1,首次登入需要修改密碼
[root@pc8 ~]# cat /root/.mysql_secret //初始密碼存放路徑
# The random password set for the root user at Thu Mar 2 11:15:58 2017 (local time): m9vGqj2s
[root@pc8 ~]# mysql -hlocalhost -uroot -pm9vGqj2s
mysql> set password for root@"localhost"=password("123"); //修改初始密碼
Query OK, 0 rows affected (0.00 sec)
3.2,分別在PC8和PC7服務器創建從服務器授權:
mysql> grant replication slave on *.* to slaveuser@"%" identified by "123456";
3.3,配置pc7爲pc8的從服務器:
[root@pc8 ~]# mysql -hlocalhost -uroot -p123
mysql> show master status;
+----------------+----------+--------------+------------------+----------
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB
+----------------+----------+--------------+------------------+----------
| master8.000004 | 120 | | | |
+----------------+----------+--------------+------------------+----------
3.4,在PC7配置master服務器:
[root@pc7 ~]# mysql -uroot -p123
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to
-> master_host="192.168.4.8",
-> master_user="slaveuser",
-> master_password="123",
-> master_log_file="master8.000004",
-> master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.39 sec)
mysql> start slave;
Query OK, 0 rows affected (0.05 sec)
mysql> show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
-------------------------------------------------------------------------
主從同步的工作過程
Slave_IO_Running: Yes
IO線程:負責把主數據庫服務器binlog日誌裏的sql命令拷貝到本機的中繼日誌文件裏。
IO線程 No狀態的原因?
從數據庫服務器連接不上主數據庫服務器:
ping
iptables
selinux
連接的授權用戶
binlog日誌文件指定錯誤
binlog日誌pos點位置錯誤
查看報錯信息
Last_IO_Error: 報錯信息
修改錯誤:
stop slave;
change master to 選項="值",選項="值",;
start slave;
-----------------------------------------------------------------------
Slave_SQL_Running: Yes
SQL線程:執行本機中繼日誌文件裏的sql命令,把數據寫進本機的庫裏。
IO線程 No狀態的原因?
執行本機中繼日誌文件裏的sql命令時,本機沒有命令使用到的庫 表 或字段。
查看報錯信息
Last_SQL_Error: 報錯信息
ls /var/lib/mysql/
master.info 記錄連接主數據庫服務器配置信息
relay-log.info 記錄中繼日誌信息文件
mail-relay-bin.00000x 中繼日誌文件
mail-relay-bin.index 中繼日誌的索引文件
----------------------------------------------------------------------
測試:
4.1,在PC8中創建一個數據庫。
mysql> create database test008;
Query OK, 1 row affected (0.01 sec)
4.2,到PC5-7看是否同步。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db001 |
| db002 |
| db007 |
| mysql |
| performance_schema |
| test |
| test008 |
| webdb |
+--------------------+
9 rows in set (0.06 sec)
=================================================
三,安裝MySQL-MMM
軟件介紹:MySQL主主複製管理器
監控、故障轉移 一套腳本套件(perl)
提供2種服務:
mmm-monitor: 負責所有的監控工作, 決定故障節點的移除或恢復 。
mmm-agent : 運行在MySQL服務器上,提供簡單遠程服務集、提供給監控節點。
-------------------------------------------------------------------------
1.1,在所以主機上安裝mysql mmm 軟件 (4臺數據庫服務器 + 監控服務器)
#tar -zxvf mysql-mmm-2.2.1.tar.gz
#cd mysql-mmm-2.2.1
#make install
#cd /etc/mysql-mmm
#ls *.conf
mmm_agent.conf mmm-agent服務的主配置文件(數據庫主機)
mmm_mon.conf mmm-monitor服務的主配置文件(監控主機)
mmm_common.conf 公信息配置文件,在所有主機上都要配置
mmm_tools.conf
1.2,在4臺數據庫服務器上做如下授權
mysql>grant replication client,process,super on *.* to agent@"%" identified by "123";
mysql>grant replication client on *.* to monitor@"%" identified by "123";
-------------------------------------------------------------------------
[root@pc5 mysql-mmm]# ls
mmm_agent.conf mmm_common.conf mmm_mon.conf mmm_tools.conf
mmm_common.conf 公信息配置文件,在所有主機上都要配置
-------------------
active_master_role writer
<host default>
cluster_interface eth0 //本機當前使用的網卡
pid_path /var/run/mmm_agentd.pid
bin_path /usr/lib/mysql-mmm/
replication_user slaveuser
replication_password 123
agent_user agent
agent_password 123
</host>
<host master8>
ip 192.168.4.8
mode master
peer master7
</host>
<host master7>
ip 192.168.4.7
mode master
peer master8
</host>
<host slave6>
ip 192.168.4.6
mode slave
</host>
<host slave5>
ip 192.168.4.5
mode slave
</host>
<role writer>
hosts master8, master7
ips 192.168.4.101
mode exclusive
</role>
<role reader>
hosts slave6, slave5
ips 192.168.4.102, 192.168.4.103
mode balanced
</role>
-----------------------------------------------------------------------
mmm_agent.conf mmm-agent服務的主配置文件(數據庫主機)
[root@pc5 mysql-mmm]# cat mmm_agent.conf
include mmm_common.conf
this slave5
-------------------------------------------------------------------------
mmm_mon.conf mmm-monitor服務的主配置文件(監控主機)
[root@pc9 mysql-mmm]# cat mmm_mon.conf
include mmm_common.conf
<monitor>
ip 192.168.4.9 //本機IP
pid_path /var/run/mmm_mond.pid
bin_path /usr/lib/mysql-mmm/
status_path /var/lib/misc/mmm_mond.status
ping_ips 192.168.4.5, 192.168.4.6, 192.168.4.8, 192.168.4.7 //要監控的數據庫服務器
</monitor>
<host default>
monitor_user monitor //需數據庫授權
monitor_password 123
</host>
debug 0 0|1 報錯時開機可以設置爲1,方便查看報錯信息
=========================================================================
1.3,在所以主機上 安裝服務運行時依賴的軟件包。
tar -zxvf Algorithm-Diff-1.1902.tar.gz
cd Algorithm-Diff-1.1902
perl Makefile.PL
make
make install
rpm -ivh --nodeps perl-Log-Log4perl-1.26-1.el6.rf.noarch.rpm
tar -zxvf Proc-Daemon-0.03.tar.gz
cd Proc-Daemon-0.03
perl Makefile.PL
make
make install
1.4,在4臺數據庫服務器上安裝獲取虛擬Ip地址程序。
#yum -y install gcc gcc-c++
#gunzip Net-ARP-1.0.8.tgz
#tar -xvf Net-ARP-1.0.8.tar
#cd Net-ARP-1.0.8
#perl Makefile.PL
#make
#make install
1.5,啓動服務
1 啓動數據庫服務器上mmm-agent 服務
[root@pc8 mysql-mmm]# /etc/init.d/mysql-mmm-agent start
Daemon bin: '/usr/sbin/mmm_agentd'
Daemon pid: '/var/run/mmm_agentd.pid'
Starting MMM Agent daemon... Ok
[root@stu mysql-mmm]# netstat -utnalp | grep agent
tcp 0 0 172.40.50.171:9989 0.0.0.0:* LISTEN 24009/mmm_agentd
[root@pc8 mysql-mmm]# netstat -utnalp | grep :9989
tcp 0 0 172.40.50.171:9989 0.0.0.0:* LISTEN 24009/mmm_agentd
日誌文件 /var/log/mysql-mmm/mmm_agentd.log
-------------------------------------------------------------------------
2 啓動監控服務器上mmm-monitor 服務
[root@pc9 mysql-mmm]# /etc/init.d/mysql-mmm-monitor start
Daemon bin: '/usr/sbin/mmm_mond'
Daemon pid: '/var/run/mmm_mond.pid'
Starting MMM Monitor daemon: Ok
[root@pc9 mysql-mmm]# netstat -utnalp | grep :9988
tcp 0 0 172.40.50.177:9988 0.0.0.0:* LISTEN 23544/mmm_mond
3.查看數據庫服務器的狀態:
[root@pc9 mysql-mmm]# mmm_control show
[root@pc9 mysql-mmm]# mmm_control set_online master8
[root@pc9 mysql-mmm]# mmm_control set_online master7
[root@pc9 mysql-mmm]# mmm_control set_online slave5
[root@pc9 mysql-mmm]# mmm_control set_online slave6
[root@pc9 mysql-mmm]# mmm_control show
master7(192.168.4.7) master/ONLINE. Roles:
master8(192.168.4.8) master/ONLINE. Roles: writer(192.168.4.101)
slave5(192.168.4.5) slave/ONLINE. Roles: reader(192.168.4.103)
slave6(192.168.4.6) slave/ONLINE. Roles: reader(192.168.102)
-------------------------------------------------------------------------
4.查看虛擬ip地址
[root@pc8 ~]# ip addr show | grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.4.8/24 brd 192.168.4.255 scope global eth0
inet 192.168.4.101/32 scope global eth0
======================================================