mysql 5.6.39源码层面看参数(一)

mysql 5.6.39源码层面看参数(一)

open_files_limit:
用于指定操作系统允许mysqld打开的最大文件数。只读变量,修改需要重启mysql服务
max_connections:
MySQL的实际最大可连接数为max_connections+1;这个参数实际起作用的最大值(实际最大可连接数)为100000,即该参数最大值不能超过16384,即使超过也以100000为准;
增加max_connections参数的值,不会占用太多系统资源。系统资源(CPU、内存)的占用主要取决于查询的密度、效率等;该参数设置过小的最明显特征是出现”Too many connections”错误
table_definition_cache:
存放表的定义信息。是frm文件在内存中的映射。MySQL需要打开frm文件,并将其内容初始化为Table Share 对象。这里存放与存储引擎无关的,独立的表定义相关信息
table_open_cache:
The number of open tables for all threads.
Increasing this value increases the number of file descriptors that mysqld requires.
实际上 table_open_cache的实际值大于table_definition_cache

这几个参数是通过mysqld_main()主函数调用adjust_related_options()再调用adjust_table_def_size()、adjust_open_files_limit()、adjust_max_connections()、adjust_table_cache_size()

adjust_related_options()实现方式

void adjust_related_options(ulong *requested_open_files)
{
 /* In bootstrap, disable grant tables (we are about to create them) */
 if (opt_bootstrap)
 opt_noacl= 1;

/* The order is critical here, because of dependencies. */
 adjust_open_files_limit(requested_open_files);/*计算open_file_limit*/
 adjust_max_connections(*requested_open_files);/*计算max_connections*/
 adjust_table_cache_size(*requested_open_files);/*计算table_open_cache*/
 adjust_table_def_size();/*计算table_definition_cache*/
}
void adjust_open_files_limit(ulong *requested_open_files)
{
 ulong limit_1;
 ulong limit_2;
 ulong limit_3;
 ulong request_open_files;
 ulong effective_open_files;

/* MyISAM requires two file handles per table. */
 limit_1= 10 + max_connections + table_cache_size * 2;

/*
 We are trying to allocate no less than max_connections*5 file
 handles (i.e. we are trying to set the limit so that they will
 be available).
 */
 limit_2= max_connections * 5;

/* Try to allocate no less than 5000 by default. */
 limit_3= open_files_limit ? open_files_limit : 5000;
 /*计算如上三个理论值最大出来*/
 request_open_files= max<ulong>(max<ulong>(limit_1, limit_2), limit_3);

/* Notice: my_set_max_open_files() may return more than requested. */
 effective_open_files= my_set_max_open_files(request_open_files);

if (effective_open_files < request_open_files)
 {
 char msg[1024];

if (open_files_limit == 0)
 {
 snprintf(msg, sizeof(msg),
 "Changed limits: max_open_files: %lu (requested %lu)",
 effective_open_files, request_open_files);
 buffered_logs.buffer(WARNING_LEVEL, msg);
 }
 else
 {
 snprintf(msg, sizeof(msg),
 "Could not increase number of max_open_files to "
 "more than %lu (request: %lu)",
 effective_open_files, request_open_files);
 buffered_logs.buffer(WARNING_LEVEL, msg);
 }
 }

open_files_limit= effective_open_files;
 if (requested_open_files)
 *requested_open_files= min<ulong>(effective_open_files, request_open_files);
}
void adjust_max_connections(ulong requested_open_files)
{
 ulong limit;
 /sql_cons.h文件里定义了#define TABLE_OPEN_CACHE_MIN 400/
 limit= requested_open_files - 10 - TABLE_OPEN_CACHE_MIN * 2;
 /*根据前面计算的requested_open_files来计算调整max_connections*/
 if (limit < max_connections)
 {
 char msg[1024];

snprintf(msg, sizeof(msg),
 "Changed limits: max_connections: %lu (requested %lu)",
 limit, max_connections);
 buffered_logs.buffer(WARNING_LEVEL, msg);

max_connections= limit;
 }
}
void adjust_table_cache_size(ulong requested_open_files)
{
 ulong limit;
 /*根据前面计算的出来的requested_open_files及max_connecions计算出table_open_cache*/
 limit= max<ulong>((requested_open_files - 10 - max_connections) / 2,
 TABLE_OPEN_CACHE_MIN);

if (limit < table_cache_size)
 {
 char msg[1024];

snprintf(msg, sizeof(msg),
 "Changed limits: table_open_cache: %lu (requested %lu)",
 limit, table_cache_size);
 buffered_logs.buffer(WARNING_LEVEL, msg);

table_cache_size= limit;
 }

table_cache_size_per_instance= table_cache_size / table_cache_instances;
}
void adjust_table_def_size()
{
 longlong default_value;
 sys_var *var;
 /*table_definition_cache大小从400随着table_cache_size增大而递增到2000*/
 default_value= min<longlong> (400 + table_cache_size / 2, 2000);
 var= intern_find_sys_var(STRING_WITH_LEN("table_definition_cache"));
 DBUG_ASSERT(var != NULL);
 var->update_default(default_value);

if (! table_definition_cache_specified)
 table_def_size= default_value;
}