MySQL-MMM架构部署(有图)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL-MMM架构部署(有图)相关的知识,希望对你有一定的参考价值。

mysql-MMM架构部署
MySQL-MMM架构使用
MySQL性能调优

1 MySQL-MMM架构部署
1.1 问题

本案例要求熟悉实现MySQL-MMM的架构部署,主要包括以下任务:

安装依赖包
安装软件包
配置MySQL-MMM

1.2 方案

使用5台RHEL 6虚拟机,如图-2所示。其中192.168.4.10、192.168.4.11作为MySQL双主服务器,192.168.4.12、192.168.4.13作为主服务器的从服务器,192.168.4.100作为MySQL-MMM架构中管理监控服务器,实现监控MySQL主从服务器的工作状态及决定故障节点的移除或恢复工作,架构搭建完成后使用客户机192.168.4.120进行访问,客户机需要安装MySQL-client软件包。
技术分享图片
图-2
1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:安装MySQL-MMM

1)安装依赖关系(MySQL集群内5台服务器master1,master2,slave1,slave2,monitor)均需安装

[[email protected] ~]# yum -y install gcc* perl-Date-Manip  perl-Date-Manip  perl-Date-Manip perl-XML-DOM-XPath perl-XML-Parser perl-XML-RegExp rrdtool perl-Class-Singleton perl perl-DBD-MySQL perl-Params-Validate perl-MailTools perl-Time-HiRes
.. ..

2)安装MySQL-MMM软件依赖包(MySQL集群内5台服务器master1,master2,slave1,slave2,monitor)均需安装,软件包讲师提供

安装安装Log-Log4perl 类

[[email protected] ~]# rpm -ivh perl-Log-Log4perl-1.26-1.el6.rf.noarch.rpm
warning: perl-Log-Log4perl-1.26-1.el6.rf.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 6b8d79e6: NOKEY
Preparing...                ######################### [100%]
1:perl-Log-Log4perl        ########################  [100%]

安装Algorithm-Diff类

[[email protected] ~]# tar -zxvf Algorithm-Diff-1.1902.tar.gz  //解压安装包
.. .. 
[[email protected] ~]# cd Algorithm-Diff-1.1902                //切换到安装目录
[[email protected] Algorithm-Diff-1.1902]# perl  Makefile.PL    //生成makefile文件
Checking if your kit is complete...
Looks good
Writing Makefile for Algorithm::Diff
[[email protected] Algorithm-Diff-1.1902]# make && make install        //编译,编译安装
.. ..
[[email protected] Algorithm-Diff-1.1902]# cd                //切换到软件包目录
[[email protected] ~]#

安装Proc-Daemon类

[[email protected] ~]# tar -zxvf Proc-Daemon-0.03.tar.gz    //解压安装包
.. ..
[[email protected] ~]# cd Proc-Daemon-0.03                    //切换到安装目录
[[email protected] Proc-Daemon-0.03]# perl    Makefile.PL    //生成makefile文件
Checking if your kit is complete...
Looks good
Writing Makefile for Proc::Daemon
[[email protected] Proc-Daemon-0.03]# make && make install    //编译,编译安装
.. ..
[[email protected] Proc-Daemon-0.03]# cd                    //切换到软件包目录
[[email protected] ~]#

安装Net-ARP虚拟IP分配工具:

[[email protected] ~]# gunzip Net-ARP-1.0.8.tgz    //使用gunzip解压tgz格式的安装包
[[email protected] ~]# tar xvf Net-ARP-1.0.8.tar            //解压tar安装包
.. ..
[[email protected] ~]# cd Net-ARP-1.0.8                    //切换到安装目录
[[email protected] Net-ARP-1.0.8]# perl Makefile.PL        //生成makefile文件
Module Net::Pcap is required for make test!
Checking if your kit is complete...
Looks good
Writing Makefile for Net::ARP
[[email protected] Net-ARP-1.0.8]# make && make install    //编译,编译安装
.. ..
[[email protected] Net-ARP-1.0.8]# cd                        //切换到软件包目录
[[email protected] ~]#

安装Mysql-MMM软件包:

[[email protected] ~]# tar xvf mysql-mmm-2.2.1.tar.gz        //解压安装包
.. ..
[[email protected] ~]# cd mysql-mmm-2.2.1                    //切换到安装目录
[[email protected] mysql-mmm-2.2.1]# make && make install    //编译,编译安装
.. ..
[[email protected] mysql-mmm-2.2.1]#

步骤二:修改配置文件

1)修改公共配置文件

本案例中MySQL集群的5台服务器(master1、master2、slave1、slave2、monitor)都需要配置,可以先配好一台后使用scp复制。

[[email protected] ~]# vim  /etc/mysql-mmm/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    pwd123            //设置主从同步用户密码
    agent_user            agent                //mmm-agent控制数据库用户
    agent_password        agent                //mmm-agent控制数据库用户密码
</host>
<host master1>                            //设置第一个主服务器
    ip                    192.168.4.10            //master1 IP 地址
    mode                    master
    peer                    master2                //指定另外一台主服务器
</host>
<host master2>                            //指定另外一台主服务器
    ip                    192.168.4.11
    mode                    master
    peer                    master1
</host>
<host slave1>                                //设置第一台从服务器
    ip                    192.168.4.12            //slave1 IP 地址
    mode                    slave                //本段落配置的是slave服务器
</host>
<host slave2>
    ip                    192.168.4.13
    mode                    slave
</host>
<role writer>                                //设置写入服务器工作模式
    hosts                master1,master2        //提供写的主服务器
    ips                    192.168.4.200        //设置VIP地址
    mode                    exclusive            //排他模式
</role>
<role reader>                                //设置读取服务器工作模式
    hosts                slave1,slave2        //提供读的服务器信息
    ips                    192.168.4.201,192.168.4.202    //多个虚拟IP
    mode                    balanced                        //均衡模式
</role>
[[email protected] ~]#

2)修改管理主机配置文件(monitor主机配置)

[[email protected] ~]# vim /etc/mysql-mmm/mmm_mon.conf 
include mmm_common.conf
<monitor>
    ip                        192.168.4.100        //设置管理主机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.10,192.168.4.11,192.168.4.12,192.168.4.13
                                                //设置被监控数据库
</monitor>
<host default>
    monitor_user            monitor                    //监控数据库MySQL用户    monitor_password        monitor                    //监控数据库MySQL用户密码
</host>
debug 0
[[email protected] ~]#

3)修改客户端配置文件

master1配置

[[email protected] ~]# cat /etc/mysql-mmm/mmm_agent.conf 
include mmm_common.conf
this master1

master2配置

[[email protected] ~]# cat /etc/mysql-mmm/mmm_agent.conf 
include mmm_common.conf
this master2

slave1配置

[[email protected] ~]# cat /etc/mysql-mmm/mmm_agent.conf 
include mmm_common.conf
this slave1

slave2配置

[[email protected] ~]# cat /etc/mysql-mmm/mmm_agent.conf 
include mmm_common.conf
this slave2

2 MySQL-MMM架构使用
2.1 问题

本案例要求基于普通版的MySQL服务器改造MMM架构,完成以下任务操作:

启动MMM集群架构
设置集群中服务器为online状态

2.2 方案

MySQL-MMM架构部署完成后需要启动,数据库端启动mmm-agent进程,管理端启动mmm-monitor进程,启动完成后设置所有数据库主机状态为online。
2.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:启动MMM集群架构

1)启动mmm-agent进程

master1操作:

[[email protected] ~]# /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

master2操作:

[[email protected] ~]# /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

slave1操作:

[[email protected] ~]# /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

slave2操作:

[[email protected] ~]# /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

2)启动mmm-monitor进程

monitor主机操作:

[[email protected] ~]# /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

步骤二:设置集群中服务器为online状态

控制命令只能在管理端monitor服务器上执行。

查看当前集群中各服务器状态:

[[email protected] ~]# mmm_control show
  master1(192.168.4.10) master/AWAITING_RECOVERY. Roles: 
  master2(192.168.4.11) master/AWAITING_RECOVERY. Roles: 
  slave1(192.168.4.12) slave/AWAITING_RECOVERY. Roles: 
  slave2(192.168.4.13) slave/AWAITING_RECOVERY. Roles: 

设置4台数据库主机状态为online:

[[email protected] ~]# mmm_control set_online master1
OK: State of ‘master1‘ changed to ONLINE. Now you can wait some time and check its new roles!
[[email protected] ~]# mmm_control set_online master2
OK: State of ‘master2‘ changed to ONLINE. Now you can wait some time and check its new roles!
[[email protected] ~]# mmm_control set_online slave1
OK: State of ‘slave1‘ changed to ONLINE. Now you can wait some time and check its new roles!
[[email protected] ~]# mmm_control set_online slave2
OK: State of ‘slave2‘ changed to ONLINE. Now you can wait some time and check its new roles!
[[email protected] ~]#

再次查看当前集群中各服务器状态:

[[email protected] ~]# mmm_control show
  master1(192.168.4.10) master/ONLINE. Roles: writer(192.168.4.200)
  master2(192.168.4.11) master/ONLINE. Roles: 
  slave1(192.168.4.12) slave/ONLINE. Roles: reader(192.168.4.201)
  slave2(192.168.4.13) slave/ONLINE. Roles: reader(192.168.4.202)
[[email protected] ~]#

步骤三:测试MySQL-MMM架构

1)客户机安装MySQL-client软件包

[[email protected] ~]# tar xvf MySQL-5.6.15-1.el6.x86_64.rpm-bundle.tar
.. ..
[[email protected] ~]# rpm -ivh MySQL-client-5.6.15-1.el6.x86_64.rpm
.. ..

2)MySQL-MMM虚拟IP访问测试

[[email protected] ~]# mysql -h192.168.4.200 -uroot -ppwd123 -e "show databases"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| tarena             |
| test               |
+--------------------+
[[email protected] ~]#
[[email protected] ~]# mysql -h192.168.4.200 -uroot -ppwd123 -e "show databases"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| tarena             |
| test               |
+--------------------+
[[email protected] ~]#
[[email protected] ~]# mysql -h192.168.4.202 -uroot -ppwd123 -e "show databases"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| tarena             |
| test               |
+--------------------+
[[email protected] ~]#

3)主数据库宕机测试

[[email protected] ~]# service mysql stop                    //停止master1上服务
Shutting down MySQL....                                    [确定]
[[email protected] ~]#
[[email protected] ~]# mmm_control show                        //查看集群内服务器状态

通过输出信息可以看到虚拟IP从master1切换到master2:

  master1(192.168.4.10) master/HARD_OFFLINE. Roles: 
  master2(192.168.4.11) master/ONLINE. Roles: writer(192.168.4.200)
  slave1(192.168.4.12) slave/ONLINE. Roles: reader(192.168.4.201)
  slave2(192.168.4.13) slave/ONLINE. Roles: reader(192.168.4.202)
[[email protected] ~]#
[[email protected] ~]# mysql -h192.168.4.200 -uroot -ppwd123 -e "show databases"    //访问虚拟IP测试
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| tarena             |
| test               |
+--------------------+
[[email protected] ~]#

3 MySQL性能调优
3.1 问题

基于一台普通版的MySQL服务器,执行下列操作:

练习my.cnf配置相关选项
启用慢查询日志
查看各种系统变量、状态变量

3.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:MySQL并发及连接控制

max_connections对应并发客户端连接的数量,增加该值会增加 mysqld 要求的文件描述符的数量。若这个数值太小,可能会经常出现“too many connections”错误。比如 默认的数值是151,可以将其改为1024。

1)查看当前已建立的连接数

mysql> FLUSH STATUS;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW GLOBAL STATUS LIKE ‘max_used_connections‘;
+----------------------+-------+
| Variable_name        | Value |
+----------------------+-------+
| Max_used_connections | 5     |
+----------------------+-------+
1 row in set (0.05 sec)

2)查看当前的最大连接数限制

mysql> SHOW VARIABLES LIKE ‘max_connections‘;
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 151   |
+-----------------+-------+
1 row in set (0.00 sec)

步骤二:MySQL缓存参数控制

当 Key_reads / Key_read_requests 较低时,可适当加大key_buffer_size的缓存值,以提高性能。而增大sort_buffer_size的值,可以显著提高ORDER和GROUP的响应速度。

1)查看key_read相关数值

mysql> SHOW GLOBAL STATUS LIKE ‘key_read%‘;
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| Key_read_requests | 0     |
| Key_reads         | 0     |
+-------------------+-------+
2 rows in set (0.00 sec)

2)查看当前的key_buffer_size缓存大小

mysql> SHOW VARIABLES LIKE ‘key_buffer_size‘;
+-----------------+---------+
| Variable_name   | Value   |
+-----------------+---------+
| key_buffer_size | 8388608 |
+-----------------+---------+
1 row in set (0.03 sec)

3)查看当前的sort_buffer_size大小

mysql> SHOW VARIABLES LIKE ‘sort_buffer_size‘;
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| sort_buffer_size | 262144 |
+------------------+--------+
1 row in set (0.00 sec)

4)查看检索表记录时的读取缓存大小

缓存值read_buffer_size和read_rnd_buffer_size会影响SQL查询的响应速度:

mysql> SHOW VARIABLES LIKE ‘read_%_size‘;
+----------------------+--------+
| Variable_name        | Value  |
+----------------------+--------+
| read_buffer_size     | 131072 |
| read_rnd_buffer_size | 262144 |
+----------------------+--------+
2 rows in set (0.00 sec)

步骤三:MySQL线程重用和开表控制

分析“已打开表的数量/当前可缓存表的数量”,比值不超过95%就基本正常。

1)查看当前已打开、一共打开过多少个表

mysql> SHOW GLOBAL STATUS LIKE ‘open%tables‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Open_tables   | 23     |
| Opened_tables | 72    |
+---------------+-------+
2 rows in set (0.01 sec)

2)查看当前可缓存多少个打开的表

mysql> SHOW VARIABLES LIKE ‘table_open_cache‘;
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| table_open_cache | 2000  |
+------------------+-------+
1 row in set (0.00 sec)

步骤四:MySQL调整示例:记录慢查询

1)调整my.cnf配置文件,启用慢查询

[[email protected] ~]# vim  /etc/my.cnf
[mysqld]
.. ..
slow_query_log=1                              //启用慢查询
slow_query_log_file=mysql-slow.log          //制定慢查询日志文件
long_query_time=5                              //查询耗时超过5秒才记录
log_queries_not_using_indexes=1              //记录未使用索引的查询
[[email protected] ~]# service  mysql  restart
Shutting down MySQL.....                                   [确定]
Starting MySQL....                                         [确定]

2)查看慢查询日志(mysqldumpslow工具)

[[email protected] ~]# mysqldumpslow  /var/lib/mysql/mysql-slow.log
Reading mysql slow query log from /var/lib/mysql/mysql-slow.log
Count: 1  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), [email protected]
.. ..

3)了解与查询相关的缓存选项

查看当前的查询缓存大小:

mysql> SHOW VARIABLES LIKE ‘query_cache%‘;
+------------------------------+---------+
| Variable_name                | Value   |
+------------------------------+---------+
| query_cache_limit            | 1048576 |          //超过此大小则不再缓存
| query_cache_min_res_unit     | 4096    |
| query_cache_size             | 1048576 |          //缓存空间的大小
| query_cache_type             | OFF     |
| query_cache_wlock_invalidate | OFF     |
+------------------------------+---------+
5 rows in set (0.00 sec)

查看当前的查询缓存统计数据:

mysql> SHOW GLOBAL STATUS LIKE ‘qcache%‘;
+-------------------------+---------+
| Variable_name           | Value   |
+-------------------------+---------+
| Qcache_free_blocks      | 1       |
| Qcache_free_memory      | 1031368 |              //缓存中的空闲内存
| Qcache_hits             | 0       |
| Qcache_inserts          | 0       |
| Qcache_lowmem_prunes    | 0       |
| Qcache_not_cached       | 100       |              //不适合缓存的数量
| Qcache_queries_in_cache | 0       |
| Qcache_total_blocks     | 1       |
+-------------------------+---------+
8 rows in set (0.00 sec)

步骤五:关于MySQL状态和相关变量的查看

1)查看服务器的相关状态值(运行中动态变化)

使用SHOW GLOBAL STATUS语句,可结合LIKE条件做模糊过滤。

默认有400多个状态值:

mysql> SHOW GLOBAL STATUSG
*************************** 1. row ***************************
Variable_name: Aborted_clients
        Value: 0
*************************** 2. row ***************************
Variable_name: Aborted_connects
        Value: 0
*************************** 3. row ***************************
Variable_name: Binlog_cache_disk_use
        Value: 0
*************************** 4. row ***************************
Variable_name: Binlog_cache_use
        Value: 0
*************************** 5. row ***************************
Variable_name: Binlog_stmt_cache_disk_use
        Value: 0
.. ..                                              //省略中间的大量状态值
.. ..
*************************** 435. row ***************************
Variable_name: Threads_connected
        Value: 1
*************************** 436. row ***************************
Variable_name: Threads_created
        Value: 1
*************************** 437. row ***************************
Variable_name: Threads_running
        Value: 1
*************************** 438. row ***************************
Variable_name: Uptime
        Value: 5322
*************************** 439. row ***************************
Variable_name: Uptime_since_flush_status
        Value: 2283
439 rows in set (0.00 sec)

2)查看服务器的运行选项(一般为静态限制,可通过my.cnf文件配置,或SET修改)

使用SHOW VARIABLES语句,也可结合LIKE条件做模糊过滤。

默认也有400多个(接近500个)配置选项:

mysql> SHOW VARIABLESG
*************************** 1. row ***************************
Variable_name: auto_increment_increment
        Value: 1
*************************** 2. row ***************************
Variable_name: auto_increment_offset
        Value: 1
*************************** 3. row ***************************
Variable_name: autocommit
        Value: ON
*************************** 4. row ***************************
Variable_name: automatic_sp_privileges
        Value: ON
*************************** 5. row ***************************
Variable_name: back_log
        Value: 80
.. ..                                              //省略中间的大量状态值
.. ..
*************************** 486. row ***************************
Variable_name: version_comment
        Value: MySQL Cluster Community Server (GPL)
*************************** 487. row ***************************
Variable_name: version_compile_machine
        Value: x86_64
*************************** 488. row ***************************
Variable_name: version_compile_os
        Value: Linux
*************************** 489. row ***************************
Variable_name: wait_timeout
        Value: 28800
*************************** 490. row ***************************
Variable_name: warning_count
        Value: 0
490 rows in set (0.01 sec)

以上是关于MySQL-MMM架构部署(有图)的主要内容,如果未能解决你的问题,请参考以下文章

Database基础:部署集群基础环境MySQL-MMM架构部署MySQL-MMM架构使用

mysql5.6部署集群基础环境

MYSQL-MMM主从同步部署mysql高用集群

mysql-mmm 架构详细教程

Mysql-mmm集群部署

DBA成长之路---MySQL-MMM集群部署