Setting MySQL Configuration Variables - MySQL 5.7 vs MySQL 8.0
Tags: mysql, configuration, database, tutorial
Published: 2020-11-06 16:08:36

---
title: Setting MySQL Configuration Variables - MySQL 5.7 vs MySQL 8.0
published: true
description: In this blog post, we will explain the differences in managing the configuration variables between MySQL 5.7 and MySQL 8.0.
tags: mysql, configuration, database, tutorial
cover_image: https://scalegrid.io/blog/wp-content/uploads/2020/10/Setting-MySQL-Configuration-Variables-MySQL-5-7-vs-MySQL-8-0-Blog.jpg
---
<link rel="https://scalegrid.io/blog/setting-mysql-configuration-variables-mysql-5-7-vs-mysql-8-0/" />
<p style="text-align: justify";>MySQL <a target="_blank" style="color: #2da964;" href="https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html">configuration variables</a> are a set of server system variables used to configure the operation and behavior of the server. In this blog post, we will explain <a target="_blank" style="color: #2da964;" href="https://scalegrid.io/blog/setting-mysql-configuration-variables-mysql-5-7-vs-mysql-8-0/">the differences in managing the configuration variables between MySQL 5.7 and MySQL 8.0</a>.</p>

<p style="text-align: justify";>We will explain three different ways for setting the configuration variables based on your use-case. Configuration variables that can be set at run time are called Dynamic variables and those that need a MySQL server restart to take effect are called Non-Dynamic variables.</p>

<table style="border: solid 20px #def5fe;" width="100%">
<tbody>
<tr>
<th style="padding: 20px 30px 20px 30px;">
<h2 style="padding-bottom: 10px;">Setting MySQL Configuration Variables</h2>
<ul>
<li style="padding-bottom: 10px;"><a style="color: #2da964; text-decoration: underline;" href="#1">1: Set the Configuration for the Current Lifespan of a Running MySQL Server</a></li>
<li style="padding-bottom: 10px;"><a style="color: #2da964; text-decoration: underline;" href="#2">2: Set and Persist the Configuration Change across MySQL Restarts</a></li>
<li style="padding-bottom: 10px;"><a style="color: #2da964; text-decoration: underline;" href="#3">3: Setting Configuration Variables that are not Dynamic</a></li>
<li style="padding-bottom: 10px;"><a style="color: #2da964; text-decoration: underline;" href="#4">MySQL Configuration Management across Multiple Servers</a></li>
</ul>
</th>
</tr>
</tbody>
</table>

<h2 style="padding-top: 15px;"><a id="1"></a>1: Set the Configuration for the Current Lifespan of a Running MySQL Server</h3>
<p style="text-align: justify";>Most of the MySQL configurations are dynamic in nature and can be set at run time using the SET command. This means that the changes are not persistent and will be lost if MySQL server restarts. This is useful to test the behavior of your config change before making it permanent.</p>

<p style="text-align: justify";>For both MySQL 5.7 and 8.0, you can accomplish this by using the command SET GLOBAL</p>

<p style="text-align: justify";">Example:</p>
<pre><code>
mysql> set global max_connect_errors=10000;
</code></pre>
<h2 style="padding-top: 15px;"><a id="2"></a>2: Set and Persist the Configuration Change across MySQL Restarts</h3>
<p style="text-align: justify";>Once you are happy with the configuration change settings, you would want to make it permanent.</p>

<p style="text-align: justify";>In MySQL 5.7, you would need to do this in 2 steps:</p>

<ol>
<li>Set the run time configuration setting using the command SET GLOBAL</li>
</ol>
<pre><code>
mysql> set global max_connect_errors=10000;
</code></pre>
<ol start="2" style="padding-top: 15px;">
<li>Save this change in your my.cnf file by updating the existing entry for max_connect_errors or by adding a new one.</li>
</ol>

<p style="text-align: justify";>This has become much easier in MySQL 8.0. You can do this in a single step using the command SET PERSIST</p>
<pre><code>
mysql> set persist max_connect_errors=10000;
</code></pre>

<p style="text-align: justify";>This will set the run-time value for the configuration and also persist the change by saving it in the file mysqld-auto.cnf that exists in the data directory. This is a json file and you will now see the following entries in the file.</p>
<pre><code>
{

"Version": 1,

"mysql_server": {

"max_connect_errors": {

"Value": "10000",

"Metadata": {

"Timestamp": 1581135119397374,

"User": "sgroot",

"Host": "localhost"

}

}

}

}
</code></pre>
<p style="text-align: justify";><b>Note: </b> The configuration settings present in mysqld-auto.cnf always overrides the values present in the my.cnf file. So, any further changes you make in the my.cnf file for the variable "max_connect_errors” do not take effect. This may be confusing for those transitioning from MySQL 5.7 as they may be used to storing all their settings in my.cnf</p>

<h2 style="padding-top: 15px;"><a id="3"></a>3: Setting Configuration Variables that are not Dynamic</h3>
<p style="text-align: justify";>Some of the configuration variables cannot be set at run time and would require a MySQL restart to take effect.</p>

<p style="text-align: justify";>In MySQL 5.7, you would make an entry for these variables in your my.cnf file and restart MySQL server for it to take effect. Example for such a variable is innodb_log_file_size.</p>

<p style="text-align: justify";>In MySQL 8.0, you can run a command called SET PERSIST ONLY which will make an entry in mysqld-auto.cnf.</p>

<p style="text-align: justify";">Example:</p>
<pre><code>
mysql> set persist_only innodb_log_file_size=134217728;
</code></pre>
<p style="text-align: justify";>It is also possible to restart the MySQL server from the command line using the RESTART command. This will cause the changed value of ‬innodb_log_file_size to take effect.</p>

<p style="text-align: justify";><b>Note: </b>The RESTART command works only if MySQL is managed using external programs like systemd or <a target="_blank" style="color: #2da964;" href="https://dev.mysql.com/doc/refman/8.0/en/mysqld-safe.html">mysqld_safe</a>. Please see more details on this <a target="_blank" style="color: #2da964;" href="https://dev.mysql.com/doc/refman/8.0/en/restart.html">here</a>.</p>

<p style="text-align: justify";>Otherwise , RESTART command fails with a message like the following.</p>
<pre><code>
mysql> RESTART;

ERROR 3707 (HY000): Restart server failed (mysqld is not managed by supervisor process).
</code></pre>
<h2 style="padding-top: 15px;"><a id="4"></a>MySQL Configuration Management across Multiple Servers</h3>

<p style="text-align: justify";>Managing MySQL configuration in source-replica environments is a tedious process if you have to manage multiple clusters that are running different MySQL versions. This is where a managed solution like ScaleGrid would be beneficial.</p>

<p style="text-align: justify";><a target="_blank" style="color: #2da964;" href="https://help.scalegrid.io/docs/mysql-configuration-settings">The ScaleGrid UI console</a> can be used to review the current settings of various configuration variables or set their values.</p>

<p style="text-align: justify";>ScaleGrid can recognize when a configuration setting is non-dynamic and will warn the user if a MySQL restart is required for the value to take effect. ScaleGrid will also make a backup of the current my.cnf file before applying any new configuration changes.</p>

<p style="text-align: justify";>In source-replica environments, ScaleGrid changes the configuration settings in a rolling fashion one server at a time. If a non-dynamic variable needs to be set, ScaleGrid performs a failover of the current master to minimize the downtime that is involved if MySQL has to be otherwise restarted.</p>