How to Install MySQL on Debian 9
6 min read
With the release of Debian 9 Stretch MySQL, the world’s most popular open source relational database management system is no longer available in the Debian’s repositories and MariaDB has become the default database system. MariaDB is a backward compatible, binary drop-in replacement of MySQL.
In this tutorial, we will show you how to install and secure MySQL on a Debian 9 machine from the MySQL Apt Repository. If your application does not have any specific requirements, you should stick with MariaDB, the default database system in Debian 9.
Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges.
Step 1: Configuring MySQL Repository
Once the download is completed install the release package with the following command:
sudo dpkg -i mysql-apt-config_0.8.10-1_all.deb
You will be presented with the configuration menu where you can select the MySQL version you want to install.
MySQL 8.0 is pre-selected, if you want to install another version of MySQL, select
MySQL Server & Cluster (Currently selected: mysql-8.0) and choose your preferred MySQL version
We’re going to install MySQL version 8.0. Select the last option
OK and press
Enter (as shown on the image above) to save the configuration.
Step 2: Installing MySQL
Before installing MySQL on your Debian 9 server first update the package list with:
sudo apt update
Once the packages list is updated run the following command to install MySQL on your Debian server:
sudo apt install mysql-server
The installer will ask you to set the MySQL root password. Do not set the password now (leave it blank), we will do that in the next section.
Next, you will be presented with a message informing you about the new MySQL 8 authentication. Before selecting the default MySQL 8 authentication plugin make sure it is supported by your application.
Step 3: Verifying MySQL installation
Once the installation is completed, the MySQL service will start automatically.
We can check the MySQL service status by typing:
sudo systemctl status mysql
● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: Active: active (running) since Thu 2018-08-02 17:22:18 UTC; 18s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 14797 ExecStartPre=/usr/share/mysql-8.0/mysql-systemd-start pre (co Main PID: 14832 (mysqld) Status: "SERVER_OPERATING" Tasks: 37 (limit: 4915) CGroup: /system.slice/mysql.service └─14832 /usr/sbin/mysqld
Step 4: Securing MySQL
mysql_secure_installation command to set the root password and to improve the security of the MySQL installation:
Securing the MySQL server deployment. Connecting to MySQL using a blank password. 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:
You will be asked to configure the
VALIDATE PASSWORD PLUGIN which is used to test the strength of the MySQL users passwords. There are three levels of password validation policy, low, medium and strong. Press
ENTER if you don’t want to set up the validate password plugin.
Please set the password for root here. New password: Re-enter new password:
On the next prompt, you will be asked to set a password for the MySQL root user.
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. 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. 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. 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. All done!
Once you set the root password the script will also ask you to remove the anonymous user, restrict root user access to the local machine and remove the test database. You should answer “Y” (yes) to all questions.
Step 5: Connect to MySQL from the command line
To interact with MySQL through the terminal we will use the MySQL client which is installed as a dependency of the MySQL server package.
To log in to the MySQL server as the root user type:
mysql -u root -p
You will be prompted to enter the root password you have previously set when the
mysql_secure_installation script was run.
Once you enter the password you will be presented with the MySQL shell as shown below:
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 8.0.12 MySQL Community Server - GPL Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
Create a Database
Once you are connected to the MySQL shell, you can create a new database by typing the following command:
CREATE DATABASE new_database;
Query OK, 1 row affected (0.00 sec)
Now that we have created a database we can create a table to store some data.
Before running the SQL statements for creating a table we need to connect to the database:
In this example we will create a simple table named
contacts with three fields,
CREATE TABLE contacts ( id INT PRIMARY KEY, name VARCHAR(30), email VARCHAR(30) );
Query OK, 1 row affected (0.00 sec)
In this tutorial, we’ve shown you how to install and secure a MySQL server on a Debian 9 server. We have also shown you how to connect to the MySQL shell and how to create a new database and table.
Now that your MySQL server is up and running and you know how to connect to the MySQL server from the command line, you might want to check the following guides: