Scopes of System Variables: Global and Session

This section describes global and session scopes of system variables. GLOBAL and SESSION keywords can be used to separate system variable scopes.

What Is Scope of a System Variable? Scopes of system variables are levels of controls that system variables can be applied to.

MySQL server supports two scopes for system variables:

Here are some basic guidelines on how to view and manage system variables in Global and Session scopes.

1. Some system variables have only 1 scope: Global Scope. These variables are called Global System Variables and applies to all user sessions. Examples of Global System Variables are:

2. Other system variables are Non-Global System Variables and have 2 scopes: Global Scope and Session Scope. Each Non-Global System Variable has 2 values: one for the Global Scope and one for the Session Scope. Examples of Global System Variables are:

3. When a MySQL server is started, the global value (value in the Global scope) of a system variable is initialized by the server based on server command options and the system environment.

4. When a user session is started, the session value (value in the Global scope) of a system variable is determined as below:

5. "SHOW [...] VARIABLES" statement can be used to view both global value and session value with different syntaxes:

6. "@@var" syntax represents only the session value of a system variable. For example:

mysql> set @@wait_timeout = 30000;

mysql> select @@wait_timeout;
+----------------+
| @@wait_timeout |
+----------------+
|          30000 |
+----------------+

mysql> show global variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout  | 28800 |
+---------------+-------+

7. "SET [...] var" statement can be used to set both global value and session value with different syntaxes:

Note that Global System Variables do not have any session values. So you can only set their global values. For example:

mysql> set @@slow_query_log = OFF;
ERROR 1229 (HY000): Variable 'slow_query_log' is a GLOBAL variable and
  should be set with SET GLOBAL

mysql> set slow_query_log = OFF;
ERROR 1229 (HY000): Variable 'slow_query_log' is a GLOBAL variable and
  should be set with SET GLOBAL

mysql> set global slow_query_log = OFF;
Query OK, 0 rows affected (0.02 sec)

Table of Contents

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

 Introduction of MySQL Programs

 PHP Programs and MySQL Server

 Perl Programs and MySQL Servers

 Java Programs and MySQL Servers

 Datatypes and Data Literals

 Operations and Expressions

 Character Strings and Bit Strings

 Commonly Used Functions

 Table Column Types for Different Types of Values

 Using DDL to Create Tables and Indexes

 Using DML to Insert, Update and Delete Records

 Using SELECT to Query Database

 Window Functions for Statistical Analysis

 Use Index for Better Performance

 Transaction Management and Isolation Levels

 Locks Used in MySQL

 Defining and Calling Stored Procedures

 Variables, Loops and Cursors Used in Stored Procedures

System, User-Defined and Stored Procedure Variables

 System Variables Like @@version

Scopes of System Variables: Global and Session

 User-Defined Variables Like @x

 User-Defined vs. Stored Procedure Variables

 MySQL Server Administration

 Storage Engines in MySQL Server

 InnoDB Storage Engine - Primary and Secondary Indexes

 Performance Tuning and Optimization

 Bulk Changes on Large Tables

 MySQL Server on macOS

 Installing MySQL Server on Linux

 Connection, Performance and Second Instance on Linux

 Archived Tutorials

 References

 Full Version in PDF/EPUB