Install MySQL MariaDB

Update apt packages

First use the apt-get alternative apt to update the packages index by typing:

sudo apt update

Install MariaDB

Once the packages list is updated, install MariaDB by running the following command:

sudo apt install mariadb-server

The MariaDB service will start automatically.

Verify MariaDB is running

sudo systemctl status mariadb

You should see something like:

● mariadb.service - MariaDB 10.3.23 database server
   Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2021-03-26 13:27:32 MDT; 2h 58min ago
     Docs: man:mysqld(8)
           https://mariadb.com/kb/en/library/systemd/
  Process: 379 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SUCCESS)
  Process: 384 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
  Process: 387 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $? -e
  Process: 513 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
  Process: 531 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
 Main PID: 439 (mysqld)
   Status: "Taking your SQL requests now..."
    Tasks: 30 (limit: 2354)
   Memory: 114.8M
   CGroup: /system.slice/mariadb.service
           └─439 /usr/sbin/mysqld

Mar 26 13:27:30 ral-itapps-dev systemd[1]: Starting MariaDB 10.3.23 database server...
Mar 26 13:27:31 ral-itapps-dev mysqld[439]: 2021-03-26 13:27:31 0 [Note] /usr/sbin/mysqld (mysqld 10.3.23-MariaDB-0+deb10u1) starting as proc
Mar 26 13:27:31 ral-itapps-dev mysqld[439]: 2021-03-26 13:27:31 0 [Warning] Could not increase number of max_open_files to more than 16364 (r
Mar 26 13:27:32 ral-itapps-dev systemd[1]: Started MariaDB 10.3.23 database server.
Mar 26 13:27:32 ral-itapps-dev /etc/mysql/debian-start[542]: Upgrading MySQL tables if necessary.
Mar 26 13:27:32 ral-itapps-dev /etc/mysql/debian-start[601]: Checking for insecure root accounts.

Use CTR+C to exit the status display.

Secure MariaDB

To improve the security of the MariaDB installation run the mysql_secure_installation script.

The script will prompt you to set a password for the root account, remove the anonymous user, restrict root user access to the local machine and remove the test database.

At the end the script will reload the privilege tables ensuring that all changes take effect immediately.

All steps are explained in detail and it is recommended to answer “Y” (yes) to all questions.

sudo mysql_secure_installation

Set the root password when prompted.

Login to MariaDB

With Root Password

To login, use sudo as the root user with a password:

sudo mysql -u root

Without Root Password

MariaDB for Debian uses by default the UNIX auth_socket plugin. This means that if you are a system root user (sudoer), you can login as root to mysql without a password. This technique was pioneered by Otto Kekäläinen in Debian MariaDB packages and has been successfully used in Debian since as early as MariaDB 10.0.

To login, use sudo as the root user without a password:

sudo mysql -u root

The user will be prompted to enter their own sudoers password to auth as root.

More Resources