Chapter 82. System Variables

Starting from MySQL 4.0.3, we provide better access to a lot of system and connection variables. Many variables can be changed dynamically while the server is running. This allows you to modify server operation without having to stop and restart it.

The mysqld server maintains two kinds of variables while it runs. Global variables affect the overall operation of the server. Session variables affect its operation for individual client connections.

When the server starts, it initializes all global variables to their default values. These defaults may be changed by options specified in option files or on the command line. After the server starts up, those global variables that are dynamic can be changed by connecting to the server and issuing a SET GLOBAL var_name statement. To change a global variable, you must have the SUPER privilege.

The server also maintains a set of session variables for each client that connects. The client's session variables are initialized at connect time using the current values of the corresponding global variables. For those session variables that are dynamic, the client can change them by issuing a SET SESSION var_name statement. Setting a session variable requires no special privilege, but a client can change only its own session variables, not those of any other client.

A change to a global variable affects only client connections that occur after the change. It does not affect session variables for any client that is already connected (not even those of the client that issues the SET GLOBAL statement).

Global or session variables may be set or retrieved using several syntax forms. The following examples use sort_buffer_size as a sample variable name.

To set the value of a GLOBAL variable, use one of the following syntaxes:

mysql> SET GLOBAL sort_buffer_size=value;
mysql> SET @@global.sort_buffer_size=value;

To set the value of a SESSION variable, use one of the following syntaxes:

mysql> SET SESSION sort_buffer_size=value;
mysql> SET @@session.sort_buffer_size=value;
mysql> SET sort_buffer_size=value;

LOCAL is a synonym for SESSION.

If you don't specify GLOBAL, SESSION, or LOCAL when setting a variable, SESSION is the default. See SET OPTION.

To retrieve the value of a GLOBAL variable, use one of the following statements:

mysql> SELECT @@global.sort_buffer_size;
mysql> SHOW GLOBAL VARIABLES like 'sort_buffer_size';

To retrieve the value of a SESSION variable, use one of the following statements:

mysql> SELECT @@session.sort_buffer_size;
mysql> SHOW SESSION VARIABLES like 'sort_buffer_size';

Here, too, LOCAL is a synonym for SESSION.

If you don't specify @@global, @@session, or @@local when retrieving a variable with SELECT, MySQL returns the SESSION value if it exists and the GLOBAL value otherwise.

If you don't specify GLOBAL, SESSION, or LOCAL for SHOW VARIABLES, MySQL returns the SESSION value.

The reason for requiring the GLOBAL keyword when setting GLOBAL-only variables but not when retrieving them is to ensure that we don't later run into problems if we would introduce a SESSION variable with the same name or remove a SESSION variable. In that case, a client with the SUPER privilege might change the GLOBAL variable by accident, rather than just the SESSION variable for the client's own connection.

The following table shows the full list of all variables that you can change and retrieve using GLOBAL or SESSION. The last column indicates for each variable whether GLOBAL or SESSION (or both) apply.

Variable nameValue typeType
autocommitboolSESSION
big_tablesboolSESSION
binlog_cache_sizenumGLOBAL
bulk_insert_buffer_sizenumGLOBAL | SESSION
concurrent_insertboolGLOBAL
connect_timeoutnumGLOBAL
convert_character_setstringSESSION
delay_key_writeOFF | ON | ALLGLOBAL
delayed_insert_limitnumGLOBAL
delayed_insert_timeoutnumGLOBAL
delayed_queue_sizenumGLOBAL
error_countnumSESSION
flushboolGLOBAL
flush_timenumGLOBAL
foreign_key_checksboolSESSION
identitynumSESSION
insert_idboolSESSION
interactive_timeoutnumGLOBAL | SESSION
join_buffer_sizenumGLOBAL | SESSION
key_buffer_sizenumGLOBAL
last_insert_idnumSESSION
local_infileboolGLOBAL
log_warningsboolGLOBAL
long_query_timenumGLOBAL | SESSION
low_priority_updatesboolGLOBAL | SESSION
max_allowed_packetnumGLOBAL | SESSION
max_binlog_cache_sizenumGLOBAL
max_binlog_sizenumGLOBAL
max_connect_errorsnumGLOBAL
max_connectionsnumGLOBAL
max_error_countnumGLOBAL | SESSION
max_delayed_threadsnumGLOBAL
max_heap_table_sizenumGLOBAL | SESSION
max_join_sizenumGLOBAL | SESSION
max_relay_log_sizenumGLOBAL
max_sort_lengthnumGLOBAL | SESSION
max_tmp_tablesnumGLOBAL
max_user_connectionsnumGLOBAL
max_write_lock_countnumGLOBAL
myisam_max_extra_sort_file_sizenumGLOBAL | SESSION
myisam_repair_threadsnumGLOBAL | SESSION
myisam_max_sort_file_sizenumGLOBAL | SESSION
myisam_sort_buffer_sizenumGLOBAL | SESSION
net_buffer_lengthnumGLOBAL | SESSION
net_read_timeoutnumGLOBAL | SESSION
net_retry_countnumGLOBAL | SESSION
net_write_timeoutnumGLOBAL | SESSION
query_alloc_block_sizenumGLOBAL | SESSION
query_cache_limitnumGLOBAL
query_cache_sizenumGLOBAL
query_cache_typeenumGLOBAL
query_prealloc_sizenumGLOBAL | SESSION
range_alloc_block_sizenumGLOBAL | SESSION
read_buffer_sizenumGLOBAL | SESSION
read_rnd_buffer_sizenumGLOBAL | SESSION
rpl_recovery_ranknumGLOBAL
safe_show_databaseboolGLOBAL
server_idnumGLOBAL
slave_compressed_protocolboolGLOBAL
slave_net_timeoutnumGLOBAL
slow_launch_timenumGLOBAL
sort_buffer_sizenumGLOBAL | SESSION
sql_auto_is_nullboolSESSION
sql_big_selectsboolSESSION
sql_big_tablesboolSESSION
sql_buffer_resultboolSESSION
sql_log_binlogboolSESSION
sql_log_offboolSESSION
sql_log_updateboolSESSION
sql_low_priority_updatesboolGLOBAL | SESSION
sql_max_join_sizenumGLOBAL | SESSION
sql_quote_show_createboolSESSION
sql_safe_updatesboolSESSION
sql_select_limitboolSESSION
sql_slave_skip_counternumGLOBAL
sql_warningsboolSESSION
table_cachenumGLOBAL
table_typeenumGLOBAL | SESSION
thread_cache_sizenumGLOBAL
timestampboolSESSION
tmp_table_sizeenumGLOBAL | SESSION
transaction_alloc_block_sizenumGLOBAL | SESSION
transaction_prealloc_sizenumGLOBAL | SESSION
tx_isolationenumGLOBAL | SESSION
wait_timeoutnumGLOBAL | SESSION
warning_countnumSESSION
unique_checksboolSESSION

Variables that are marked as num can be given a numeric value. Variables that are marked as bool can be set to 0, 1, ON or OFF. Variables that are marked as enum should normally be set to one of the available values for the variable, but can also be set to the number that correspond to the desired enumeration value. (The first enumeration value corresponds to 0; note that this differs from ENUM columns, for which the first enumeration value corresponds to 1.)

Here is a description of some of the variables:

VariableDescription
identityAlias for last_insert_id (Sybase compatibility)
sql_low_priority_updatesAlias for low_priority_updates
sql_max_join_sizeAlias for max_join_size
versionAlias for VERSION() (Sybase (?) compatibility)

Further information about system variables can be found in the startup options section, and in the descriptions of the SHOW VARIABLES and SET statements. See Server options. See SHOW VARIABLES. See SET OPTION.