Install and Secure MySQL on Debian, Ubuntu, Linux Mint, Pop!_OS, Elementary OS
MySQL is probably the most-used relational database in the world despite the fierce competition between SQL databases (e.g. PostgreSQL, MariaDB). This tutorial will cover how to set up MySQL on Debian/Ubuntu based Linux distros in CLI, so all the steps could be done through SSH without the need for a graphical interface.
Installation
Update the package lists then install mysql-server
with apt
:
$ sudo apt update
$ sudo apt install mysql-server
Start MySQL database:
$ sudo systemctl start mysql
Check if the MySQL server is up and running:
$ systemctl status mysql
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2022-02-11 06:20:13 UTC; 8min ago
Main PID: 112185 (mysqld)
Status: "Server is operational"
Tasks: 39 (limit: 28688)
Memory: 359.7M
CGroup: /system.slice/mysql.service
└─112185 /usr/sbin/mysqld
Output
Configuration
We'll be enabling the MySQL service so it'll start on boot, and also use the recommended script for securing our database.
Start on Startup
To make MySQL automatic start on system startup, enable mysql.service
:
$ sudo systemctl enable mysql
Synchronizing state of mysql.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable mysql
Output
Security
mysql_secure_installation
is a shell script provided by the MySQL package on Linux. It improves the security of your database by:
- Setting password for
root
user. - Disable remote
root
logins. - Remove the anonymous users.
- Remove the test database which can be accessed by anonymous users.
Run the script with sudo
and follow its instructions:
$ sudo mysql_secure_installation
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
Output
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: y
Output
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Output
Please set the password for root here.
New password:
Re-enter new password:
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
Output
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Output
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
Output
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Output
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
Output
All done!
Output