MySQL-MMM架構部署 MySQL-MMM架構使用 MySQL性能調優
1 MySQL-MMM架構部署
1.1 問題mysql
本案例要求熟悉實現MySQL-MMM的架構部署,主要包括如下任務:sql
安裝依賴包 安裝軟件包 配置MySQL-MMM
1.2 方案數據庫
使用5臺RHEL 6虛擬機,如圖-2所示。其中192.168.4.十、192.168.4.11做爲MySQL雙主服務器,192.168.4.十二、192.168.4.13做爲主服務器的從服務器,192.168.4.100做爲MySQL-MMM架構中管理監控服務器,實現監控MySQL主從服務器的工做狀態及決定故障節點的移除或恢復工做,架構搭建完成後使用客戶機192.168.4.120進行訪問,客戶機須要安裝MySQL-client軟件包。
圖-2
1.3 步驟vim
實現此案例須要按照以下步驟進行。緩存
步驟一:安裝MySQL-MMM服務器
1)安裝依賴關係(MySQL集羣內5臺服務器master1,master2,slave1,slave2,monitor)均需安裝架構
[root@master1 ~]# 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 類ide
[root@mysql-master1 ~]# 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類工具
[root@mysql-master1 ~]# tar -zxvf Algorithm-Diff-1.1902.tar.gz //解壓安裝包 .. .. [root@mysql-master1 ~]# cd Algorithm-Diff-1.1902 //切換到安裝目錄 [root@mysql-master1 Algorithm-Diff-1.1902]# perl Makefile.PL //生成makefile文件 Checking if your kit is complete... Looks good Writing Makefile for Algorithm::Diff [root@mysql-master1 Algorithm-Diff-1.1902]# make && make install //編譯,編譯安裝 .. .. [root@mysql-master1 Algorithm-Diff-1.1902]# cd //切換到軟件包目錄 [root@mysql-master1 ~]#
安裝Proc-Daemon類
[root@mysql-master1 ~]# tar -zxvf Proc-Daemon-0.03.tar.gz //解壓安裝包 .. .. [root@mysql-master1 ~]# cd Proc-Daemon-0.03 //切換到安裝目錄 [root@mysql-master1 Proc-Daemon-0.03]# perl Makefile.PL //生成makefile文件 Checking if your kit is complete... Looks good Writing Makefile for Proc::Daemon [root@mysql-master1 Proc-Daemon-0.03]# make && make install //編譯,編譯安裝 .. .. [root@mysql-master1 Proc-Daemon-0.03]# cd //切換到軟件包目錄 [root@mysql-master1 ~]#
安裝Net-ARP虛擬IP分配工具:
[root@mysql-master1 ~]# gunzip Net-ARP-1.0.8.tgz //使用gunzip解壓tgz格式的安裝包 [root@mysql-master1 ~]# tar xvf Net-ARP-1.0.8.tar //解壓tar安裝包 .. .. [root@mysql-master1 ~]# cd Net-ARP-1.0.8 //切換到安裝目錄 [root@mysql-master1 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 [root@mysql-master1 Net-ARP-1.0.8]# make && make install //編譯,編譯安裝 .. .. [root@mysql-master1 Net-ARP-1.0.8]# cd //切換到軟件包目錄 [root@mysql-master1 ~]#
安裝Mysql-MMM軟件包:
[root@mysql-master1 ~]# tar xvf mysql-mmm-2.2.1.tar.gz //解壓安裝包 .. .. [root@mysql-master1 ~]# cd mysql-mmm-2.2.1 //切換到安裝目錄 [root@mysql-master1 mysql-mmm-2.2.1]# make && make install //編譯,編譯安裝 .. .. [root@mysql-master1 mysql-mmm-2.2.1]#
步驟二:修改配置文件
1)修改公共配置文件
本案例中MySQL集羣的5臺服務器(master一、master二、slave一、slave二、monitor)都須要配置,能夠先配好一臺後使用scp複製。
[root@master1 ~]# 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> [root@master1 ~]#
2)修改管理主機配置文件(monitor主機配置)
[root@monitor ~]# 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 [root@monitor ~]#
3)修改客戶端配置文件
master1配置
[root@master1 ~]# cat /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf this master1
master2配置
[root@master2 ~]# cat /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf this master2
slave1配置
[root@slave1 ~]# cat /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf this slave1
slave2配置
[root@slave2 ~]# 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操做:
[root@master1 ~]# /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操做:
[root@master2 ~]# /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操做:
[root@master2 ~]# /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操做:
[root@slave2 ~]# /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主機操做:
[root@monitor ~]# /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服務器上執行。
查看當前集羣中各服務器狀態:
[root@monitor ~]# 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:
[root@monitor ~]# mmm_control set_online master1 OK: State of 'master1' changed to ONLINE. Now you can wait some time and check its new roles! [root@monitor ~]# mmm_control set_online master2 OK: State of 'master2' changed to ONLINE. Now you can wait some time and check its new roles! [root@monitor ~]# mmm_control set_online slave1 OK: State of 'slave1' changed to ONLINE. Now you can wait some time and check its new roles! [root@monitor ~]# mmm_control set_online slave2 OK: State of 'slave2' changed to ONLINE. Now you can wait some time and check its new roles! [root@monitor ~]#
再次查看當前集羣中各服務器狀態:
[root@monitor ~]# 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) [root@monitor ~]#
步驟三:測試MySQL-MMM架構
1)客戶機安裝MySQL-client軟件包
[root@client ~]# tar xvf MySQL-5.6.15-1.el6.x86_64.rpm-bundle.tar .. .. [root@client ~]# rpm -ivh MySQL-client-5.6.15-1.el6.x86_64.rpm .. ..
2)MySQL-MMM虛擬IP訪問測試
[root@client ~]# 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 | +--------------------+ [root@client ~]# [root@client ~]# 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 | +--------------------+ [root@client ~]# [root@client ~]# 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 | +--------------------+ [root@client ~]#
3)主數據庫宕機測試
[root@master1 ~]# service mysql stop //中止master1上服務 Shutting down MySQL.... [肯定] [root@master1 ~]# [root@monitor ~]# 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) [root@monitor ~]# [root@client ~]# 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 | +--------------------+ [root@client ~]#
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配置文件,啓用慢查詢
[root@dbsvr1 ~]# 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 //記錄未使用索引的查詢 [root@dbsvr1 ~]# service mysql restart Shutting down MySQL..... [肯定] Starting MySQL.... [肯定]
2)查看慢查詢日誌(mysqldumpslow工具)
[root@dbsvr1 ~]# 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), 0users@0hosts .. ..
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 STATUS\G *************************** 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 VARIABLES\G *************************** 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)