admin 1 year ago
commit
399787aa87
7 changed files with 75 additions and 0 deletions
  1. 1 0
      .env.template
  2. 3 0
      .gitignore
  3. 19 0
      README.md
  4. 14 0
      docker-compose.yml
  5. 24 0
      utils/create_db.sh
  6. 8 0
      utils/delete_db.sh
  7. 6 0
      utils/show_db.sh

+ 1 - 0
.env.template

@@ -0,0 +1 @@
+MYSQL_ROOT_PASSWORD=PutYourPasswordHere

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+data/
+.env
+utils/*__db.conf

+ 19 - 0
README.md

@@ -0,0 +1,19 @@
+### mariadb
+
+#### Setup
+Setup the DB Root Password
+   - `cp .env.template .env && vi .env`
+   - The root password in `.env` gets stored in the DB during the intial container deployment.
+     Changes of the file in a later stage have no effect.
+   - The password can be changed later on only using `mysqladmin' -u root password 'new-password'`.
+
+
+#### Maintenance
+- **Create a new DB** by running `.utils/create_db.sh <database_name>`
+  - `<database_name>` is the name and user of the new DB
+  - The password is set randomly and stored with further details in `db_defintion__<database_name>.txt`.
+- **List DBs** by running `./utils/db_names.sh`
+- **Delete a DB** by running `.utils/delete_db.sh <database_name>`
+- **For backup** see the container db_sqldump
+- **Upgrade** `docker exec db sh -c 'mariadb-upgrade -p$MYSQL_ROOT_PASSWORD`
+- **Restore** `docker exec db sh -c 'mysql -p$MYSQL_ROOT_PASSWORD < /mnt/backup/backupfile.sql'`

+ 14 - 0
docker-compose.yml

@@ -0,0 +1,14 @@
+version: '3.8'
+
+services:
+   db:
+     image: mariadb:10.6 # pinned to stable release
+     container_name: db
+     volumes:
+       - ./data/mysql:/var/lib/mysql/
+       - ./data/backup:/mnt/backup
+     restart: on-failure:3
+
+networks:
+   db_default:
+     external: true

+ 24 - 0
utils/create_db.sh

@@ -0,0 +1,24 @@
+#!/bin/bash -xe
+# Create new DB in dockernized sql db
+
+[ $# -eq 0 ] && { echo "Usage: $0 <database_name>"; exit 1; }
+
+# Remove special characters and make db / username lower case
+DB_NAME=`echo $1 | tr -dc '[:alnum:]\n\r' | tr '[:upper:]' '[:lower:]'`
+
+# Source db root pass
+source /opt/docker/db/MYSQL_ROOT_PASSWORD.env
+
+# Generate password for the new db
+NEW_DB_PASS=`apg -n 1 -m 32 -M CN`
+
+# Create db, corresponding user and grant privileges
+docker exec db mysql -p$MYSQL_ROOT_PASSWORD --execute "CREATE DATABASE $DB_NAME; \
+GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, DROP, INDEX ON $DB_NAME.* TO \"$DB_NAME\"@\"%\" IDENTIFIED BY \"$NEW_DB_PASS\"; \
+FLUSH PRIVILEGES;"
+
+# Create conf file to be source by other scripts
+echo "DB_FOR=$1" >> $1__db.conf 
+echo "DB_NAME=$DB_NAME" >> $1__db.conf
+echo "DB_USER=$DB_NAME" >> $1__db.conf
+echo "DB_PASS=$NEW_DB_PASS" >> $1__db.conf

+ 8 - 0
utils/delete_db.sh

@@ -0,0 +1,8 @@
+#!/bin/bash -ex
+# Create new DB in dockernized sql db
+
+[ $# -eq 0 ] && { echo "Usage: $0 <database_name>"; exit 1; }
+
+source /opt/docker/db/MYSQL_ROOT_PASSWORD.env
+
+docker exec db mysql -p$MYSQL_ROOT_PASSWORD --execute "DROP DATABASE $1;"

+ 6 - 0
utils/show_db.sh

@@ -0,0 +1,6 @@
+#!/bin/bash -ex
+# Create new DB in dockernized sql db
+
+source /opt/docker/db/MYSQL_ROOT_PASSWORD.env
+
+docker exec db mysql -p$MYSQL_ROOT_PASSWORD --execute "show databases;"