MySQL is the most popular Open Source SQL database management system. It is developed and supported by Oracle Corporation. MySQL is widely used on Linux systems. Now MySQL providers also provide their own apt repository for installing MySQL on Ubuntu systems.
This tutorial will help you to install the MySQL server on Ubuntu 22.04 Jammy Jellyfish Linux systems.
Prerequisities
You must have a running Ubuntu 20.04 Linux system with sudo privileges user access.
Step 1 – Installing MySQL on Ubuntu 22.04
The default Ubuntu repositories contain MySQL 8.0. Which can be installed directly using the package manager without adding third-party PPA.
To install the available MySQL server version, execute the following command.
sudo apt-get install mysql-server
Press ‘y’ for any confirmation asked by the installer.
Once the installation is finished, you can secure the MySQL server by executing the following command.
sudo mysql_secure_installation
You will go through a wizard of questions to secure the MySQL server. Follow the onscreen instructions below:
- Press ‘y’ to enable validate password plugin. This will allow you to set a strict password policy for user accounts.
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
- Chose the password complexity level. Read the all 3 options and choose one:
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: 1
- Enter a new password and re-enter it. Make sure it matches the complexity level as described above.
New password: ************* Re-enter new password: *************
- Press ‘y’ to continue with provided password.
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
- Remove default anonymous users from MySQL server:
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
- Disable root login from remote systems
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
- Remove test database form MySQL created by default during installation.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Reload all privileges to apply above changes immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
You have secured the MySQL server in the LAMP stack on Ubuntu 22.04 Linux system.
Step 2 – Connect to MySQL Server
Remember that the above password set for the root accounts is used for remote users only. To log in from the same system, just type mysql on terminal.
sudo mysql
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 14 Server version: 8.0.28-0ubuntu4 (Ubuntu) Copyright (c) 2000, 2022, Oracle and/or its affiliates. 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 3 – Creating Database and Users
Here is few example queries to create database and users in MySQL server.
- Create a database named ‘mydb’.
CREATE DATABASE mydb;
- Next, create a user named ‘myuser’ accessible from ‘localhost’ only.
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'secure_password_';
- Grant permissions on database to user.
GRANT ALL ON mydb.* to 'myuser'@'localhost';
- Apply the permission changes at runtime.
FLUSH PRIVILEGES;
Step 4 – Manage MySQL Service
- To check the database server status.
sudo systemctl status mysql
- Use below command to start MySQL server.
sudo systemctl start mysql
- To stop MySQL server:
sudo systemctl stop mysql
- Restart MySQL database server, type:
sudo systemctl restart mysql
Step 5 – Uninstall (Remove) MySQL Server
If you no longer need to use the MySQL server, uninstall it from your server.
To remove MySQL server type:
sudo apt purge mysql-server-*
To completely uninstall MySQL, remove the following folders as well.
rm -rf /etc/mysql
rm -rf /var/lib/mysql
Conclusion
This tutorial helped you to install the MySQL server on Ubuntu 20.04 LTS Linux system. Also includes instructions to secure the database server and uninstall it.