Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

mysql

The MySQL / MariaDB client and mysqldump: run SQL, manage users and grants, back up and restore databases.

Level
DevSecOps service
Group
Databases and messaging
Needs root
For changes
Package
mysql
Also called
mariadb, mysqldump

What it does

MySQL and its fork MariaDB (the default on RHEL: mariadb-server) run many web apps: WordPress, WooCommerce, ERPNext, Odoo add-ons, PHP apps. The mysql client (MariaDB also calls it mariadb) connects on port 3306 or a local socket. mysqldump (mariadb-dump) makes logical backups. mysql_secure_installation removes test users and the test database.

How to write it

mysql [-h HOST] [-u USER] [-p] [DB] [-e "SQL"]
mysqldump [OPTIONS] DB > FILE.sql

Main parts

Useful SQL

SHOW DATABASES; / SHOW TABLES;
List.
CREATE USER 'u'@'host' IDENTIFIED BY 'pw';
New user.
GRANT … ON db.* TO 'u'@'host';
Give rights.
SHOW GRANTS FOR 'u'@'host';
Show rights.
SHOW PROCESSLIST;
Running queries.
SHOW VARIABLES LIKE 'x%';
Settings.
FLUSH PRIVILEGES;
Reload grants (after direct table edits).

Options 8

Most used

Flags

-h HOST / -P PORT
Server.
-u USER / -p
User / ask for password.
-e "SQL"
Run and exit.
-B -N
Batch, no column names (scripts).
--ssl-mode=VERIFY_IDENTITY
Require TLS and check the name (MySQL).
--single-transaction
Consistent InnoDB dump without locks (mysqldump).
--routines --triggers --events
Dump code objects too.
--all-databases
Dump everything.

Examples 10

$ run as a normal user# run as root (or with sudo)

  1. 1Install and secure MariaDB on RHEL

    dnf install -y mariadb-server
    systemctl enable --now mariadb
    mysql_secure_installation
  2. 2Create a database and an app user

    mysql -u root -p
    MariaDB [(none)]> CREATE DATABASE shop CHARACTER SET utf8mb4;
    MariaDB [(none)]> CREATE USER 'shop'@'10.20.2.%' IDENTIFIED BY 'Ch4nge-me-2026';
    MariaDB [(none)]> GRANT SELECT, INSERT, UPDATE, DELETE ON shop.* TO 'shop'@'10.20.2.%';
  3. 3Check users and rights

    mysql -u root -p -e "SELECT user, host FROM mysql.user;"
    mysql -u root -p -e "SHOW GRANTS FOR 'shop'@'10.20.2.%';"
  4. 4Back up without locking the site

    mysqldump -u backup -p --single-transaction --routines --triggers shop | gzip > shop-$(date +%F).sql.gz
  5. 5Restore

    gunzip < shop-2026-10-27.sql.gz | mysql -u root -p shop
  6. 6Find slow or stuck queries

    mysql -u root -p -e "SHOW FULL PROCESSLIST;" | awk '$6 > 30'
  7. 7Database sizes

    mysql -u root -p -e "SELECT table_schema, ROUND(SUM(data_length+index_length)/1024/1024,1) AS mb FROM information_schema.tables GROUP BY table_schema ORDER BY mb DESC;"
  8. 8Keep the password out of the command line

    cat ~/.my.cnf
    [client]
    user=backup
    password=Example-Only-2026
    chmod 600 ~/.my.cnf
  9. 9Listen only on the private network

    cat /etc/my.cnf.d/bind.cnf
    [mysqld]
    bind-address=10.20.2.10
    systemctl restart mariadb
  10. 10Run in a container for a lab

    podman run -d --name db -e MARIADB_ROOT_PASSWORD=labonly -p 127.0.0.1:3306:3306 docker.io/library/mariadb:11.4

Watch out

  • -pPASSWORD on the command line is visible in ps and shell history; use -p or ~/.my.cnf.
  • Never open 3306 to the internet; allow only app servers.
  • GRANT ALL ON *.* for an app user is a full takeover risk.

Files

/etc/my.cnf, /etc/my.cnf.d/
Server and client settings.
/var/lib/mysql/
Data files.
/var/log/mariadb/mariadb.log
Log (RHEL).

Tips to remember

  • For WordPress/WooCommerce sites, a nightly mysqldump --single-transaction plus off-site copies is the minimum.

Exit codes

0Success.
1Error (mysql) / wrong option (mysqldump).
2Server error during the dump (mysqldump).