Page cover

🐬MariaDB - Self Hosted database server

2024-04-18

MariaDB is an open-source relational database management system derived from MySQL, offering enhanced features and compatibility. It provides robust performance, security, and scalability for various applications, making it a popular choice in the database community.

You can host your very own central database server on your network, allowing all your applications to connect to one central database server.

The major applications I use are WordPress and Nextcloud. When deploying these applications on your server, you are required to connect to a local database on the same host. The database you connect to doesn't have to be on the same local host server and works great where applications are deployed in a shared environment.

For this tutorial, I will be using Ubuntu as my operating system on xneelo cloud. The steps will apply to any Debian-based Linux distro and can be performed on a cloud instance or physical hardware.

Step 1 - System Updates

Access your server on the command line and perform updates

sudo apt update -y

Apply the available updates

sudo apt dist-upgrade -y

Step 2 - Install Maria DB server

sudo apt install mariadb-server

Once the installation completes, secure the database by running the below command and following the instructions

sudo mysql_secure_installation

Step 3 - Set the Bind address

Navigate to the following directory and edit the file 50-server.cnf

Look for the bind-address section and change it to 0.0.0.0 to allow remote connections to the database.

Restart MariaDB to allow the changes to take effect

Confirm that MariaDB is running after the service restart

Create scripts to easily add users and delete users, and create a database backup

Step 1

Navigate to your home directory and create a folder named 'db_scripts'

Change directory into the folder

Using your text editor of choice, add the following scripts:

Add a new database and user

Add the below snippet of code to the file:

Save the file and exit your text editor

Delete an existing user

Add the below snippet of code to the file:

Create a script to backup databases

Add the below snippet of code to the file:

Test the Scripts

CREATE A NEW DATABASE AND USER

You should see the below available options:

DELETE AN EXISTING DATABASE AND USER

You should see the below output:

CREATE A DATABASE DUMP BACKUP:

You should see the below output:

All database backups completed successfully.

The script is set to first create a directory for the backup files and then dump the backup files in the directory /usr/databases/mysql_dumps. This can be changed in the bash script as desired.

Summary: You have successfully configured MariaDB and added bash scripts to interact with the database.

Instead of manually running the scripts from the 'db_scripts' directory, we can create an alias in the server .bashrc file to allow the scripts to be run from anywhere on the server

To do this, first change the permissions of the files

Now EDIT the .bashrc file

Add the following aliases at the end of the file

Remember to update the paths in the script to point to the directory where you saved the scripts

restart the .bashrc file

You will now be able to run the scripts via the aliases set in the .bashrc file from anywhere on the server.

Last updated