en:server:services:mariadb

MariaDB

MariaDB is a community-developed, commercially supported fork of the MySQL relational database management system (RDBMS), intended to remain free and open-source software under the GNU General Public License. Development is led by some of the original developers of MySQL, who forked it due to concerns over its acquisition by Oracle Corporation in 2009.

MariaDB is intended to maintain high compatibility with MySQL, with library binary parity and exact matching with MySQL APIs and commands, allowing it in many cases to function as drop-in replacement for MySQL. However, new features are diverging. It includes new storage engines like Aria, ColumnStore, and MyRocks.

Its lead developer/CTO is Michael “Monty” Widenius, one of the founders of MySQL AB and the founder of Monty Program AB. On 16 January 2008, MySQL AB announced that it had agreed to be acquired by Sun Microsystems for approximately $1 billion. The acquisition completed on 26 February 2008. Sun was then bought the following year by Oracle Corporation. MariaDB is named after Widenius' younger daughter, Maria. (MySQL is named after his other daughter, My.)

pacman -S mariadb
mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
systemctl enable --now mariadb.service

Set the password for the mysql root user, and most questions can be answered yes, for example, if you do not need remote access.

mysql_secure_installation
mysql -u root -p
CREATE USER 'user_name'@'localhost' IDENTIFIED BY 'mypassword';
FLUSH PRIVILEGES;
EXIT;
mysql -u root -p
CREATE USER 'user_name'@'%' IDENTIFIED BY 'mypassword';
FLUSH PRIVILEGES;
EXIT;
mysql -u root -p
CREATE DATABASE db_name;
GRANT ALL ON db_name.* to 'user_name'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;
\q

or something more detailed

GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, INDEX, DROP, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON db_name_here.* TO 'myuser'@'localhost';
mysql -u root -p
SHOW DATABASES;
DROP DATABASE db_name;
FLUSH PRIVILEGES;
EXIT;
mysql -u root -p
SELECT User FROM mysql.user;
DROP USER user_name@localhost;
FLUSH PRIVILEGES;
EXIT;
SET PASSWORD FOR 'user_name'@'localhost' = PASSWORD('password');
FLUSH PRIVILEGES;
EXIT;

You can dump all databases with the following command mysqldump -u root -p --all-databases | gzip > all_databases.sql.gz or just one single database mysqldump -u root -p –databases database_name | gzip > database_name.sql.gz.

Or use the following script, which contains:

  • separate databases (when you extract the file)
  • define after how many days the old backups should be deleted
  • email notification
#!/bin/bash
# Shell script to backup MySQL database

# Set these variables
MyUSER="my_user"             # DB_USERNAME
MyPASS="mypassword"          # DB_PASSWORD
MyHOST="localhost"           # DB_HOSTNAME

# Backup Dest directory
DEST="/path/to/backup/mysql/"

# Email for notifications
# EMAIL="email"
# How many days old files must be to be removed
DAYS="60"

# Linux bin paths
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"

# Get date in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y_%H-%M")"

# Create Backup sub-directories
MBD="$DEST/$NOW/mysql"
install -d $MBD

# DB skip list
SKIP="information_schema
performance_schema"

# Get all databases
DBS="$($MYSQL -h $MyHOST -u $MyUSER -p$MyPASS -Bse 'show databases')"

# Archive database dumps
for db in $DBS
do
    skipdb=-1
    if [ "$SKIP" != "" ];
    then
                for i in $SKIP
                do
                        [ "$db" == "$i" ] && skipdb=1 || :
                done
    fi
 
    if [ "$skipdb" == "-1" ] ; then
        FILE="$MBD/$db.sql"
        $MYSQLDUMP -h $MyHOST -u $MyUSER -p$MyPASS $db > $FILE
    fi
done

# Archive the directory, send mail and cleanup
cd $DEST
tar -cf $NOW.tar $NOW
$GZIP -9 $NOW.tar

#echo -e "Subject: MySQL backup $(hostname)\r\n\r\nMySQL backup is completed! Backup name is $NOW.tar.gz" | msmtp -a default $EMAIL
rm -rf $NOW

# Remove old files
find $DEST -mtime +$DAYS -exec rm -f {} \;

Use crontab for automation.

mysql -u root -p db_name < database.sql

Upon a major version release of mariadb (for example mariadb-10.7.4-1 to mariadb-10.8.3-1), it is wise to upgrade databases:

  1. upgrade the package(s)
  2. restart the daemon
  3. run mariadb-upgrade
systemctl restart mariadb.service
mariadb-upgrade -u root -p

To check all tables in all databases:

mariadb-check --all-databases -u root -p -c

To analyze all tables in all databases:

mariadb-check --all-databases -u root -p -a

To repair all tables in all databases:

mariadb-check --all-databases -u root -p -r

To optimize all tables in all databases:

mariadb-check --all-databases -u root -p -o

All “Check/update database” commands in one block

systemctl restart mariadb.service
mariadb-upgrade -u root -p
mariadb-check --all-databases -u root -p -c
mariadb-check --all-databases -u root -p -a
mariadb-check --all-databases -u root -p -r
mariadb-check --all-databases -u root -p -o
  • en/server/services/mariadb.txt
  • Last modified: 2023/02/14 21:42
  • by dan