Tech:MariaDB

MariaDB is the chosen database software used in production. Currently we run 10.4.x on database servers.

Configuration
Database configuration is in Puppet.

Master-replica replication
As of January 2021, we are using replication for database backups (dbbackup1).

Dump data from master to slave
mariabackup allows you to create a point-in-time dump of the database cluster. In this case, data is streamed to another server (destination) and extracted in /home/dbcopy/backup-db6-10april. The destination directory must exist and be writable before the command is executed!

In this example, we're going to clone db12 (c3) to dbbackup1. dbbackup1 is a server running multiple instances of MariaDB (multi-instance setup).

mariabackup --backup --slave-info --safe-slave-backup --stream=xbstream | ssh -i /home/dbcopy/.ssh/id_ed25519 dbcopy@dbbackup1.miraheze.org "mbstream -x -C /home/dbcopy/backup-db12-10january2020/" (the directory at the destination must be created before, please specify the correct date as well)

At the destination, the backup must be prepared, since the data is not consistent yet. You can do this using mariabackup: mariabackup --prepare --open-files-limit=900000 --target-dir=/home/dbcopy/backup-db12-10january2020 --use-memory=2G

Finally, copy the directory to the appropriate location in /srv: systemctl stop mariadb@c3 rm -rf /srv/mariadb.c3 mv /home/dbcopy/backup-db12-10january2020 /srv/mariadb.c3 chown -R mysql:mysql /srv/mariadb.c3 systemctl start mariadb@c3

Replicate from master
You need to change,  ,   and. Never set up replication without TLS and never disable verification.

Look at the  file fir the binlog positions. The first field (e.g. ) is the value for , the second field is  ) (large digit number). The third field is a global transaction ID, but we don't use GTID for replication yet.

CHANGE MASTER TO   MASTER_HOST=" .miraheze.org", MASTER_PORT=3306, MASTER_USER="replica", MASTER_PASSWORD="REDACTED", MASTER_LOG_FILE='MYSQLBINLOG', MASTER_LOG_POS=POSITION, MASTER_SSL=1, MASTER_SSL_CA='/etc/ssl/certs/Sectigo.crt', MASTER_SSL_VERIFY_SERVER_CERT=1; START SLAVE;

Revoke Grants / Drop User
To revoke grants do the following:

Example:
 * 1) Run
 * 2) Run
 * 3) Run

To drop a user: Example:
 * 1) Run

Restoring Database from a backup
To restore a database from a .idb file following these steps.


 * 1) Follow Restore backup first. (Make sure the backup is not a diff rather it needs to be a full backup).
 * 2) Setup mysql on test2 temporarily as we doin't want to use a production db system for this.
 * 3) Copy over the .idb files from db4 to test2.
 * 4) Create a fake db on test2 "create database testdb".
 * 5) Create the table matching the contents of the idb file.
 * 6) After creating the table, run "ALTER TABLE table_name DISCARD TABLESPACE;".
 * 7) Then copy the .idb file to /var/lib/mysql// .idb (the file name your copying should match the table name in /var/lib/mysql//).
 * 8) After that run "ALTER TABLE table_Name IMPORT TABLESPACE;".
 * 9) Now you can generate an sql file and import into db4.