MySQL Session vs Global Variables Explained

MySQL utilizes two distinct scopes for its configuration variables—global and session—to balance overall server performance with individual client connection requirements. This article explains the operational differences between session-level and global-level variables in MySQL, how they interact, and how database administrators can manage them to optimize database behavior.

Global Variables

Global variables control the overall operation of the MySQL server. When the MySQL server starts, it initializes these variables using default values or configurations specified in the option files (such as my.cnf or my.ini).

Session Variables

Session variables apply strictly to individual client connections. They allow developers and administrators to customize the database behavior for a specific task or query session without affecting other connected users.

How Global and Session Variables Interact

When a new client connects to MySQL, the server automatically initializes the client’s session variables by copying the values from the currently active global variables.

Once the session is established, changes made to the global variables will not affect any currently active sessions. The new global values will only apply to connections created after the change was made. Conversely, changing a session variable only impacts the current connection and has zero effect on other active sessions or the global defaults.

Not All Variables Exist in Both Scopes

It is important to note that not all MySQL system variables can be set at both levels:

To check the current value of a variable at either scope, use the SHOW VARIABLES statement with the appropriate modifier:

SHOW GLOBAL VARIABLES LIKE 'join_buffer_size';
SHOW SESSION VARIABLES LIKE 'join_buffer_size';