Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

mysql

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

Niveau
DevSecOps-service
Groep
Databases en messaging
Root nodig
Voor wijzigingen
Pakket
mysql
Ook
mariadb, mysqldump

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

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.

Zo schrijf je het

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

Belangrijkste delen

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).

Opties 8

Meest gebruikt

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.

Voorbeelden 10

$ als gewone gebruiker# als root (of met 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

Let op

  • -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.

Bestanden

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

Tips om te onthouden

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

Exitcodes

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