Skip to content

apankov/_db

Repository files navigation

_db

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.

Initialization

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.

Backends

The tools auto-detect how to reach MySQL based on db.conf:

  • Local service — leave both DOCKER_* values empty. The local mysql client connects to DBHOST/DBPORT.

  • Docker container — set DOCKER_MYSQLD_CONTAINER to the container name (e.g. mysqld). The client is run inside the container via docker exec, so keep DBHOST=localhost.

  • Docker Compose — set DOCKER_COMPOSE_SERVICE to the service name. The v2 docker compose CLI is auto-detected, falling back to legacy docker-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.

Tools description

  • 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, or dump.sql otherwise)

  • dbdump.sh - calls mysqldump, writes to the file passed as first argument or dump.sql otherwise

  • 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

Usage examples

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

Credits

To all my friends who proved usefulness of this set by daily usage.

About

Simple set of MySQL cli tools

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages