In this guide, you will learn to secure a MySQL server.
Note: MariaDB is a popular fork of MySQL. The information provided in this article applies to MariaDB as well.
- A Linux system with MySQL installed (Using a different OS? See how to install and configure MySQL on a Windows server, CentOS 7, or CentOS 8).
- Access to the terminal (Ctrl+Alt+T).
- A network connection.
- An account with administrator privileges.
Note: Deploy a MySQL database instance on one of our pre-configured Bare Metal Cloud instances for superior performance, starting at only $0.10/h.
After installation, the MySQL server instance on your machine is insecure and susceptible to attacks.
mysql_secure_installation is a shell script developed for securing the MySQL server installation on Unix systems. The script configures security settings and allows you to:
- Set a password for root accounts (see how to reset or change MySQL root password)
- Remove the root accounts accessible from outside the localhost.
- Remove anonymous-user accounts.
- Delete the test database, accessible by anonymous users.
- Reload the user privileges tables.
[option] arguments are optional and discussed in the following section.
1. Execute the
mysql_secure_installation script using the following syntax:
sudo mysql_secure_installation [option]
2. Type your password and press
Y to set up the
VALIDATE PASSWORD component which checks whether the new password is strong enough.
3. Next, enter
2 depending on the password strength you want to set :
0- Low. The password consists of at least 8 characters.
1- Medium. The password consists of at least 8 characters (including numeric, mixed case, and special characters).
2- Strong. The password consists of at least 8 characters (including numeric, mixed case, and special characters, and compares the password to a dictionary file).
4. Once you specify the required strength, enter and re-enter the password.
5. The program evaluates the strength of your password and requires confirmation with
Y to continue.
6. Next you need to answer the following security features:
- Remove anonymous users?
- Disallow root login remotely?
- Remove test database and access to it?
- Reload privilege tables now?
To run the script with the default setting, the recommended answer to all these questions is
mysql_secure_installation script accepts certain options that customize MySQL security configurations. Specify the options in the command line or in the
[client] group of the option file.
Note: MySQL can read startup options from option files (also called configuration files). To check whether the program reads option files, use the
mysql --help command. If the program reads option files, the output indicates the name of the files and which option groups it recognizes. Open the file and add the wanted options to the
[client] group. It will be read by all MySQL clients, and the options specified will apply to all clients.
The most commonly used
mysql_secure_installation options are
For example, you can configure MySQL to permit IPv6 connections by clients that connect to the local server using port 3307. To do so, you need to add the
::1 local host address and change the default port (3306) to 3307.
Therefore, when running the installation script, you would use the command:
mysql_secure_installation --host=::1 --port=3307
Other supported options include:
|Specify the base directory.|
|Print the program argument list and exit.|
|Prevents the script from reading the default options from any option file.|
|Instructs the script to read only the specified option file |
|Reads the specified file |
|Reads the usual option groups, but also groups with the usual names and a |
|Displays a help message and exits.|
|Connects to the MySQL server on the specified host.|
|Prevents script from reading option files (except .mylogin.cnf file).|
|The script accepts this option but always ignores it. Hence, the script prompts for a password whenever invoked.|
|Specify the TCP/IP port number to connect to.|
|Prints the program name and the default options.|
|Specify a transport protocol to use for connecting to the server |
|Specify the Unix socket file or Windows |
|Enables or disables connection encryption, respectively. The options beginning with |
|Specify the file containing the trusted SSL Certificate Authorities list.|
|Specify the directory path containing trusted SSL Certificate Authority certificate files.|
|The path to the file containing the client SSL public key certificate.|
|A list of permissible ciphers for connection encryption.|
|The path to the file containing certificate revocation lists.|
|The directory containing certificate revocation list files.|
|The path to the file containing the client SSL private key certificate.|
|Specify one of the server connection security states, in order of increasing strictness: |
|Instruct the client to verify host name identity against the server certificate containing the Common Name identity.|
|Specify a comma-separated list of permissible TLS protocols for encrypted connections.|
|The script executes without interaction.|
|Specify the MySQL account user name for connecting to the server.|
This guide showed how to improve MySQL server security in Linux. After securing MySQL, we recommend improving MySQL performance to ensure a smooth server operation.
Continue learning about MySQL in our article on important MySQL commands that includes a downloadable cheat sheet.