cheat-sheets

MariaDB Cheat-Sheet

MariaDB is a community fork of MySQL. Day-to-day SQL, backup and user commands are the same, see MySQL. General SQL: SQL.

Install MariaDB on Ubuntu #

bash
sudo apt update
sudo apt install mariadb-server
sudo mysql_secure_installation

Check it runs: sudo systemctl status mariadb and sudo mariadb.

Access Database from outside #

Open /etc/mysql/mariadb.conf.d/50-server.cnf and change the bind-address to:

bind-address = 0.0.0.0

Restart with sudo systemctl restart mariadb, then open the firewall only to trusted hosts (sudo ufw allow from <CIDR> to any port 3306, see UFW (uncomplicated firewall)). Prefer a specific interface address over 0.0.0.0.

Create Administrative User #

  1. Create a new user newuser for the host localhost with a new password:
mysql
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
  1. Grant all permissions to the new user
mysql
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';

Use ON <db>.* instead of *.* for an application user, and add WITH GRANT OPTION only for real admins.

  1. Update permissions
mysql
FLUSH PRIVILEGES;

Maintenance #

COMMANDDESCRIPTION
sudo mariadbRoot login through the unix socket
mariadb-admin statusUptime and thread count
mariadb-admin processlistRunning queries
mariadb-dump --single-transaction --all-databases > all.sqlFull logical backup
mariadb-check --all-databases --auto-repairCheck and repair tables
mariadb-upgradeRun after a major version upgrade
SELECT VERSION();Server version
SHOW GLOBAL STATUS LIKE 'Threads_connected';Connections now
SET GLOBAL slow_query_log=1; SET GLOBAL long_query_time=1;Log queries slower than 1 s

Run with Docker #

bash
docker run -d --name mariadb -e MARIADB_ROOT_PASSWORD=<PASSWORD> \
  -v mariadb-data:/var/lib/mysql -p 127.0.0.1:3306:3306 mariadb:11

Users and Authentication #

SQLDESCRIPTION
CREATE USER 'app'@'192.168.1.%' IDENTIFIED BY '<PASSWORD>';App user limited to the LAN
GRANT SELECT, INSERT, UPDATE, DELETE ON app.* TO 'app'@'192.168.1.%';Least privilege
CREATE USER 'admin'@'localhost' IDENTIFIED VIA unix_socket;Log in as the matching OS user, no password
ALTER USER 'root'@'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('<PASSWORD>');Give root a password (default on Debian/Ubuntu is socket authentication)
CREATE ROLE reader; GRANT SELECT ON app.* TO reader; GRANT reader TO 'alice'@'%'; SET DEFAULT ROLE reader FOR 'alice'@'%';Roles
SHOW GRANTS FOR 'app'@'192.168.1.%';Privileges
SELECT user, host, plugin FROM mysql.user;List users (a view over mysql.global_priv in 10.4+)
SET PASSWORD FOR 'app'@'%' = PASSWORD('<NEWPASS>');Change a password
DROP USER 'app'@'%';Remove

On Debian/Ubuntu sudo mariadb works for root through the unix socket, a password is not needed and a remote root login is not possible. Create a separate admin user for remote use, see MySQL.

Indexes, EXPLAIN and Slow Queries #

Index and query tuning follow MySQL. MariaDB specifics:

SQLDESCRIPTION
EXPLAIN SELECT ...;Plan (EXPLAIN EXTENDED and FORMAT=JSON also work)
ANALYZE SELECT ...;Runs the query and shows estimated and actual rows. MariaDB’s counterpart to EXPLAIN ANALYZE
ANALYZE FORMAT=JSON SELECT ...;Detailed timing
SHOW INDEX FROM users;List indexes
SELECT * FROM information_schema.INDEX_STATISTICS;Index use (needs userstat=1)
SET GLOBAL userstat=1;Enable table, index and user statistics
SET GLOBAL slow_query_log=1; SET GLOBAL long_query_time=1;Slow query log
SET GLOBAL log_slow_verbosity='query_plan,explain';Add plans to the slow log
ANALYZE TABLE t PERSISTENT FOR ALL;Engine-independent statistics

Summarise the slow log: mariadb-dumpslow -s t /var/log/mysql/mariadb-slow.log | head.

Tuning Basics #

In /etc/mysql/mariadb.conf.d/50-server.cnf under [mysqld], then sudo systemctl restart mariadb.

SETTINGSTARTING POINT
innodb_buffer_pool_size50-70% of RAM on a dedicated host
innodb_log_file_size512M - 2G for write-heavy workloads
innodb_flush_log_at_trx_commit1 safe, 2 faster with small risk
innodb_flush_methodO_DIRECT
max_connections150 - 300
character-set-server / collation-serverutf8mb4 / utf8mb4_unicode_ci
thread_handlingpool-of-threads for many short connections
query_cache_type0. The query cache is mostly a bottleneck on busy servers

Use MySQLTuner for suggestions. Check with SHOW GLOBAL VARIABLES LIKE '...'; and SHOW GLOBAL STATUS;.

Backup and Replication #

COMMANDDESCRIPTION
mariadb-dump --single-transaction --routines --triggers --events --all-databases > all.sqlLogical backup
mariadb-dump --single-transaction <DB> | gzip > db.sql.gzOne database, compressed
mariadb-backup --backup --target-dir=/backup/full --user=root --password=<PASSWORD>Physical hot backup (package mariadb-backup)
mariadb-backup --prepare --target-dir=/backup/fullPrepare it for restore
mariadb-backup --copy-back --target-dir=/backup/fullRestore into an empty data dir (stop the server first, then chown -R mysql:mysql)

GTID replication, monitoring lag and failover are in MySQL and MariaDB Replication. Galera Cluster gives multi-primary replication: see the MariaDB docs.

Used as the external datastore for K3s HA in Install K3S in High Availability Mode. MariaDB 10.5+ also ships mariadb* command names, with mysql* kept as links.

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.