Home  >  Article  >  Database  >  Detailed explanation of MySQL high-availability MMM construction plan and architectural principles_MySQL

Detailed explanation of MySQL high-availability MMM construction plan and architectural principles_MySQL

WBOY
WBOYOriginal
2016-09-09 08:13:391224browse

Let’s take a look at the architecture first, as shown below:

Deployment

1. Modify hosts

Perform the same operation in all servers.

vim /etc/hosts

192.168.137.10 master
192.168.137.20 backup
192.168.137.30 slave
192.168.137.40 monitor

2. Add mysql user

It only needs to be executed on all database sides, not the monitoring side.

GRANT REPLICATION CLIENT ON *.* TO 'mmm_monitor'@'192.168.137.%' IDENTIFIED BY 'mmm_monitor'; 
GRANT SUPER,REPLICATION CLIENT, PROCESS ON *.* TO 'mmm_agent'@'192.168.137.%' IDENTIFIED BY 'mmm_agent';
flush privileges;

Note: The repl user has already been created when building the master-slave service.

3. Install monitoring software

Note: All monitoring terminals are installed, but the monitoring terminal only needs to use mysql-mmm-monitor, and mysql-mmm-agent does not need to be started.

On the database side, you only need to install mysql-mmm-agent

1. Execute on the monitoring server

wget http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm –ivh epel-release-6-8.noarch.rpm
yum –y install mysql-mmm*

2. Execute on the database server, it must be executed on every database server

wget http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm –ivh epel-release-6-8.noarch.rpm
yum -y install mysql-mmm-agent

Path description:

4. Configuration file

db server configuration files: mmm_agent.conf, mmm_common.conf

Configuration files of the monitoring server: mmm_mon.conf, mmm_common.conf (This file is the same for all servers)

Configure db server

1. Modify the master server

vim /etc/mysql-mmm/mmm_common.conf

active_master_role  writer  ###积极的master角色的标示,所有的db服务器都需要开启read_only参数,对于writer服务器监控代理会自动将read_only属性关闭。

<host default>
 cluster_interface  eth0  #####群集的网络接口
 pid_path    /var/run/mysql-mmm/mmm_agentd.pid ####pid路径
 bin_path    /usr/libexec/mysql-mmm/    #####可执行文件路径 
 replication_user  repl   #######复制用户
 replication_password repl   #######复制用户密码
 agent_user    mmm_agent  #######代理用户,用于更改只读操作
 agent_password   mmm_agent  #######代理用户密码
</host>

<host master>   ##########master1的host名
 ip  192.168.137.10 #####master1的ip
 mode master  ########角色属性,master代表是主
 peer backup  ########与master1对等的服务器的host名,也就是master2的服务器host名
</host>

<host backup>  ####和master的概念一样
 ip  192.168.137.20
 mode master
 peer master
</host>

<host slave>  #####从库的host名,如果存在多个从库可以重复一样的配置
 ip  192.168.137.30 ####从的ip
 mode slave #####slave的角色属性代表当前host是从
</host>

<role writer> ####writer角色配置
 hosts master,backup ####能进行写操作的服务器的host名,如果不想切换写操作这里可以只配置master,这样也可以避免因为网络延时而进行write的切换,但是一旦master出现故障那么当前的MMM就没有writer了只有对外的read操作。
 ips  192.168.137.100 #####对外提供的写操作的虚拟IP
 mode exclusive #####exclusive代表只允许存在一个主,也就是只能提供一个写的IP
</role>

<role reader> #####read角色配置
 hosts backup,slave ######对外提供读操作的服务器的host名,当然这里也可以把master加进来
 ips  192.168.137.120,192.168.137.130,192.168.137.140 ###对外提供读操作的虚拟ip,这两个ip和host不是一一对应的,并且ips也hosts的数目也可以不相同,如果这样配置的话其中一个hosts会分配两个ip
 mode balanced ###balanced代表负载均衡
</role>

At the same time, copy this file to other servers including the monitoring server, and the configuration remains unchanged

scp /etc/mysql-mmm/mmm_common.conf slave:/etc/mysql-mmm/
scp /etc/mysql-mmm/mmm_common.conf backup:/etc/mysql-mmm/
scp /etc/mysql-mmm/mmm_common.conf monitor:/etc/mysql-mmm/

2. Agent file configuration

vim /etc/mysql-mmm/mmm_agent.conf

Note: This configuration only configures the db server. The monitoring server does not need to be configured. The host name after this is changed to the host command of the current server, and master2 and slave are also changed to the host name of the corresponding server.

3. Start the agent process

chkconfig mysql-mmm-agent on

service mysql-mmm-agent start

Needs to be started on each db server

Configure monitoring server

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 #####群集的状态文件,也就是执行mmm_control show操作的显示来源。
 ping_ips   192.168.137.10,192.168.137.20,192.168.137.30 ######被监控的db服务器的ip地址
 auto_set_online  0 ####设置自动online的时间,默认是超过60s就将它设置为online,默认是60s,这里将其设为0就是立即online

 # 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 ####监控db服务器的用户
 monitor_password mmm_monitor ####监控db服务器的密码
</host>

debug 0  #######debug 0正常模式,1为debug模式

Note: The comments in the configuration file are for ease of understanding. It is best to remove the comments during deployment to avoid potential impacts caused by the comments.

Start monitoring process

chkconfig mysql-mmm- monitor on
service mysql-mmm-monitor start

Note: Whether on the db side or the monitoring side, if the configuration file is modified, the agent process and monitoring process need to be restarted.

Operation Analysis

Log files

Log files are often the key to analyzing errors, so you must be good at using log files to analyze problems.

db side:/var/log/mysql-mmm/mmm_agentd.log

Monitoring terminal:/var/log/mysql-mmm/mmm_mond.log

Command file

mmm_agentd: startup file of db agent process

mmm_mond: startup file of the monitoring process

mmm_backup: backup file

mmm_restore: Restore files

mmm_clone

mmm_control: Monitoring operation command file

On the db server side, there is only the mmm_agentd program, and the others are on the monitor server side.

mmm_control usage

mmm_control program can be used to monitor cluster status, switch writers, set online and offline operations, etc.

Valid commands are:
 help        - show this message ###帮助信息
 ping        - ping monitor ###ping当前的群集是否正常
 show        - show status ####群集在线状态检查
 checks [<host>|all [<check>|all]] - show checks status #####执行监控检查操作
 set_online <host>     - set host <host> online ####将host设置为online
 set_offline <host>    - set host <host> offline ###将host设置为offline
 mode        - print current mode. ####打印输出当前的mode
 set_active      - switch into active mode.
 set_manual      - switch into manual mode.
 set_passive      - switch into passive mode.
 move_role [--force] <role> <host> - move exclusive role <role> to host <host> ####移除writer服务器为指定的host服务器
          (Only use --force if you know what you are doing!)
 set_ip <ip> <host>    - set role with ip <ip> to host <host>

1. Check all db server cluster status

[root@monitor mysql-mmm]# mmm_control checks all
master ping   [last change: 2016/06/07 16:31:24] OK
master mysql  [last change: 2016/06/07 16:31:24] OK
master rep_threads [last change: 2016/06/07 16:31:24] OK
master rep_backlog [last change: 2016/06/07 16:31:24] OK: Backlog is null
slave ping   [last change: 2016/06/07 16:31:24] OK
slave mysql  [last change: 2016/06/07 16:31:24] OK
slave rep_threads [last change: 2016/06/07 16:31:24] OK
slave rep_backlog [last change: 2016/06/07 16:31:24] OK: Backlog is null
backup ping   [last change: 2016/06/07 16:31:24] OK
backup mysql  [last change: 2016/06/07 16:31:24] OK
backup rep_threads [last change: 2016/06/07 16:31:24] OK
backup rep_backlog [last change: 2016/06/07 16:31:24] OK: Backlog is null

Check items include: ping, whether mysql is running normally, whether the copy thread is normal, etc.

2. Check the online status of the cluster environment

[root@monitor mysql-mmm]# mmm_control show
 backup(192.168.137.20) master/ONLINE. Roles: reader(192.168.137.120)
 master(192.168.137.10) master/ONLINE. Roles: writer(192.168.137.100)
 slave(192.168.137.30) slave/ONLINE. Roles: reader(192.168.137.130)

3. Perform offline operation on the specified host

[root@monitor mysql-mmm]# mmm_control set_offline backup
OK: State of 'backup' changed to ADMIN_OFFLINE. Now you can wait some time and check all roles!
[root@monitor mysql-mmm]# mmm_control show
 backup(192.168.137.20) master/ADMIN_OFFLINE. Roles: 
 master(192.168.137.10) master/ONLINE. Roles: writer(192.168.137.100)
 slave(192.168.137.30) slave/ONLINE. Roles: reader(192.168.137.120), reader(192.168.137.130)

4. Perform onine operation on the specified host

[root@monitor mysql-mmm]# mmm_control set_online backup
OK: State of 'backup' changed to ONLINE. Now you can wait some time and check its new roles!
[root@monitor mysql-mmm]# mmm_control show
 backup(192.168.137.20) master/REPLICATION_FAIL. Roles: 
 master(192.168.137.10) master/ONLINE. Roles: writer(192.168.137.100)
 slave(192.168.137.30) slave/ONLINE. Roles: reader(192.168.137.120), reader(192.168.137.130)

[root@monitor mysql-mmm]# mmm_control show
 backup(192.168.137.20) master/ONLINE. Roles: reader(192.168.137.120)
 master(192.168.137.10) master/ONLINE. Roles: writer(192.168.137.100)
 slave(192.168.137.30) slave/ONLINE. Roles: reader(192.168.137.130)

5. Execute write switching

1. View the master corresponding to the current slave

[root@slave ~]# mysql -uroot -proot -e 'show slave status \G;'
Warning: Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
    Slave_IO_State: Waiting for master to send event
     Master_Host: 192.168.137.10
     Master_User: repl
     Master_Port: 3306
    Connect_Retry: 60
    Master_Log_File: mysql-bin.000073
   Read_Master_Log_Pos: 1461
    Relay_Log_File: mysql-relay-bin.000006
    Relay_Log_Pos: 283
  Relay_Master_Log_File: mysql-bin.000073
    Slave_IO_Running: Yes
   Slave_SQL_Running: Yes

2.Writer switching, make sure that the writer attribute in the mmm_common.conf file has the corresponding host configured, otherwise it cannot be switched

[root@monitor mysql-mmm]# mmm_control move_role writer backup
OK: Role 'writer' has been moved from 'master' to 'backup'. Now you can wait some time and check new roles info!
[root@monitor mysql-mmm]# mmm_control show
 backup(192.168.137.20) master/ONLINE. Roles: reader(192.168.137.120), writer(192.168.137.100)
 master(192.168.137.10) master/ONLINE. Roles: 
 slave(192.168.137.30) slave/ONLINE. Roles: reader(192.168.137.130)

3.save automatically switches from the library to the new master

[root@slave ~]# mysql -uroot -proot -e 'show slave status \G;'
Warning: Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
    Slave_IO_State: Waiting for master to send event
     Master_Host: 192.168.137.20
     Master_User: repl
     Master_Port: 3306
    Connect_Retry: 60
    Master_Log_File: mysql-bin.000039
   Read_Master_Log_Pos: 120
    Relay_Log_File: mysql-relay-bin.000002
    Relay_Log_Pos: 283
  Relay_Master_Log_File: mysql-bin.000039
    Slave_IO_Running: Yes
   Slave_SQL_Running: Yes

Other processing issues

 如果不想让writer从master切换到backup(包括主从的延时也会导致写VIP的切换),那么可以在配置/etc/mysql-mmm/mmm_common.conf时,去掉3612df8997eca9306e1f789dddf71f78中的backup

<role writer> ####writer角色配置
 hosts master ###这里只配置一个Hosts
 ips  192.168.137.100 #####对外提供的写操作的虚拟IP
 mode exclusive #####exclusive代表只允许存在一个主,也就是只能提供一个写的IP
</role>

 这样的话当master出现故障了writer写操作不会切换到backup服务器,并且slave也不会指向新的master,此时当前的MMM之前对外提供写服务。

总结

主从的延时会导致写VIP的切换。

       1.对外提供读写的虚拟IP是由monitor程序控制。如果monitor没有启动那么db服务器不会被分配虚拟ip,但是如果已经分配好了虚拟ip当monitor程序关闭了原先分配的虚拟ip不会立即关闭外部程序还可以连接访问(只要不重启网络),这样的好处就是对于monitor的可靠性要求就会低一些,目前还不知道能维持多长的时间,但是如果这个时候其中的某一个db服务器故障了就无法处理切换,也就是原先的虚拟ip还是维持不变,挂掉的那台DB的虚拟ip会变的不可访问。

       2.agent程序受monitor程序的控制处理write切换,从库切换等操作。如果monitor进程关闭了那么agent进程就起不到什么作用,它本身不能处理故障。

       3.monitor程序负责监控db服务器的状态,包括Mysql数据库、服务器是否运行、复制线程是否正常、主从延时等;它还用于控制agent程序处理故障。

       4.monitor会每隔几秒钟监控db服务器的状态,如果db服务器已经从故障变成了正常,那么monitor会自动在60s之后将其设置为online状态(默认是60s可以设为其它的值),有监控端的配置文件参数“auto_set_online”决定,群集服务器的状态有三种分别是:HARD_OFFLINE→AWAITING_RECOVERY→online

       5.默认monitor会控制mmm_agent会将writer db服务器read_only修改为OFF,其它的db服务器read_only修改为ON,所以为了严谨可以在所有的服务器的my.cnf文件中加入read_only=1由monitor控制来控制writer和read,root用户和复制用户不受read_only参数的影响

以上就是本文的全部内容,希望本文对大家学习mysql有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn