MySQL is the popular relational database management system. This tutorial will help you to install and secure MySQL database server on your Ubuntu 20.04 LTS system.
You may like: How to Install phpMyAdmin on Ubuntu 20.04
Prerequisites
Before continuing for MySQL installation on Ubuntu 20.04:
- Login to Ubuntu as sudo privileged user
- For the newly installed systems, required to complete initial server setup
Step 1 – Install MySQL Server
The MySQL server 8.0 packages are available under the default repositories on Ubuntu 20.04. After login to your server, update apt cache and run install packages. To install MySQL server type:
sudo apt update sudo apt install mysql-server
Wait for the package manager to complete the installation.
After finishing the installation, the MySQL service will start automatically. To check the service status type:
sudo systemctl status mysql
Step 2 – Secure MySQL Server
At this stage, MySQL server is not secured yet. You can switch to root user of your system and connect to MySQL server. It will not prompt for any password.
So, here we need to secure the MySQL server. The packages provides a script to do it. Execute the following command for the initial settings of your MySQL server. You will see that script will prompt for more settings than earlier MySQL versions like password validation policy etc.
sudo mysql_secure_installation
Follow the on screen instruction to finish wizard. All the user inputs are highlighted in the wizard below. This will create password for the root user, remove test database and user to secure database server.
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: yThere 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: 2Please 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) : yBy 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) : ySuccess. 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) : ySuccess. 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) : ySuccess. All done!
Step 3 – Manage MySQL Service
MySQL service is managed under the systemd service manager. You can follow the default systemctl commands to manage database service on command line. Below is the some frequently used commands to manage service:
- To stop MySQL service, type:
sudo systemctl stop mysql - To start MySQL service, type:
sudo systemctl start mysql - Stop then start MySQL service, type:
sudo systemctl restart mysql - To view status of MySQL service, type:
sudo systemctl status mysql
Step 4 – Connect MySQL Server
You have successfully installed and secured the MySQL database server on your Ubuntu 20.04 system. Now connect to your database server on command line with the following command.
mysql -u root -p
Enter MySQL root user password to connect:
Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 20 Server version: 8.0.19-0ubuntu5 (Ubuntu) Copyright (c) 2000, 2020, 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>
Step 5 – Create MySQL User and Database
As you have connected to the MySQL server on command line. Your database server is ready to use for your applications to store data.
We recommend to create a separate database user to connect database by the application. Use the following commands to create a database on MySQL. Then create a user and assign privileges to user on database.
1 2 3 4 5 6 7 | CREATE DATABASE mydb; CREATE USER 'myuser'@'localhost' IDENTIFIED by 'Pa$$w0rd'; GRANT ALL on mydb.* to 'myuser'@'localhost'; FLUSH PRIVILEGES; |
Conclusion
This tutorial helps you with the installation of MySQL server on Ubuntu system. It also provides instruction’s to secure database server. Next, you can install phpMyAdmin to administer MySQL server via web based interface.
8 Comments
Thanks Rahul
How can i install Kubeflow In ubuntu do u have any tutorial for it.
I have a question about this tutorial.
Yes, please
‘secret’ did not work, but ‘Pa$$w0rd’ did. when creating a user.
Because there is a plugin called “validate_password” and by default it rejects passwords that are:
– shorter then 8 characters
– without at least 1 number
– without at least 1 upper case character
– without at least 1 special character
You can list it’s settings with:
sudo mysql
SHOW VARIABLES LIKE ‘validate_password%’;
If you set them as belows:
SET GLOBAL validate_password.length=5;
SET GLOBAL validate_password.number_count=0;
SET GLOBAL validate_password.mixed_case_count=0;
SET GLOBAL validate_password.special_char_count=0;
…only then it will accept ‘secret’ as a valid password.
Hello,
How I can install MySQL 5? This is very important to me for using Magento 1 version.
Thank you.
Currently Ubuntu 20.04 default repository or MySQL official apt repository doesn’t contains packages of MySQL 5.7. We will update you once the packages are available.