本篇文章给大家介绍一下使用zabbix监控mysql的解决方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。
相关推荐:《mysql教程》
zabbix 监控 mysql
1、监控规划
在创建监控项之前要尽量考虑清楚要监控什么,怎么监控,监控数据如何存储,监控数据如何展现,如何处理报警等。要进行监控的系统规划需要对Zabbix很了解,这里只是提出监控的需求。
需求一:监控MySQL的状态,当状态发生异常,发出报警;
需求二:监控MySQL的操作,并用图表展现;
2、自定义脚本监控扩展Agent
Zabbix Server与Agent之间监控数据的采集主要是通过Zabbix Server主动向Agent询问某个Key的值,Agent会根据Key去调用相应的函数去获取这个值并返回给Server端。Zabbix 2.4.7的Agent本并没有内置MySQL的监控功能(但是Server端提供了相应的Template配置),所以我们需要使用Zabbix的User Parameters功能,为MySQL添加监控脚本。
3、授权mysql登录用户(agent端)
mysql> grant usage on *.* to zabbix@127.0.0.1 identified by '123456'; mysql> flush privileges;
4、agent端配置
存活检测
利用UserParameter参数自定义Agent Key。
对于需求一 ,我们采用mysqladmin这个工具来实现,命令如下:
# mysqladmin -h 127.0.0.1 -u zabbix -p123456 ping mysqld is alive
如果MySQL状态正常,会显示mysqld is alive,否则会提示连接不上。对于服务器端,mysqld is alive这样的句子不好理解,服务器端最好只接收1和0,1表示服务可用,0表示服务不可用。那么再改进一下这个命令,如下:
# mysqladmin -h 127.0.0.1 -u zabbix -p123456 ping |grep -c alive 1
用户名和密码放在命令中对于以后的维护不好,所以我们在/var/lib/zabbix/下创建一个包含MySQL用户名和密码的配置文件“.my.cnf”,如下:
user=zabbix host=127.0.0.1 password='123456'
有了这个文件后的命令变更为
HOME=/var/lib/zabbix/ mysqladmin ping |grep -c alive 1
做完这一步后需要做的就是,将这个监控命令添加到Zabbix Agent中,并与一个Key对应,这样Zabbox Server就能通过这个Key获取MySQL的状态了。我们使用mysql.ping作为MySQL状态的Key。
首先在去除/etc/zabbix/zabbix_agentd.conf中
“Include=/etc/zabbix_agentd.d/” 这一行的注释符。
其次,在/etc/zabbix/zabbix_agentd.d/目录下创建userparameter_mysql.conf文件。在文件中添加如下命令:
UserParameter=mysql.ping,HOME=/var/lib/zabbix mysqladmin ping | grep -c alive
使用下面的命令测试是否正常工作。
# /usr/sbin/zabbix_agentd -t mysql.ping mysql.ping [t|1]
其他性能指标
1.添加userparameter_mysql
vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf ####监控mysql性能的脚本 UserParameter=mysql.status[*],/etc/zabbix/zabbix_agentd.d/check_mysql.sh $1 #####mysql版本 UserParameter=mysql.version,mysql -V
2.check_mysql.sh
#!/bin/bash # ------------------------------------------------------------------------------- # FileName: check_mysql.sh # Revision: 1.0 # ------------------------------------------------------------------------------- # Copyright: # License: GPL # 用户名 MYSQL_USER='zabbix' # 密码 MYSQL_PWD='zabbix@123' # 主机地址/IP MYSQL_HOST='ip' # 端口 MYSQL_PORT='3306' # 数据连接 MYSQL_CONN="/usr/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}" # 参数是否正确 if [ $# -ne "1" ];then echo "arg error!" fi # 获取数据 case $1 in Uptime) result=`${MYSQL_CONN} status 2>/dev/null |cut -f2 -d":"|cut -f1 -d"T"` echo $result ;; Com_update) result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_update"|cut -d"|" -f3` echo $result ;; Slow_queries) result=`${MYSQL_CONN} status 2>/dev/null |cut -f5 -d":"|cut -f1 -d"O"` echo $result ;; Com_select) result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_select"|cut -d"|" -f3` echo $result ;; Com_rollback) result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_rollback"|cut -d"|" -f3` echo $result ;; Questions) result=`${MYSQL_CONN} status 2>/dev/null |cut -f4 -d":"|cut -f1 -d"S"` echo $result ;; Com_insert) result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_insert"|cut -d"|" -f3` echo $result ;; Com_delete) result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_delete"|cut -d"|" -f3` echo $result ;; Com_commit) result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_commit"|cut -d"|" -f3` echo $result ;; Bytes_sent) result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Bytes_sent" |cut -d"|" -f3` echo $result ;; Bytes_received) result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Bytes_received" |cut -d"|" -f3` echo $result ;; Com_begin) result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_begin"|cut -d"|" -f3` echo $result ;; *) echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)" ;; esac
3.授权:
chmod +x /etc/zabbix/zabbix_agentd.d/check_mysql.sh Chown zabbix.zabbix /etc/zabbix/zabbix_agentd.d/check_mysql.sh
4. zabbix_agent上测试:
zabbix_agentd -t mysql.ping
5.Zabbix_server测试
zabbix_get -s ip -P 端口 -k mysql.ping
5、在web端进行配置
**创建主机 **
关联模板
创建监控项
创建图形
查看监控图像
其他监控项以此配置完成
6、zabbix自带mysql监控项
version:数据库版本 key_buffer_size:myisam的索引buffer大小 sort_buffer_size:会话的排序空间(每个线程会申请一个) join_buffer_size:这是为链接操作分配的最小缓存大小,这些连接使用普通索引扫描、范围扫描、或者连接不适用索引 max_connections:最大允许同时连接的数量 max_connect_errors:允许一个主机最多的错误链接次数,如果超过了就会拒绝之后链接(默认100)。可以使用flush hosts命令去解除拒绝 open_files_limits:操作系统允许mysql打开的文件数量,可以通过opened_tables状态确定是否需要增大table_open_cache,如果opened_tables比较大且一直还在增大说明需要增大table_open_cache max-heap_tables_size:建立的内存表的最大大小(默认16M)这个参数和tmp_table_size一起限制内部临时表的最大值(取这两个参数的小的一个),如果超过限制,则表会变为innodb或myisam引擎,(5.7.5之前是默认是myisam,5.7.6开始是innodb,可以通过internal_tmp_disk_storage_engine参数调整)。 max_allowed_packet:一个包的最大大小 ##########GET INNODB INFO #INNODB variables innodb_version: innodb_buffer_pool_instances:将innodb缓冲池分为指定的多个(默认为1) innodb_buffer_pool_size:innodb缓冲池大小、5.7.5引入了innodb_buffer_pool_chunk_size, innodb_doublewrite:是否开启doublewrite(默认开启) innodb_read_io_threads:IO读线程的数量 innodb_write_io_threads:IO写线程的数量 ########innodb status innodb_buffer_pool_pages_total:innodb缓冲池页的数量、大小等于innodb_buffer_pool_size/(16*1024) innodb_buffer_pool_pages_data:innodb缓冲池中包含数据的页的数量 ########## GET MYSQL HITRATE 1、查询缓存命中率 如果Qcache_hits+Com_select<>0则为 Qcache_hits/(Qcache_hits+Com_select),否则为0 2、线程缓存命中率 如果Connections<>0,则为1-Threads_created/Connections,否则为0 3、myisam键缓存命中率 如果Key_read_requests<>0,则为1-Key_reads/Key_read_requests,否则为0 4、myisam键缓存写命中率 如果Key_write_requests<>0,则为1-Key_writes/Key_write_requests,否则为0 5、键块使用率 如果Key_blocks_used+Key_blocks_unused<>0,则Key_blocks_used/(Key_blocks_used+Key_blocks_unused),否则为0 6、创建磁盘存储的临时表比率 如果Created_tmp_disk_tables+Created_tmp_tables<>0,则Created_tmp_disk_tables/(Created_tmp_disk_tables+Created_tmp_tables),否则为0 7、连接使用率 如果max_connections<>0,则threads_connected/max_connections,否则为0 8、打开文件比率 如果open_files_limit<>0,则open_files/open_files_limit,否则为0 9、表缓存使用率 如果table_open_cache<>0,则open_tables/table_open_cache,否则为0
The above is the detailed content of How to use zabbix to monitor mysql. For more information, please follow other related articles on the PHP Chinese website!

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor