高性能MySQL(第四版):二、可靠性工程世界中的监控

定义服务水平目标

服务水平指标(SLI):SLI回答了“如何衡量客户是否满意”的问题,SLI可以是业务级别的指标,如“面向客户的API的响应时间”或最基本的“服务已启动”

服务水平目标(SLO):SLO回答了“为了确保客户满意,能允许SLI达到的最低限度是多少”的问题。SLI加上SLO构成了了解客户是否满意的基本方程式。

服务水平协议(SLA):SLA回答了“我同意的SLO会产生什么后果”的问题。SLA是可选的。

怎样才能让客户满意

目标:定义确保客户满意的最低标准

可用性承诺的9(例如99%、99.9%、99.99%)越多,就越难实现,所花费的工程时间也就越昂贵。

以上的指标与目标用于指导在“将工程时间花在新功能上”与“将时间花在可恢复性和修复问题上”之间做出决策。

在第一次定义一些SLI和SLO之后,并不意味着一成不变,随着业务的发展,将对客户体验有更深入的了解,这将推动SLI和SLO的改进。

用什么来度量

定义好的SLI和匹配的SLO是简洁地解释如何为客户提供愉快的用户体验的核心。

MySQL的三个重要主题:可用性、延迟和关键错误缺失。

监控解决方案

商业:SolarWinds数据库性能管理工具

开源:Percona监控和管理工具,称为PMM。客户端/服务器方式运行。

监控可用性

可用性:指能够无错误地相应客户的请求。

MySQL中Threads_running状态计数器可以作为可用性问题的关键指标。可以监控Thread_runningmax_connections的差距,作为监控数据点。

监控报错

每次报错是否都需要跟踪和提醒?这是要视情况而定的。

常见错误:

Lock wait timeout:可能是主节点上的行级锁争用性在不断扩大,即事务不断重试但仍然失败。这可能是无法写入的前兆。

Aborted connections:可能表明客户端和数据库实例之间的某个访问层出现了问题,不跟踪这一点会导致大量客户端重试,消耗资源。

MySQL服务器跟踪的名为Connection_errors_XXX的服务器变量集非常有用,其中XXX是不同类型的连接错误。

服务器错误“too many connections”或操作系统级别的“cannot create new thread”。这些表明,应用程序创建和打开的连接数超过了数据库服务器配置中允许的连接数,这个限制可能来自服务器的max_connections变量或者MySQL进程被允许打开的线程数。这些错误会立即转化为5XX错误返回给应用程序。

主动监控

稳态监控:用于出发随叫随到(on-all)告警的仪表盘和脚本

监控方面需要达到的平衡是,它既要具有可操作性,又要成为真正的前导指标。

磁盘空间使用率增长

伴随着Undo日志的长时间运行的事务或alter table之类的操作是导致磁盘空间快速耗尽的原因。可以跟踪的方法有:跟踪增长率,设置多个阈值。

连接数增长

当流量不断增长时,数据库服务器可以支持有限的连接池即服务器参数max_connection。

监控连接数增长时为了确保资源不会耗尽到危及数据库可用性的程度。这种风险可能以两种不同的方式出现:

  • 应用程序打开了大量未使用的连接,导致产生毫无理由的连接过多风险。迹象时连接的线程数(threads_connected)很高,而运行的线程数(threads_running)仍然很低。
  • 应用程序层正在积极使用大量的连接,并有导致数据库过载的风险。可以通过产看连接的线程数(threads_connected)和运行的线程数(threads_running)都处于高值(成百上千?)并持续增加来识别这种状态

设置连接数监控时,要用百分比而不是绝对值,threads_connected/max_connections

连接风暴:指在生产系统中,应用程序层感知到查询延迟增加,并通过打开更多到数据库层的连接进行相应的情况。这可能会导致数据库负载显著增加,因为它要处理大量涌入的新连接,这会占用执行查询请求的资源。连接风暴可能导致max_connections中的可用连接突然减少,并增加数据库可用性风险。

复制延迟

MySQL有原生的复制功能,可以将数据从源服务器发送到一个或多个副本服务器。数据从写入源服务器到在副本服务器上可读取之间的延迟被称为复制延迟。设置复制延迟告警需要谨慎。

I/O使用率

数据库工程师努力的目标之一是“尽可能多地在内存中工作,因为这样更快”。最好的方法是不要从磁盘读取太多数据,否则查询就只能等待那些宝贵的I/O周期。

iostat工具可以监控I/O等待

子增键空间

自动递增主键在默认情况下被创建为有符号整数,并且可能会耗尽键空间。

使用PMM及其Prometherus导出器(exporter),开启collect.auto_increment.colums设置。

或者使用下面查询,它可以作为指标生成器或告警来进行修改,这个查询依赖与information_schema,其中包含有关数据库实例中表的所有元数据。

以下SQL无法运行,如有懂得,请评论区告知。

SELECT
	t.TABLE_SCHEMA AS `schema`,
	t.TABLE_NAME AS `table`,
	t.AUTO_INCREMENT AS `suto_increment`,
	c.DATE_TYPE AS `pk_type`,
	(
		t.AUTO_INCREMENT / (
		CASE
				DATE_TYPE 
				WHEN 'tinyint' THEN
			IF
				( COLUMN_TYPE LIKE '%unsigned', 255, 127 ) 
				WHEN 'samllint' THEN
			IF
				( COLUMN_TYPE LIKE '%unsigned', 65535, 32767 ) 
				WHEN 'mediumint' THEN
			IF
				( COLUMN_TYPE LIKE '%unsigned', 16777215, 8388607 ) 
				WHEN 'int' THEN
			IF
				( COLUMN_TYPE LIKE '%unsigned', 4294967295, 2147483647 ) 
				WHEN 'bigint' THEN
			IF
				( COLUMN_TYPE LIKE '%unsigned', 18446744073709551615, 9223372036854775807 ) 
			END / 100 
		) 
	) AS `max_value` 
FROM
	information_schema.TABLES t
	INNER JOIN information_schema.COLUMNS c ON t.TABLE_SCHEMA = c.TABLE_SCHEMA 
	AND t.TABLE_NAME = c.TABLE_NAME 
WHERE
	t.AUTO_INCREMENT IS NOT NULL 
	AND c.COLUMN_KEY = 'PRI' 
	AND c.DATE_TYPE LIKE '%int';

创建备份/恢复时间

度量长期性能

了解业务节奏

业务节奏可能意味着峰值流量时间比“平均值”大几个数量级。

使用监控工具检查性能

对平均值说不

请始终关心峰值,这样能够在视图中保持偶尔尖刺的逼真度。

与百分位为友

百分位依赖于在给定时间范围内对数据点进行排序,并根据百分位一处最高值的数据点。

长期保留与性能之间的取舍

Paperback: 1224 pages Data: September 8, 2008 Description: The unexpected pleasure of reading books about databases is that they are often written by authors with highly organized minds. Paul DuBois and his editors at New Riders have assembled MySQL with a clarity and lucidity that inspires confidence in the subject matter: a (nearly) freely redistributable SQL-interpreting database client/server primarily geared for Unix systems but maintained for Windows platforms as well. What isn't "free" about MySQL (the application) is its server's commercial use; all clients and noncommercial server use are free. DuBois's tome isn't free either, but its list price is modest in light of its value and the value of its namesake. The volume is superbly organized into 12 chapters and 10 appendices and contains a concise table of contents and a comprehensive 50-page index. It is peppered with references to the online HTML documentation that comes with the source and binary distributions (which are available and easy to install in stable rpm and tar releases.) The first third of MySQL is an excellent instruction tool for database newbies; the second third is a detailed reference for MySQL developers; and the last third consists of clearly annotated appendices, including C, Perl (but not Python), and PHP interfaces. Perhaps as an indication of the collective will of the developers of MySQL, DuBois does not separate Windows 95/98/NT design or development specifics from its main discussions. Platform-independent design is a goal, not a reality, and users will have to rely on newsgroups and mailing lists for details. Moreover, security issues are addressed in a mere 18 pages, a large part of which is devoted to standard Unix file and network-access permissions. Next to nothing is mentioned about defense against common hacking strategies, the use of secure shell interfaces, or access encryption. Although it is nearly 800 pages in length, DuBois's book is thankfully not encyclopedic. It is a valuable précis of the MySQL database, and its easy-to-skim look and feel will make it an excellent browse for database experts who want to know what is and is not possible within MySQL, the application.
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值