DBTrail

Quick Start

From nothing to a heavy report answered from DBTrail's copy, in three steps you copy and paste in a terminal. No browser needed.

Three steps, all in a terminal. Copy each block, paste it, press Enter.

You need

  • A Linux machine with Docker with Compose and the mysql client. DBTrail runs there.
  • A MySQL login that can create users, for one step.
  • Row binlogs and a primary key on every InnoDB table. Step 2 checks them.
  • MySQL or Percona Server 8.0, 8.4 or 9.7 (not the 9.0 to 9.6 Innovation releases), or MariaDB 10.11 and newer.

Install DBTrail

the machine that will run DBTrail
curl -fsSL https://raw.githubusercontent.com/dbtrail/dbtrail/main/install.sh | sh
cd dbtrail

It waits until DBTrail answers. Ignore the browser page it may open.

Connect your MySQL

same terminal

Check the binlog. In mysql on your server (it is SQL), this must say ON, ROW and FULL:

SHOW VARIABLES WHERE Variable_name IN ('log_bin', 'binlog_format', 'binlog_row_image');
  • Not FULL? The fix.
  • RDS or Aurora: a parameter group sets them: read this first.
  • At the first connect, a table with no primary key stops recording for the whole server: find them.

Say where it is. Change the first three lines: server, database, and the login that can create users. The last two make passwords for DBTrail.

export MYSQL_HOST=db1.example.com
export MYSQL_DB=shop
export MYSQL_ADMIN=root
export DBTRAIL_PW=$(openssl rand -hex 16)
export FWD_PW=$(openssl rand -hex 16)

Keep this terminal open: every block below reads these lines.

Create DBTrail's users on your MySQL (what each one may do): dbtrail reads the binlog and tables; dbtrail_fwd runs quick lookups and what the copy cannot answer. It asks for the $MYSQL_ADMIN password.

mysql -h "$MYSQL_HOST" -u "$MYSQL_ADMIN" -p <<SQL
CREATE USER 'dbtrail'@'%' IDENTIFIED BY '$DBTRAIL_PW';
GRANT REPLICATION SLAVE, REPLICATION CLIENT, SELECT ON *.* TO 'dbtrail'@'%';
GRANT RELOAD, BACKUP_ADMIN, PROCESS, SHOW VIEW ON *.* TO 'dbtrail'@'%';
CREATE USER 'dbtrail_fwd'@'%' IDENTIFIED BY '$FWD_PW';
GRANT SELECT ON $MYSQL_DB.* TO 'dbtrail_fwd'@'%';
SQL
  • MariaDB: leave out BACKUP_ADMIN. RDS or Aurora: another line for RELOAD.
  • App writes through DBTrail? Give dbtrail_fwd its grants instead of SELECT (the risk).

MySQL on this same machine? Without these two, server add stops with connect: connection refused. They change MySQL's config to listen on every address, and restart it, offline for a few seconds (what changes).

export MYSQL_HOST=host.docker.internal
printf '[mysqld]\nbind-address = 0.0.0.0\n' | sudo tee /etc/mysql/mysql.conf.d/zz-dbtrail.cnf
sudo systemctl restart mysql

Connect it to DBTrail, through a file only you can read, deleted after.

cat > dbtrail-server.cnf <<EOF
[client]
host=$MYSQL_HOST
user=dbtrail
password=$DBTRAIL_PW

[dbtrail-forward]
user=dbtrail_fwd
password=$FWD_PW
EOF
chmod 600 dbtrail-server.cnf
./dbtrail server add --defaults-file dbtrail-server.cnf --name "$MYSQL_DB"
rm dbtrail-server.cnf

It checks your MySQL, says what to fix, starts recording changes, and prints the mysql line and password of DBTrail's MySQL port.

server add checks MySQL line by line, starts recording changes, and prints the mysql line for DBTrail's port with its password.
You should see

Keep that password: it is shown once. ./dbtrail port new-password makes another.

Make the first copy.

./dbtrail snapshot run "$MYSQL_DB"
./dbtrail snapshot schedule "$MYSQL_DB" --every 1h

The first line reads every table once. Writes may wait a moment as it starts: pick a quiet time (why). On MariaDB, not while an ALTER TABLE runs (why). The second keeps the copy current from the recorded changes.

Run a heavy report

the DBTrail machine, same terminal
mysql -h 127.0.0.1 -P 3309 -u "$MYSQL_DB" -p "$MYSQL_DB"

Paste the password from step 2. Then a total over one of your biggest tables:

SELECT status, count(*) FROM orders GROUP BY status;
The query over 10 million orders, answered by DBTrail's copy in 0.74 seconds.

The copy answers it, without loading your MySQL. To see who answered, right after:

SHOW ROUTER STATUS;

last_answer says copy.

SHOW ROUTER STATUS: last_answer is copy.

In a browser instead

Same steps at http://127.0.0.1:8090: pick a username and password, and the form to add your server opens.

Next

On this page