brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: a [client] section in ~/.my.cnf keeps the database password off your command line

Database
~/tips/tip-database-my-cnf-client-password

cat > ~/.my.cnf <<'EOF'

Stores the client login in a private file so commands and cron jobs need no -p.

Typing -pSecret works, but the password then sits in your shell history and, while the command runs, in the process list where other users can read it.

The MariaDB and MySQL clients read option files, and the [client] group applies to all of them:

cat > ~/.my.cnf <<'EOF'
[client]
user=backup
password=change-me
EOF
chmod 600 ~/.my.cnf

After that, mariadb shop and mariadb-dump shop log in without asking. To see what a client would pick up, run mariadb --print-defaults; it prints the options it found, password included, so do that only on your own screen.

For a cron job I prefer a separate file for that one task and point to it explicitly:

mariadb-dump --defaults-extra-file=/etc/backup/db.cnf --databases shop > shop.sql

Two details matter. The chmod 600 is not optional, since the file holds a plain text password, and the client ignores an option file that everyone can write to, with only a warning. And --defaults-extra-file has to be the very first option on the line, or the client complains. Give that backup user only the rights a dump needs, not root.

Further reading: Configuring MariaDB with option files

More Database tips mariadb -e 'CREATE DATABASE IF NOT EXISTS blog' mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql SELECT table_schema, table_name, CHECKSUM TABLE shop.orders; services: All Database tips (53) All topics

Link