# How To Install MariaDB on Ubuntu 22.04

MariaDB is a robust open-source relational database management system that emerged as a fork of MySQL. Due to its fault tolerance, speed, and scalability, it is commonly used as a replacement for MySQL.

{% embed url="<https://youtu.be/iY9h9JaLQXI?si=n2mMGGBDWZmFsaqY>" %}

## Prerequisites

* Launch the [Ubuntu 22.04 instance](https://docs.neevcloud.com/neevcloud-products/computes/getting-started-launch-vms) on Neevcloud
* Access the server using [SSH](https://docs.neevcloud.com/neevcloud-products/computes/getting-started-launch-vms#ssh)

## Update && Upgrade the system <a href="#update-and-and-upgrade-the-system" id="update-and-and-upgrade-the-system"></a>

First, ensure your package list is up to date to avoid any issues with outdated packages

```
sudo apt-get update -y
sudo apt-get upgrade -y
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FMObrQTXtMWWPV9oqhYe9%2Fimage.avif?alt=media&#x26;token=fe9b2ab4-1eb9-4459-a1e7-695f4cb685aa" alt=""><figcaption></figcaption></figure>

## Installing MariaDB

Install the MariaDB package by running the following command

```
sudo apt-get install mariadb-server -y
```

### Start and Enable MariaDB

After installation, the MariaDB service starts and enables

Once the MariaDB is installed, start the MariaDB server

```
sudo systemctl start mariadb
```

Enable it to start when the system rebooted

```
sudo systemctl enable mariadb
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F7eVMXzZYOIY3Gt8aO34N%2Fimage.png?alt=media&#x26;token=7d25bbf4-7cbf-49f6-bd2c-af85f8b4486d" alt=""><figcaption></figcaption></figure>

### To check the status&#x20;

Verify that the database service is active and running. Run the following command

```
sudo systemctl status mariadb
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2Fsl5GofDQjqt6wPMywrDa%2Fimage.png?alt=media&#x26;token=0b07c340-84ac-4caa-b746-b9ee6c336dbb" alt=""><figcaption></figcaption></figure>

### Network status of the MariaDB Service

The network status of the MariaDB service can also be checked by running the `ss` command at the terminal prompt

```
sudo ss -antpl | grep mariadb
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2Fof8CHw9uNeNxyhT9LkTh%2Fimage.png?alt=media&#x26;token=cd8b1dc9-182d-43b8-9450-7e6eb2b14fc0" alt=""><figcaption></figcaption></figure>

## Configure MariaDB

Running the mysql\_secure\_installation script is advisable to set up your database security levels and access privileges.

```
sudo mysql_secure_installation
```

To secure MariaDB and access it, we must use the current credentials for login, and password required for the root user. If you recently set up MariaDB and If you haven't created the root password, simply hit the enter key at this point.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FOkZYciA2xYaG9vE9iFCB%2Fimage.png?alt=media&#x26;token=97397d7b-29cf-45e1-9c5b-477a391eaa2e" alt=""><figcaption></figcaption></figure>

Setting the root password or using the unix\_socket, You already have your root account protected, so you can safely answer 'n'.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FhJ3DprJHRlBUTnL4tO89%2Fimage.png?alt=media&#x26;token=7f3aac0e-7de8-45d1-986a-a9f55bafe3f0" alt=""><figcaption></figcaption></figure>

You set the password of your root account and the password should be 8 to 10-bit.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F3HBgRpqKDKgh66tQXFlC%2Fimage.png?alt=media&#x26;token=c2a60558-13d6-4183-b0e5-be4543667345" alt=""><figcaption></figcaption></figure>

By default, MariaDB installation has an anonymous user, if you want to remove the anonymous user press 'Y' otherwise and click 'n'.

if you disallow root login remotely press Y and otherwise click enter.

By Default. MariaDB comes with a database named 'test' that anyone can access. if you want to remove the test database press 'Y', otherwise 'n'.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FwpLcTkOBf6EU2eFab0I1%2Fimage.png?alt=media&#x26;token=bebb8069-45e8-4532-9e2c-fbc164c29d0c" alt=""><figcaption></figcaption></figure>

All done! If you've completed all of the above steps, your MariaDB installation should now be secure. you can proceed to reopen MariaDB and revert the root user's authentication method to the default, auth\_socket. To verify your identity as the main MySQL user with a password, execute this command

```
sudo mysql -u root -p
```

After executing the commands, please enter your password

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FR9Ukb4pwGvykxYFiNeXK%2Fimage.png?alt=media&#x26;token=45d22bd2-19f9-4900-bf50-75eaa779c9e1" alt=""><figcaption></figcaption></figure>

### Create a MariaDB User <a href="#ftoc-heading-2" id="ftoc-heading-2"></a>

To create a new MariaDB user, type the following command in the MariaDB console just like&#x20;

**\[MariaDB (none)]>**

```
CREATE USER 'username'@localhost IDENTIFIED BY 'password1';
```

* **Grant Privileges to MariaDB User**

Once you have set up a new user, you can give them the necessary permissions.

```
GRANT ALL PRIVILEGES ON *.* TO 'username'@localhost IDENTIFIED BY 'password1';
```

### Creating a MariaDB Database

To create a database in MariaDB, follow these steps:

{% hint style="info" %}
Log into MariaDB with the command `mysql -u username -p`. Replace `username` with your MariaDB username. You'll be prompted to enter your password.
{% endhint %}

{% hint style="info" %}
Once logged in, create a new database by executing `CREATE DATABASE database_name;`. Replace `database_name` with your desired database name.
{% endhint %}

{% hint style="info" %}
To verify the database was created, use `SHOW DATABASES;`. Your new database should appear in the list. To create a new database, run the following command from your MariaDB console just like MariaDB \[(none)]>
{% endhint %}

```
CREATE DATABASE database_name;
```

* **User and database permission**

Once you have set up a new user and database, you can give them the necessary permissions.

```
GRANT ALL PRIVILEGES ON database_name.*  TO  'username'@'%';
```

Now, review MariaDB configurations.

```
sudo mysql -u root -p 
```

Now check the status databases to run these command&#x20;

```
show databases;
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FVR6ixf8UtojIVUFQyies%2Fimage.png?alt=media&#x26;token=5d0b55ff-57db-4b36-8bb8-16b69b2734f2" alt=""><figcaption></figcaption></figure>
