启动时配置 - xiaoboluo768/mysql-system-schema GitHub Wiki

  • 要使用MySQL的performance_schema,需要在mysqld启动时启用,以启用事件收集功能,使用如下参数:
[mysqld]
performance_schema=ON  # 该参数在5.7.x之前支持performance_schema的版本中默认关闭,5.7.x版本开始默认开启
  • 注意:如果mysqld在初始化performance_schema时发现无法分配任何相关的内部缓冲区,则performance_schema将自动禁用,并将performance_schema设置为OFF

  • 在mysqld启动时可以使用启动选项进行配置需要启动哪些instruments,如下:

# 其中,instrument_name为一个instruments名称,作为key(如:wait/synch/mutex/sql/LOCK_open)。value可能的值 OFF,FALSE或0 表示禁用instruments;ON,TRUE或1 表示启用instruments;COUNTED:表示启用和计数(而不是时间)相关的instruments,启动选项格式如下:
--performance-schema-instrument='instrument_name=value'

# 使用精确匹配的instruments名称,只能指定一个instruments名称,如果要开启多个instruments,那么就需要使用--performance-schema-instrument选项多次,如果开启的instruments比较多,那就比较麻烦,可以使用通配符进行模糊匹配,如下
--performance-schema-instrument='wait/synch/cond/%=COUNTED'

# 如果要开启或禁用所有的instruments,可以使用如下选项值(注意:对于memory/performance_schema/% 这些instruments是无法关闭的)
--performance-schema-instrument='%=ON'
--performance-schema-instrument='%=OFF'
  • 只启用instruments还不够,仅仅只是启用了监视机制的代码,但是还没有指定这些数据存放在哪里,这些存放位置存放instruments收集的events数据,可以使用如下启动选项在mysqld启动时指定需要打开的consumers:
# 其中,consumer_name是一个consumers名称,如,events_waits_history。values可能的值有:OFF,FALSE或0:表示不要为某个consumers收集事件数据(不开启时相应的表视图不记录数据);ON,TRUE或1:表示为某个consumers收集事件(开启时相应的表视图中会保存数据),启动选项格式如下:
--performance-schema-consumer-consumer_name=value

如:
--performance-schema-consumer-events-waits-history=ON
  • 在performance_schema.setup_consumers表中可以查询到所有的有效consumers名称。要注意:对于setup_consumers表中的名称只能使用下划杠,而使用mysqld使用这些启动选项时可以使用中杠也可以使用下划杠(配置文件my.cnf中也可以使用下划杠和中杠),对于performance_schema,有效的system variables如下:
# performance_schema变量为ON或OFF,表示performance_schema是启用还是禁用。其他变量表示相关表大小(保存数据行数)或内存分配值大小
performance_schema=ON
performance_schema_accounts_size=-1
performance_schema_digests_size=10000
performance_schema_events_stages_history_long_size=10000
performance_schema_events_stages_history_size=10
performance_schema_events_statements_history_long_size=10000
performance_schema_events_statements_history_size=10
performance_schema_events_transactions_history_long_size=10000
performance_schema_events_transactions_history_size=10
performance_schema_events_waits_history_long_size=10000
performance_schema_events_waits_history_size=10
performance_schema_hosts_size=-1
performance_schema_max_cond_classes=80
performance_schema_max_cond_instances=-1
performance_schema_max_digest_length=1024
performance_schema_max_file_classes=80
performance_schema_max_file_handles=32768
performance_schema_max_file_instances=-1
performance_schema_max_index_stat=-1
performance_schema_max_memory_classes=320
performance_schema_max_metadata_locks=-1
performance_schema_max_mutex_classes=210
performance_schema_max_mutex_instances=-1
performance_schema_max_prepared_statements_instances=-1
performance_schema_max_program_instances=-1
performance_schema_max_rwlock_classes=40
performance_schema_max_rwlock_instances=-1
performance_schema_max_socket_classes=10
performance_schema_max_socket_instances=-1
performance_schema_max_sql_text_length=1024
performance_schema_max_stage_classes=150
performance_schema_max_statement_classes=193
performance_schema_max_statement_stack=10
performance_schema_max_table_handles=-1
performance_schema_max_table_instances=-1
performance_schema_max_table_lock_stat=-1
performance_schema_max_thread_classes=50
performance_schema_max_thread_instances=-1
performance_schema_session_connect_attrs_size=512
performance_schema_setup_actors_size=-1
performance_schema_setup_objects_size=-1
performance_schema_users_size=-1
  • 有效的mysqld启动选项如下:
performance_schema_consumer_events_stages_current=FALSE
performance_schema_consumer_events_stages_history=FALSE
performance_schema_consumer_events_stages_history_long=FALSE
performance_schema_consumer_events_statements_current=TRUE
performance_schema_consumer_events_statements_history=TRUE
performance_schema_consumer_events_statements_history_long=FALSE
performance_schema_consumer_events_transactions_current=FALSE
performance_schema_consumer_events_transactions_history=FALSE
performance_schema_consumer_events_transactions_history_long=FALSE
performance_schema_consumer_events_waits_current=FALSE
performance_schema_consumer_events_waits_history=FALSE
performance_schema_consumer_events_waits_history_long=FALSE
performance_schema_consumer_global_instrumentation=TRUE
performance_schema_consumer_statements_digest=TRUE
performance_schema_consumer_thread_instrumentation=TRUE 
performance_schema_instrument[=name]
  • 注意:
    • 启用performance_schema后,performance_schema会占用一部分服务器内存(具体多少取决于各个表视图中存放了多少数据),要增加或减少这些事件数据对内存的占用,可以通过一系列的system variables在启动时或运行过程中进行修改,具体参数含义详见文档《3-MySQL关键配置参数和状态变量说明》中1.9小节。
    • 对于动态修改,必须使用SQL语句对performance_schema下的配置表进行修改,不能使用配置选项修改,因为配置选项是启动选项,而不是system variables,如果您希望这些更改在mysqld启动时生效,可以使用mysqld启动选项或者将该语句放在一个文本文件中,并使用--init-file = file_name服务器启动选项来加载这个语句。
    • 启动时配置中的performance_schema_instrument启动选项对某个instruments设置为ON时,enabled和timed字段会同时被设置为ON,不能分开配置
    • 有相当一部分system variables是自动增长和缩小的,不需要在启动时或运行过程中去对这些自动调整的变量进行修改,以达到按需分配内存的目的(具体哪些可以自动调整,详见上文中提到的system variables中值为-1的),这些自动调整的system variables在mysqld启动时的计算规则参考的system variables如下,注意:如果给定非-1的值将不会进行自动计算也不会进行自动调整,而使用给定值:
max_connections
open_files_limit
table_definition_cache
table_open_cache

上一篇: 编译安装配置 | 下一篇: performance_schema事件计时器配置