Configuring and Managing SSL On Your MySQL Server
Tags: mysql, ssl, server, sql
Published: 2018-09-18 16:44:31

---
title: Configuring and Managing SSL On Your MySQL Server
published: true
description: Learn how to configure and manage SSL in MySQL hosting, including the default configuration, disabling SSL, and enabling and enforcing SSL on a MySQL server
tags: MySQL, SSL, Server, SQL
canonical_url: https://scalegrid.io/blog/configuring-and-managing-ssl-on-your-mysql-server/
---
<link rel="canonical" href="https://scalegrid.io/blog/configuring-and-managing-ssl-on-your-mysql-server/" />

<img src="https://mongodb.scalegrid.io/hubfs/Configuring-and-Managing-SSL-on-Your-MySQL-Server-ScaleGrid-Blog-150551-edited.png" alt="Configuring and Managing SSL On Your MySQL Server - ScaleGrid Blog" style="width:100%; height:auto;">
<p style="text-align: justify;">In this blog post, we review some of the important aspects of configuring and managing SSL in <a href="https://scalegrid.io/mysql.html" target="_blank">MySQL hosting</a>. These would include the default configuration, disabling SSL, and enabling and enforcing SSL on a MySQL server. Our observations are based on the community version of MySQL 5.7.21.</p>

<p style="text-align: justify;">Read the original tutorial: <a href="https://scalegrid.io/blog/configuring-and-managing-ssl-on-your-mysql-server/" target="_blank">Configuring and Managing SSL On Your MySQL Server
</a>

<h2 style="padding: 20px 0px 10px 0px;"><span style="font-weight: 400;">Default SSL Configuration in MySQL</span></h2>
<p style="text-align: justify;">By default, MySQL server always installs and enables SSL configuration. However, it is not enforced that clients connect using SSL. Clients can choose to connect with or without SSL as the server allows both types of connections. Let’s see how to verify this default behavior of MySQL server.</p>
<p style="text-align: justify;">When SSL is installed and enabled on MySQL server by default, we will typically see the following:</p>

<ol>
	<li style="text-align: justify;">Presence of *.pem files in the MySQL data directory. These are the various client and server certificates and keys that are in use for SSL as described <a href="https://dev.mysql.com/doc/refman/5.7/en/creating-ssl-rsa-files-using-mysql.html#creating-ssl-rsa-files-using-mysql-automatic" target="_blank">here</a>.</li>
	<li style="text-align: justify;">There will be a note in the mysqld error log file during the server start, such as:
<ul>
	<li><em>[Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.</em></li>
</ul>
</li>
	<li style="text-align: justify;">Value of ‘have_ssl’ variable will be YES:</li>
</ol>
<p style="padding-left: 60px;"><i>mysql&gt; show variables like 'have_ssl';</i></p>
<p style="padding-left: 60px;"><i>+---------------+-------+</i></p>
<p style="padding-left: 60px;"><i>| Variable_name | Value |</i></p>
<p style="padding-left: 60px;"><i>+---------------+-------+</i></p>
<p style="padding-left: 60px;"><i>| have_ssl      | YES |</i></p>
<p style="padding-left: 60px;"><i>+---------------+-------+</i></p>
&nbsp;
<p style="text-align: justify;">With respect to MySQL client, by default, it always tries to go for encrypted network connection with the server, and if that fails, it falls back to unencrypted mode.</p>
<p style="text-align: justify;">So, by connecting to MySQL server using the command:</p>
<p style="padding-left: 30px;"><i>mysql -h &lt;hostname&gt; -u &lt;username&gt; -p</i></p>
<p style="text-align: justify;">We can check whether the current client connection is encrypted or not using the status command:</p>
<p style="padding-left: 60px;"><i>mysql&gt; status</i></p>
<p style="padding-left: 60px;"><i>--------------</i></p>
<p style="padding-left: 60px;"><i>mysql  Ver 14.14 Distrib 5.7.21, for Linux (x86_64) using  EditLine wrapper</i></p>
<p style="padding-left: 60px;"><i>Connection id:          75</i></p>
<p style="padding-left: 60px;"><i>Current database:</i></p>
<p style="padding-left: 60px;"><i>Current user:           root@127.0.0.1</i></p>
<p style="padding-left: 60px;"><span style="color: #0000ff;"><i>SSL:                    Cipher in use is DHE-RSA-AES256-SHA</i></span></p>
<p style="padding-left: 60px;"><i>Current pager:          stdout</i></p>
<p style="padding-left: 60px;"><i>Using outfile:          ''</i></p>
<p style="padding-left: 60px;"><i>Using delimiter:        ;</i></p>
<p style="padding-left: 60px;"><i>Server version:         5.7.21-log MySQL Community Server (GPL)</i></p>
<p style="padding-left: 60px;"><i>Protocol version:       10</i></p>
<p style="padding-left: 60px;"><i>Connection:             127.0.0.1 via TCP/IP</i></p>
<p style="padding-left: 60px;"><i>…………………………..</i></p>
<p style="text-align: justify;">The SSL field highlighted above indicates that the connection is encrypted. We can, however, ask the MySQL client to connect without SSL by using the command:</p>
<p style="padding-left: 30px;"><i>mysql -h &lt;hostname&gt; -u &lt;username&gt;  -p </i><i>--ssl-mode=DISABLED</i></p>
<p style="padding-left: 60px;"> <i>mysql&gt; status</i></p>
<p style="padding-left: 60px;"><i>--------------</i></p>
<p style="padding-left: 60px;"><i>Connection id:          93</i></p>
<p style="padding-left: 60px;"><i>Current database:</i></p>
<p style="padding-left: 60px;"><i>Current user:           sgroot@127.0.0.1</i></p>
<p style="padding-left: 60px;"><span style="color: #0000ff;"><i>SSL:                    Not in use</i></span></p>
<p style="padding-left: 60px;"><i>Current pager:          stdout</i></p>
<p style="padding-left: 60px;"><i>Using outfile:          ''</i></p>
<p style="padding-left: 60px;"><i>Using delimiter:        ;</i></p>
<p style="padding-left: 60px;"><i>Server version:         5.7.21-log MySQL Community Server (GPL)</i></p>
<p style="padding-left: 60px;"><i>Protocol version:       10</i></p>
<p style="padding-left: 60px;"><i>Connection:             127.0.0.1 via TCP/IP</i></p>
<p style="padding-left: 60px;"><i>……………………………</i></p>
<p style="text-align: justify;">We can see that even though SSL is enabled on the server, we are able to connect to it without SSL.</p>


<h2 style="padding: 20px 0px 10px 0px;"><span style="font-weight: 400;">Disabling SSL in MySQL</span></h2>
<p style="text-align: justify;">If your requirement is to completely turn off SSL on MySQL server instead of the default option of ‘enabled, but optional mode’, we can do the following:</p>

<ol>
	<li style="text-align: justify;"> Delete the *.pem certificate and key files in the MySQL data directory.</li>
	<li style="text-align: justify;">Start MySQL with SSL option turned off. This can be done by adding a line entry:</li>
</ol>
<p style="padding-left: 60px;"><i>ssl=0 </i><i> </i>  in the my.cnf file.</p>
<p style="text-align: justify;"> We can observe that:</p>

<ol>
	<li style="text-align: justify;">There will NOT be any note in mysqld logs such as :
<ul>
	<li><i>[Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.</i></li>
</ul>
</li>
	<li style="text-align: justify;"> Value of ‘have_ssl’ variable will be DISABLED:</li>
</ol>
<p style="padding-left: 60px;"><i>mysql&gt; show variables like 'have_ssl';</i></p>
<p style="padding-left: 60px;"><i>+---------------+-------+</i></p>
<p style="padding-left: 60px;"><i>| Variable_name | Value |</i></p>
<p style="padding-left: 60px;"><i>+---------------+-------+</i></p>
<p style="padding-left: 60px;"><i>| have_ssl      | DISABLED |</i></p>
<p style="padding-left: 60px;"><i>+---------------+-------+</i></p>

<h2 style="padding: 20px 0px 10px 0px;"><span style="font-weight: 400;">Enforcing SSL in MySQL</span></h2>
<p style="text-align: justify;">We saw that though SSL was enabled by default on MySQL server, it was not enforced and we were still able to connect without SSL.</p>
<p style="text-align: justify;">Now, by setting the <a href="https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_require_secure_transport" target="_blank">require_secure_transport</a> system variable, we will be able to enforce that server will accept only SSL connections. This can be verified by trying to connect to MySQL server with the command:</p>
<p style="padding-left: 30px;"><i> mysql -h &lt;hostname&gt; -u sgroot -p --ssl-mode=DISABLED</i></p>
<p style="text-align: justify;">And, we can see that the connection would be refused with following error message from the server:</p>
<p style="padding-left: 30px;"><i>ERROR 3159 (HY000): Connections using insecure transport are prohibited while --require_secure_transport=ON.</i></p>

<h2 style="padding: 20px 0px 10px 0px;"><span style="font-weight: 400;">SSL Considerations for Replication Channels</span></h2>
<p style="text-align: justify;">By default, in a MySQL replication setup, the slaves connect to the master without encryption.</p>
<p style="text-align: justify; padding-bottom: 20px;">Hence to connect to a master in a secure way for replication traffic, slaves must use <i>MASTER_SSL=1; </i>as part of the ‘<i>CHANGE MASTER TO’</i> command which specifies parameters for connecting to the master. Please note that this option is also mandatory in case your master is configured to enforce SSL connection using <a href="https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_require_secure_transport" target="_blank">require_secure_transport</a>.</p>