Simple set of MySQL cli tools for quick manual database operations.
Works whether MySQL runs locally (as a service), in a docker run container,
or as a docker compose service.
Copy _db into some place (usually inside your project directory) or clone it from GitHub:
git clone https://github.com/apankov/_db.git
Create the config by copying the provided template:
cp db.conf.example db.conf
Edit db.conf and fill in your credentials.
The tools auto-detect how to reach MySQL based on db.conf:
-
Local service — leave both
DOCKER_*values empty. The localmysqlclient connects toDBHOST/DBPORT. -
Docker container — set
DOCKER_MYSQLD_CONTAINERto the container name (e.g.mysqld). The client is run inside the container viadocker exec, so keepDBHOST=localhost. -
Docker Compose — set
DOCKER_COMPOSE_SERVICEto the service name. The v2docker composeCLI is auto-detected, falling back to legacydocker-compose.
The password is passed to the client via the MYSQL_PWD environment variable,
so it never appears in ps/shell history and there is no "insecure password"
warning.
-
dbquery.sh- run an ad-hoc query from an argument, a file, or stdin -
dbconsole.sh- opens an interactive mysql console -
dbpatch.sh- applies an SQL patch/migration file specified as first argument -
dbreset.sh- drops the database, recreates it, then loads a dump (the file passed as first argument, ordump.sqlotherwise) -
dbdump.sh- callsmysqldump, writes to the file passed as first argument ordump.sqlotherwise -
dbbackup.sh- dumps and bzip2s the database; filename embeds the datetime (%Y-%m-%d_%H-%M-%S) -
diffs/- suggested place to keep db patch files -
dump.sql- default dump file
Run a query (works on every backend):
./dbquery.sh "select count(*) from users"
cat report.sql | ./dbquery.sh
./dbquery.sh -f report.sql
Open a console:
./dbconsole.sh
Apply a migration:
./dbpatch.sh diffs/migration-users-roles.sql
Dump / reset / backup:
./dbdump.sh
./dbreset.sh dump.sql
./dbbackup.sh
To all my friends who proved usefulness of this set by daily usage.