Messing with MySQL's Global State
One of the things I like about MySQL is that it allows you to change some of its global state as it’s running. This article will provide a quick overview of how to view the current variables and how to make changes.
Please note: All examples in this article were written using MySQL 5.5 and done in a development environment. Please do not try this on a production environment unless you are sure of the ramifications.
Global vs Session
MySQL has two levels of variables. The first is session which only affects your current connection to the server. If you’re just playing around you should use this because it should only hurt you if you do something wrong. The second is global which affects all the connections and any future connections until a reboot. We’re using this level in this article but if you want to set the session level of the variables change “global” to “session” in the examples below.
Seeing the Variables
Viewing all the variables can be done using the show global variables
command:
317 rows is a bit much to read through to find a variable you’re interested in. Thankfully because show global variables
is basically a query you can add a where clause:
You can also put the variable name in an SQL select query to perform calculations on it:
Updating a Variable
In order to update a variable we can use the set
command. For this example, we’re going to set the SQL_MODE to the mode that’s the default in MySQL 8 (per https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html). You can do this to see how your code will handle the new modes.
Globally:
Now we can see that the value has changed:
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows