MariaDB

MariaDB

This article explains how to install and configure a MariaDB database, both in a web browser (with PhpMyAdmin) and in the command line. We shall describe the installation process based on Calculate Linux Scratch.

Installing and configuring MariaDB

Installation

emerge -av mariadb

Preliminary settings

First let us set the MariaDataBase root password. Remember this is not your system root password._

# emerge --config dev-db/mariadb
Configuring pkg...
 * Trying to get password for mysql 'root' user from 'mysql' section ...
 * Trying to get password for mysql 'root' user from 'client' section ...
 * Please provide a password for the mysql 'root' user now
 * or through the /root/.my.cnf file.
 * Avoid ["'\_%] characters in the password
    > choose a root password
 * Retype the password
    > reenter the password
 * Creating the mysql database and setting proper permissions on it ...
...

Commands

Start the service

/etc/init.d/mysql start

Stop the service

/etc/init.d/mysql stop

Restart the service

/etc/init.d/mysql restart

Add to autostart:

rc-update add mysql default

Using MariaDB in the command line

Connect to the database server

# mysql -u root -p
Enter password: enter the root password here
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 100985
Server version: 5.5.50-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>

View information on databases

List the existing databases:

MariaDB [(none)]> SHOW DATABASES;

View the database (wiki in our example) with options:

MariaDB [(none)]> SHOW CREATE DATABASE `wiki`;

Connect to a database:

MariаDB [(none)]> USE wiki;

Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariаDB [wiki]>

List all tables of the database:

MariаDB [wiki]> SHOW tables;

View a table:

MariаDB [wiki]> SELECT * FROM page;

h2. Add a database, a user and access privileges

Add a database with options:

MariаDB [(none)]> CREATE DATABASE `base` CHARACTER SET utf8 COLLATE utf8_general_ci;

Add a user:

MariаDB [(none)]> CREATE USER base_user@localhost IDENTIFIED BY 'ПАРОЛЬ';

Assign the user to the database:

MariaDB [(none)]> GRANT ALL PRIVILEGES ON base.* to base_user@localhost;

Flush privileges once all this done:

MariаDB [(none)]> FLUSH PRIVILEGES;

Quit MariaDB in the command line

To quit, use the 'exit' command:

MariаDB [(none)]> exit
Bye

Backup and restore

How-to While in command line, go to the backup folder used by MariaDB.

Create a dated backup copy of a database:

# mysqldump -u base_user -p base > base-dump_10092017.sql
Enter password: enter the user's password and hit Enter

Restore a backup copy of the database:

# mysql -u base_user -p base < base-dump_10092017.sql
Enter password: enter the user's password and hit Enter