Install MariaDB or MySQL on Linux

Get started using an open source SQL database on your Linux system.
55 readers like this.
Person standing in front of a giant computer screen with numbers, data

Both MariaDB and MySQL are open source databases that use SQL and share the same original codebase. MariaDB is a drop-in replacement for MySQL, so much so that you use the same command (mysql) to interact with MySQL and MariaDB databases. This article, therefore, applies equally to MariaDB and MySQL.

Install MariaDB

You can install MariaDB using your Linux distribution's 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:

$ sudo dnf install mariadb mariadb-server

On Debian, Ubuntu, Elementary, or similar:

$ sudo apt install mariadb-client mariadb-server

Other systems may package MariaDB differently systems, so you may need to search your software repository to learn how your distribution's maintainers provide it.

Start MariaDB

Because MariaDB is designed to function, in part, as a database server, it can run on one computer and be administered from another. As long as you have access to the computer running it, you can use the mysql command to administer the database. I ran MariaDB on my local computer when writing this article, 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 the 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:

$ grep mysql /etc/group

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 uses mysql, initialize the database environment:

$ sudo mysql_install_db --user=mysql
Installing MariaDB/MySQL system tables in '/var/lib/mysql'...

The result of this step reveals the next tasks you must perform to configure MariaDB:

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:

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.

Start MariaDB using your distribution's init system:

$ sudo systemctl start mariadb

To enable the MariaDB server to start upon boot:

$ sudo systemctl enable --now mariadb

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

mysqladmin -u root password 'myreallysecurepassphrase'
mysqladmin -u root -h $(hostname) password 'myreallysecurepassphrase'

Finally, if you intend to use this installation on a production server, run the mysql_secure_installation command before going live.

What to read next
Seth Kenlon
Seth Kenlon is a UNIX geek, free culture advocate, independent multimedia artist, and D&D nerd. He has worked in the film and computing industry, often at the same time.

Comments are closed.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.