= Install MariaDB on Linux

:Author: Seth Kenlon
:Email: <seth@opensource.com>
:Revision: 1.0


== Installing MariaDB

MariaDB is a drop-in replacement for MySQL, so much so that you use the same command (`mysql`) to interact with both a MySQL and a MariaDB database.
Both are databases using SQL, and both share the same original code base.
This article is, therefore, equally relevant to MariaDB and MySQL.

Install MariaDB using your package manager.
On most distributions, MariaDB is split into a server package and a client package.
The server package provides the database "engine", the part of MariaDB that runs (usually on a physical server) in the background, listening for data input or requests for data output.
The client package provides the command `mysql`, which you can use to communicate with the server.

On RHEL, Fedora, CentOS, or similar:

----[source,bash]
$ sudo dnf install mariadb mariadb-server
----

On Debian, Ubuntu, Elementary, or similar:

----[source,bash]
$ sudo apt install mariadb-client mariadb-server
----

On other systems, MariaDB may be packaged differently, so you may need to search your software repository to learn how MariaDB is provided by your distribution's maintainers.

== Starting MariaDB

Because MariaDB is designed to function, in part, as a database server, it can run on one computer and administered from another.
As long as you have access to the computer running it, you can use the `mysql` command to administer the database.
For the purpose of this article, I ran MariaDB on my local computer, but it's just as likely that you'll interact with a MariaDB database hosted on a remote system.

Before starting MariaDB, you must create an initial database.
You should define what user you want MariaDB to use when initializing its file structure.
By default, MariaDB uses the current user, but you probably want it to use a dedicated user account.
Your package manager probably configured a system user and group for you.
Use `grep` to find out whether there's a `mysql` group:

----[source,bash]
$ grep mysql /etc/group
mysql:x:27:
----

You can also look in `/etc/passwd` for a dedicated user, but usually where there's a group there's also a user.
If there isn't a dedicated `mysql` user and group, look through `/etc/group` for an obvious alternative (such as `mariadb`).
Failing that, read your distribution's documentation to learn how MariaDB runs.

Assuming your install does use `mysql`, initialize the database environment:

----[source,bash]
$ sudo mysql_install_db --user=mysql
Installing MariaDB/MySQL system tables in '/var/lib/mysql'...
OK
[...]
----

The results of this step reveals the next tasks you must perform to get MariaDB configured.

----[source,bash]
PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:

'/usr/bin/mysqladmin' -u root password 'new-password'
'/usr/bin/mysqladmin' -u root -h $(hostname) password 'new-password'

Alternatively you can run:
'/usr/bin/mysql_secure_installation'

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.
----

First, start MariaDB using your distribution's init system:

----[source,bash]
$ sudo systemctl start mariadb
----

To enable the MariaDB server to start upon boot:

----[source,bash]
$ sudo systemctl enable --now mariadb
----

Now that you have a MariaDB server to communicate with, set a password for it:

----[source,bash]
mysqladmin -u root password 'myreallysecurepassphrase'
mysqladmin -u root -h $(hostname) password 'myreallysecurepassphrase'
----

If you intend to use this install on a production server, run the `mysql_secure_installation` command before going live.

