Optimized mariadb

This commit is contained in:
Kevin Veen-Birkenbach 2025-07-14 10:26:12 +02:00
parent 8161dd1b6d
commit 81ef808191
No known key found for this signature in database
GPG Key ID: 44D8F11FD62F878E
4 changed files with 59 additions and 44 deletions

View File

@ -1,4 +1,8 @@
version: "latest"
hostname: "svc-db-mariadb" hostname: "svc-db-mariadb"
network: "<< defaults_applications[svc-db-mariadb].hostname >>" network: "<< defaults_applications[svc-db-mariadb].hostname >>"
volume: "<< defaults_applications[svc-db-mariadb].hostname >>_data" docker:
services:
mariadb:
version: "latest"
image: "mariadb"
volume: "<< defaults_applications[svc-db-mariadb].hostname >>_data"

View File

@ -0,0 +1,30 @@
- name: "Create database: {{ database_name }}"
mysql_db:
name: "{{ database_name }}"
state: present
login_user: root
login_password: "{{ mariadb_root_pwd }}"
login_host: 127.0.0.1
login_port: "{{ database_port }}"
encoding: "{{ database_encoding }}"
collation: "{{ database_collation }}"
- name: "Create database user: {{ database_username }}"
mysql_user:
name: "{{database_username}}"
password: "{{database_password}}"
host: "%"
priv: '{{database_name}}.*:ALL'
state: present
login_user: root
login_password: "{{mariadb_root_pwd}}"
login_host: 127.0.0.1
login_port: "{{database_port}}"
# Deactivated due to https://chatgpt.com/share/683ba14b-0e74-800f-9ad1-a8979bc77093
# @todo Remove if this works fine in the future.
#- name: Grant database privileges
# ansible.builtin.shell:
# cmd: "docker exec {{mariadb_hostname }} mariadb -u root -p{{ mariadb_root_pwd }} -e \"GRANT ALL PRIVILEGES ON `{{database_name}}`.* TO '{{database_username}}'@'%';\""
# args:
# executable: /bin/bash

View File

@ -1,29 +1,29 @@
- name: Create Docker network for MariaDB - name: Create Docker network for MariaDB
docker_network: docker_network:
name: "{{ applications['svc-db-mariadb'].network }}" name: "{{ mariadb_network_name }}"
state: present state: present
ipam_config: ipam_config:
- subnet: "{{ networks.local['svc-db-mariadb'].subnet }}" - subnet: "{{ mariadb_subnet }}"
when: run_once_docker_mariadb is not defined when: run_once_docker_mariadb is not defined
- name: install MariaDB - name: install MariaDB
docker_container: docker_container:
name: "{{ mariadb_hostname }}" name: "{{ mariadb_hostname }}"
image: "mariadb:{{applications['svc-db-mariadb'].version}}" image: "{{ mariadb_image }}:{{ mariadb_version}}"
detach: yes detach: yes
env: env:
MARIADB_ROOT_PASSWORD: "{{mariadb_root_pwd}}" MARIADB_ROOT_PASSWORD: "{{ mariadb_root_pwd }}"
MARIADB_AUTO_UPGRADE: "1" MARIADB_AUTO_UPGRADE: "1"
networks: networks:
- name: "{{ applications['svc-db-mariadb'].network }}" - name: "{{ mariadb_network_name }}"
volumes: volumes:
- "{{ applications['svc-db-mariadb'].volume }}:/var/lib/mysql" - "{{ mariadb_volume }}:/var/lib/mysql"
published_ports: published_ports:
- "127.0.0.1:{{database_port}}:3306" # can be that this will be removed if all applications use sockets - "127.0.0.1:{{ mariadb_port }}:3306" # can be that this will be removed if all applications use sockets
command: "--transaction-isolation=READ-COMMITTED --binlog-format=ROW" #for nextcloud command: "--transaction-isolation=READ-COMMITTED --binlog-format=ROW" #for nextcloud
restart_policy: "{{docker_restart_policy}}" restart_policy: "{{ docker_restart_policy }}"
healthcheck: healthcheck:
test: "/usr/bin/mariadb --user=root --password={{mariadb_root_pwd}} --execute \"SHOW DATABASES;\"" test: "/usr/bin/mariadb --user=root --password={{ mariadb_root_pwd }} --execute \"SHOW DATABASES;\""
interval: 3s interval: 3s
timeout: 1s timeout: 1s
retries: 5 retries: 5
@ -51,36 +51,9 @@
- setup_mariadb_container_result.changed - setup_mariadb_container_result.changed
- run_once_docker_mariadb is not defined - run_once_docker_mariadb is not defined
- name: "Create database: {{ database_name }}" - name: "Initialize database for '{{ database_name }}'"
mysql_db: include_tasks: init.yml
name: "{{ database_name }}" when: "{{ mariadb_init }}"
state: present
login_user: root
login_password: "{{ mariadb_root_pwd }}"
login_host: 127.0.0.1
login_port: "{{ database_port }}"
encoding: "{{ database_encoding }}"
collation: "{{ database_collation }}"
- name: "Create database user: {{ database_username }}"
mysql_user:
name: "{{database_username}}"
password: "{{database_password}}"
host: "%"
priv: '{{database_name}}.*:ALL'
state: present
login_user: root
login_password: "{{mariadb_root_pwd}}"
login_host: 127.0.0.1
login_port: "{{database_port}}"
# Deactivated due to https://chatgpt.com/share/683ba14b-0e74-800f-9ad1-a8979bc77093
# @todo Remove if this works fine in the future.
#- name: Grant database privileges
# ansible.builtin.shell:
# cmd: "docker exec {{mariadb_hostname }} mariadb -u root -p{{ mariadb_root_pwd }} -e \"GRANT ALL PRIVILEGES ON `{{database_name}}`.* TO '{{database_username}}'@'%';\""
# args:
# executable: /bin/bash
- name: run the docker_mariadb tasks once - name: run the docker_mariadb tasks once
set_fact: set_fact:

View File

@ -1,3 +1,11 @@
application_id: svc-db-mariadb application_id: svc-db-mariadb
mariadb_hostname: "{{ applications | get_app_conf(application_id, 'hostname', True) }}" mariadb_hostname: "{{ applications | get_app_conf(application_id,'hostname', True) }}"
mariadb_root_pwd: "{{ applications['svc-db-mariadb'].credentials.root_password }}" mariadb_root_pwd: "{{ applications | get_app_conf(application_id,'credentials.root_password', True) }}"
mariadb_init: "{{ database_username is defined and database_password is defined and database_name is defined }}"
mariadb_subnet: "{{ networks.local['svc-db-mariadb'].subnet }}"
mariadb_network_name: "{{ applications | get_app_conf(application_id,'network', True) }}"
mariadb_volume: "{{ applications | get_app_conf(application_id,'docker.services.mariadb.volume', True) }}"
mariadb_image: "{{ applications | get_app_conf(application_id,'docker.services.mariadb.image','mariadb', True) }}"
mariadb_version: "{{ applications | get_app_conf(application_id,'docker.services.mariadb.version', True) }}"
mariadb_port: "{{ database_port | default(ports.localhost.database[ application_id ]) }}"