1. performance schema:深度性能洞察
performance schema 架构与原理
performance schema 是 mysql 内置的性能监控系统,通过内存表的形式实时收集数据库运行时的各种性能指标。
核心特性:
- 零存储i/o开销:所有数据存储在内存中
- 细粒度监控:支持线程、语句、阶段、等待事件等多个维度
- 实时数据:无需等待即可查看当前性能状态
- 低性能影响:专门优化的数据收集机制
-- 检查 performance schema 状态 show variables like 'performance_schema'; select * from performance_schema.setup_instruments where enabled='yes' limit 10;
关键应用场景
查询性能分析:
-- 查看当前正在运行的查询 select * from performance_schema.threads where processlist_id is not null; -- 分析查询执行详情 select * from performance_schema.events_statements_current where thread_id = (select thread_id from performance_schema.threads where processlist_id = connection_id());
i/o 等待统计:
-- 查看文件i/o等待情况 select * from performance_schema.file_summary_by_event_name where event_name like 'wait/io/file/%' order by sum_timer_wait desc; -- 分析表i/o性能 select * from performance_schema.table_io_waits_summary_by_table where object_schema = 'your_database';
锁等待分析:
-- 查看当前锁等待情况 select * from performance_schema.data_lock_waits; -- 分析锁统计信息 select * from performance_schema.metadata_locks;
2. sys schema:性能数据的友好界面
sys schema 架构设计
sys schema 是基于 performance schema 构建的视图层,将复杂的性能数据转换为易于理解的格式。
核心优势:
- 简化查询:预定义的视图减少复杂sql编写
- 标准化报告:提供常用的性能分析报告
- 存储过程支持:内置诊断和调优存储过程
- 开发者友好:直观的列名和数据结构
-- 查看 sys schema 中的所有视图 select table_name, table_comment from information_schema.tables where table_schema = 'sys' and table_type = 'view';
实用视图解析
会话性能分析:
-- 查看当前会话性能统计 select * from sys.session where conn_id = connection_id(); -- 查看所有活动会话 select * from sys.processlist where command != 'sleep'; -- 会话i/o统计 select * from sys.io_global_by_file_by_bytes limit 10;
内存使用分析:
-- 查看内存分配情况 select * from sys.memory_global_total; select * from sys.memory_by_thread_by_current_bytes; -- 缓冲池使用情况 select * from sys.innodb_buffer_stats_by_schema;
sql 性能分析:
-- 查看慢sql统计 select * from sys.statements_with_full_table_scans; select * from sys.statements_with_sorting; select * from sys.statements_with_temp_tables; -- 最消耗资源的查询 select * from sys.statement_analysis order by avg_latency desc limit 10;
3. mysql enterprise audit:企业级安全审计
审计功能架构
mysql 企业版审计插件提供基于策略的安全审计功能,满足合规性要求。
核心特性:
- 策略驱动:灵活的审计规则配置
- 细粒度控制:支持用户、数据库、操作类型等多维度过滤
- 性能优化:最小化对数据库性能的影响
- 标准化输出:兼容行业标准的审计日志格式
-- 检查审计插件状态 select plugin_name, plugin_status from information_schema.plugins where plugin_name like '%audit%'; -- 查看审计配置 show variables like 'audit%';
审计策略配置
基于规则的过滤:
-- 安装审计过滤器(企业版功能) -- 引用文档中提到的脚本:audit_log_filter_linux_install.sql -- 此脚本配置基于规则的mysql审计功能 -- 创建审计过滤器 select audit_log_filter_set_filter('log_all', '{ "filter": { "class": { "name": "general", "event": { "name": "status", "log": true } } } }'); -- 将过滤器分配给用户 select audit_log_filter_set_user('%', 'log_all');
审计日志管理:
-- 查看审计日志状态 select * from mysql.audit_log_filter; select * from mysql.audit_log_user; -- 旋转审计日志 set global audit_log_flush = on;
4. mysql enterprise monitor:全面的监控解决方案
监控架构与功能
mysql enterprise monitor 提供企业级的数据库监控和管理功能。
核心功能模块:
持续监控能力:
-- 监控复制状态 show slave status; show master status; -- 监控innodb状态 show engine innodb status; -- 监控锁等待 select * from sys.innodb_lock_waits;
自动预警系统:
- 性能阈值告警
- 资源使用告警
- 复制故障告警
- 安全事件告警
查询分析功能:
-- 使用enterprise monitor的查询分析特性
-- 可视化慢查询分析
-- 实时性能图表
-- 历史趋势分析
-- 用户权限审计 select * from mysql.user; select * from information_schema.user_privileges; -- 安全配置检查 show variables like 'validate_password%';
5. 进程监控与会话管理
show processlist 深度解析
show processlist
是诊断数据库性能问题的关键工具。
完整输出列说明:
-- 查看完整进程列表 show full processlist; -- 等价的信息schema查询 select * from information_schema.processlist;
各列详细含义:
id:连接标识符,用于kill命令
-- 终止特定连接 kill 12345;
user:执行语句的mysql用户
-- 按用户分组统计连接数 select user, count(*) as connection_count from information_schema.processlist group by user;
host:客户端连接来源
-- 分析连接来源分布 select substring_index(host, ':', 1) as client_host, count(*) from information_schema.processlist group by client_host;
db:当前使用的数据库
-- 查看各数据库的连接分布 select db, count(*) as connections from information_schema.processlist where db is not null group by db;
command:线程执行的命令类型
-- 分析命令类型分布 select command, count(*) as count from information_schema.processlist group by command order by count desc;
time:当前状态持续时间(秒)
-- 查找长时间运行的查询 select * from information_schema.processlist where time > 60 order by time desc;
state:线程状态信息
-- 分析线程状态 select state, count(*) as count from information_schema.processlist where state is not null group by state order by count desc;
info:正在执行的sql语句(前100字符)
-- 查看正在执行的查询 select id, user, host, db, time, state, left(info, 50) as query_preview from information_schema.processlist where info is not null and command != 'sleep';
6. 用户账户管理:安全基础
用户账户存储架构
mysql 用户账户信息存储在 mysql.user
系统表中,采用集中式的账户管理。
账户信息结构:
-- 查看用户账户基本信息 select user, host, authentication_string, account_locked, password_expired from mysql.user; -- 查看用户权限详情 show grants for 'username'@'host';
通配符主机名安全风险
风险分析:
-- 查找使用通配符的主机名(安全风险) select user, host from mysql.user where host like '%\%' or host = '%'; -- 安全的用户定义示例 create user 'app_user'@'192.168.1.%' identified by 'secure_password'; create user 'readonly_user'@'10.0.0.100' identified by 'secure_password';
安全建议:
- 避免使用
'%'
作为主机名 - 使用ip段或具体主机名
- 定期审计用户权限
- 实施最小权限原则
7. 角色管理:权限集成的现代化方案
角色概念与实现
角色是权限的集合,可以分配给多个用户,简化权限管理。
角色创建与管理:
-- 创建角色 create role 'read_only', 'write_only', 'admin_role'; -- 为角色分配权限 grant select on database.* to 'read_only'; grant insert, update, delete on database.* to 'write_only'; grant all privileges on database.* to 'admin_role'; -- 将角色分配给用户 grant 'read_only' to 'app_user'@'localhost'; grant 'admin_role' to 'dba_user'@'localhost';
角色激活与使用:
-- 查看当前角色 select current_role(); -- 激活角色 set role 'read_only'; -- 设置默认角色 set default role 'read_only' to 'app_user'@'localhost';
角色与账户的转换
文档验证:角色本质上是被锁定的账户,可以转换为普通账户。
-- 创建角色(默认被锁定) create role 'r_admin'; -- 验证角色状态 select user, host, account_locked from mysql.user where user = 'r_admin'; -- 将角色转换为可登录账户 alter user 'r_admin' identified by 'secure_password' account unlock; -- 验证转换结果 select user, host, account_locked from mysql.user where user = 'r_admin';
8. 系统权限管理:精细化控制
关键系统权限解析
file 权限:
-- 授予file权限(谨慎使用) grant file on *.* to 'backup_user'@'localhost'; -- file权限的使用场景 -- 1. select ... into outfile select * from sales_data into outfile '/tmp/sales_backup.csv'; -- 2. load data infile load data infile '/tmp/updated_data.csv' into table sales_data;
process 权限:
-- 授予process权限 grant process on *.* to 'monitor_user'@'localhost'; -- process权限的使用 show processlist; select * from information_schema.processlist; -- 监控应用示例 select * from sys.processlist where command != 'sleep' and time > 60;
reload 权限:
-- 授予reload权限 grant reload on *.* to 'maintenance_user'@'localhost'; -- reload权限的使用场景 flush tables; flush logs; flush privileges; flush status;
权限授予选项
with grant option:
-- 授予权限并允许转授 grant select, insert on database.* to 'manager_user'@'localhost' with grant option; -- 被授权用户可以继续授权给其他用户 -- (以manager_user身份执行) grant select on database.table1 to 'team_user'@'localhost';
with admin option:
-- 授予角色并允许转授 grant 'admin_role' to 'senior_dba'@'localhost' with admin option; -- 被授权用户可以继续分配角色 -- (以senior_dba身份执行) grant 'admin_role' to 'junior_dba'@'localhost';
总结
通过深入理解 mysql 的性能监控和安全管理系统,您可以:
建立全面的性能监控体系:
- 利用 performance schema 进行深度性能分析
- 通过 sys schema 简化性能数据访问
- 实现实时性能监控和预警
实施企业级安全管控:
- 配置细粒度的审计策略
- 建立完整的权限管理体系
- 遵循最小权限原则
优化数据库运维效率:
- 使用角色简化权限管理
- 通过进程监控快速诊断问题
- 建立标准化的安全基线
这些工具和最佳实践共同构成了 mysql 数据库稳定运行和安全管理的坚实基础,帮助您在保证性能的同时满足严格的安全合规要求。
以上就是mysql性能监控与安全管理的完整指南的详细内容,更多关于mysql性能监控与管理的资料请关注代码网其它相关文章!
发表评论