MySQL/MariaDB Database Server Installation and Configuration

Introduction
This guide will show you how to install and configure MySQL/MariaDB Server on Ubuntu 22.04.
What is MySQL/MariaDB Server?
MySQL/MariaDB Server is a free and open-source relational database management system. It is used by many popular websites, including Facebook, Twitter, and YouTube.
Prerequisites
- A server running Ubuntu 22.04.
- A non-root user with sudo privileges.
Installation
Before starting, it is recommended to update your system packages to the latest version. You can update them with the following command:
sudo apt update
Once your system is up-to-date, you can proceed to install MySQL/MariaDB Server.
Step 1 - Install MySQL/MariaDB Server
By default, MySQL/MariaDB Server is available in the Ubuntu 22.04 default repository. You can install it with the following command:
sudo apt install mariadb-server
Step 2 - Create a Database
By default, MySQL/MariaDB Server comes with a root user and test database. You can log in to the MySQL shell with the following command:
sudo mysql -u root -p
Enter your root password when prompted, then create a new database with the following command:
CREATE DATABASE mydb;
Step 3 - Create a Database User
Next, you will need to create a database user and grant privileges to the database. You can create a database user with the following command:
CREATE USER 'myuser'@'127.0.0.1' IDENTIFIED BY 'password';
Note: Replace myuser
and password
with your own values.
Next, grant all privileges to the mydb
database with the following command:
GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'127.0.0.1' WITH GRANT OPTION;
Next, flush the privileges and exit from the MySQL shell with the following command:
FLUSH PRIVILEGES;
EXIT;
Step 4 - Enable Remote Access
By default, MySQL/MariaDB Server is listening on the localhost only.
You can verify it with the following command:
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
Find the following line:
bind-address = 127.0.0.1
And, replace it with the following line:
bind-address = 0.0.0.0
You should only do this if you are sure that you want to allow external access to your database server. If you are unsure, you should leave this setting as it is.
Save and close the file, then restart the MySQL/MariaDB service to apply the changes:
sudo systemctl restart mariadb
Step 5 - Verify MySQL/MariaDB Database Server
At this point, MySQL/MariaDB Server is installed and configured. It's time to verify the MySQL/MariaDB Server.
You can verify it with the following command:
sudo mysql -u myuser -p
Enter your database user password when prompted, then check the database list with the following command:
SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.001 sec)
You can also check the database user with the following command:
SELECT user,host FROM mysql.user;
You should see the following output:
+------------------+-----------+
| user | host |
+------------------+-----------+
| myuser |
| debian-sys-maint | localhost |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
6 rows in set (0.001 sec)
Conclusion
Congratulations! you have successfully installed and configured MySQL/MariaDB Server on Ubuntu 22.04. You can now host your own database server with ease.