Refs #8140: MariaDB Server Deploy - Role WIP

This commit is contained in:
Xavi Lleó 2024-11-28 13:26:02 +01:00
parent 31180a8d66
commit 8c88bd8a80
15 changed files with 7477 additions and 0 deletions

View File

@ -0,0 +1,3 @@
[Service]
LimitNOFILE=600000
LimitMEMLOCK=2M

View File

@ -0,0 +1,19 @@
# Scripts to maintain MariaDB
## scheduler-log.sh
The following table should be created into MySQL/MariaDB database.
```
CREATE TABLE `eventLog` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` datetime NOT NULL,
`event` varchar(512) NOT NULL,
`error` varchar(1024) NOT NULL,
PRIMARY KEY (`id`),
KEY `date` (`date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci COMMENT='Event scheduler error log'
```
Then adjust the *$logTable* variable to the correct schema.

View File

@ -0,0 +1,9 @@
#!/bin/bash
minFree=5
memFree=$(free --gibi | awk '$1 == "Mem:" { print $7 }')
if [ "$memFree" -le "$minFree" ]; then
echo "Free memory is ${memFree}Gi, restarting mariadb service to prevent OOM killer..."
systemctl restart mariadb
fi

View File

@ -0,0 +1,25 @@
#!/bin/bash
OUTFILE=privs.sql
SCHEMA=mysql
TABLES=(
global_priv
db
tables_priv
columns_priv
procs_priv
proxies_priv
roles_mapping
)
echo "USE \`$SCHEMA\`;" > "$OUTFILE"
for TABLE in "${TABLES[@]}"
do
echo "TRUNCATE TABLE \`$SCHEMA\`.\`$TABLE\`;" >> "$OUTFILE"
done
echo "" >> "$OUTFILE"
mysqldump --no-create-info --skip-triggers "$SCHEMA" ${TABLES[@]} >> "$OUTFILE"
echo "FLUSH PRIVILEGES;" >> "$OUTFILE"

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,15 @@
#!/bin/bash
mysql -e "SET GLOBAL event_scheduler = OFF"
echo "SELECT db, name FROM mysql.event WHERE status = 'SLAVESIDE_DISABLED'" | mysql --raw --silent | \
awk '{
gsub("`", "``", $1);
gsub("`", "``", $2);
print "`"$1"`.`"$2"`";
}' | \
while read event; do
mysql -e "ALTER EVENT $event ENABLE"
done
mysql -e "SET GLOBAL event_scheduler = ON"

View File

@ -0,0 +1,16 @@
#!/bin/bash
mysql -e "SET GLOBAL event_scheduler = OFF"
echo "SELECT db, name FROM mysql.event WHERE status = 'ENABLED'" | mysql --raw --silent | \
awk '{
gsub("`", "``", $1);
gsub("`", "``", $2);
print "`"$1"`.`"$2"`";
}' | \
while read event; do
mysql -e "ALTER EVENT $event DISABLE ON SLAVE"
done
mysql -e "SET GLOBAL event_scheduler = ON"

View File

@ -0,0 +1,49 @@
#!/bin/bash
set -e
logFile="/var/log/mysql/error.log"
dateFile="/tmp/mysql_scheduler_log-lastdate"
logTable="util.eventLog"
purgeDays=30
quote() {
local str=${1//\'/\'\'/}
local str=${str//\\/\\\\}
echo "'$str'"
}
mysql -e "SELECT TRUE" > /dev/null 2>&1
if [ "$?" -ne "0" ]; then
exit
fi
if [ -f "$dateFile" ]; then
fromDate=$(cat "$dateFile")
else
fromDate=0
fi
lastDate=$(tail -n1 "$logFile" | awk '{print $1" "$2}')
toDate=$(date +%s -d "$lastDate")
awk -v fromDate="$fromDate" -v toDate="$toDate" '{
split($1, date, "-");
split($2, time, ":");
timestamp = mktime(date[1]" "date[2]" "date[3]" "time[1]" "time[2]" "time[3])
if (timestamp >= fromDate && timestamp < toDate && $4" "$5" "$6 == "[ERROR] Event Scheduler:") {
printf $1" "$2" "$7;
for (i=8; i<=NF; i++) printf FS $i ;
print "";
}
}' "$logFile" | \
\
while read line; do
date="$(echo "$line" | cut -d' ' -f1,2)"
event="$(echo "$line" | cut -d' ' -f3)"
error="$(echo "$line" | cut -d' ' -f4-)"
echo "INSERT INTO $logTable (date, event, error)" \
"VALUES ($(quote "$date"), $(quote "$event"), $(quote "$error"))" | mysql
done
echo -n "$toDate" > "$dateFile"
echo "DELETE FROM $logTable WHERE date < TIMESTAMPADD(DAY, -$purgeDays, NOW())" | mysql

View File

@ -0,0 +1,21 @@
#!/bin/bash
partner=root@db2.static.verdnatura.es
confDir=/etc/mysql/mariadb.conf.d
files=(
z90-vn.cnf
z95-production.cnf
)
#echo "Reloading service."
#service mariadb reload
if [ $? -eq "0" ]; then
echo "Synchronizing partner configuration."
for file in "${files[@]}"; do
scp "$confDir/$file" $partner:$confDir
done
#echo "Reloading partner service."
#ssh $partner service mariadb reload
fi

View File

@ -0,0 +1,120 @@
[mysqld]
# Docs: https://mariadb.com/kb/en/server-system-variables
lc_messages = es_ES
lc_time_names = es_ES
character-set-server = utf8
collation-server = utf8_unicode_ci
explicit_defaults_for_timestamp = ON
datadir = /var/lib/mysql
tmpdir = /mnt/mysqltmp
log_bin_trust_function_creators = 1
sql_mode = NO_ENGINE_SUBSTITUTION
bind-address = 0.0.0.0
max_password_errors = 50
#++++++++++++++++++++++++++++++++++++++++ Threads
thread_stack = 512K
join_buffer_size = 2M
sort_buffer_size = 4M
net_buffer_length = 256K
max_allowed_packet = 16M
read_buffer_size = 1M
read_rnd_buffer_size = 512K
#++++++++++++++++++++++++++++++++++++++++ Performance
thread_cache_size = 450
interactive_timeout = 1800
wait_timeout = 1800
open_files_limit = 20000
low_priority_updates = 1
table_open_cache = 40000
table_definition_cache = 10000
table_open_cache_instances = 1
key_buffer_size = 256K
max_heap_table_size = 128M
tmp_table_size = 128M
concurrent_insert = ALWAYS
group_concat_max_len = 10000
max_connect_errors = 50
#++++++++++++++++++++++++++++++++++++++++ Binary log
log-bin = /mnt/mysqlbin/binlog/bin.log
max_binlog_size = 1GB
binlog_cache_size = 16M
binlog_stmt_cache_size = 16M
binlog_row_image = noblob
binlog_format = row
relay_log = /mnt/mysqlbin/binlog/relay.log
binlog-ignore-db = tmp
binlog-ignore-db = PERCONA_SCHEMA
#++++++++++++++++++++++++++++++++++++++++ Replication
event-scheduler = ON
slave_exec_mode = STRICT
replicate-ignore-db = tmp
replicate-ignore-table = util.eventLog
replicate-ignore-table = cache.cache_calc
replicate-ignore-table = cache.available
replicate-ignore-table = cache.availableNoRaids
replicate-ignore-table = cache.cache_valid
replicate-ignore-table = cache.stock
replicate-ignore-table = cache.visible
#++++++++++++++++++++++++++++++++++++++++ InnoDB
transaction-isolation = READ-COMMITTED
idle_transaction_timeout = 60
innodb_io_capacity = 100
innodb_io_capacity_max = 100
innodb_monitor_enable = all
innodb_read_io_threads = 16
innodb_write_io_threads = 16
innodb_checksum_algorithm = crc32
innodb_adaptive_hash_index = 0
innodb_flush_method = O_DIRECT
innodb_log_buffer_size = 32M
innodb_log_file_size = 8G
innodb_purge_threads = 4
innodb_buffer_pool_dump_at_shutdown = ON
innodb_buffer_pool_load_at_startup = ON
#++++++++++++++++++++++++++++++++++++++++ Logging
log_error = /var/log/mysql/error.log
log_warnings = 1
log_output = TABLE
general_log = OFF
slow_query_log = ON
long_query_time = 2
min_examined_row_limit = 0
log_slow_admin_statements = ON
log_queries_not_using_indexes = OFF
max_error_count = 100
#++++++++++++++++++++++++++++++++++++++++ SSL
ssl-ca = /etc/mysql/ca.pem
ssl-cert = /etc/mysql/cert.pem
ssl-key = /etc/mysql/key.pem
#++++++++++++++++++++++++++++++++++++++++ Query cache
query_cache_limit = 0
query_cache_type = OFF
query_cache_size = 0
#++++++++++++++++++++++++++++++++++++++++ Performance Schema
performance_schema = ON
performance_schema_digests_size = 20000
performance-schema-consumer-events-statements-history = ON
performance_schema_consumer_events_statements_history_long = ON
query_response_time_stats = ON
userstat = ON

View File

@ -0,0 +1,6 @@
[mysqld]
port = 3306
max_connections = 1000
expire_logs_days = 7
innodb_buffer_pool_size = 64G

View File

@ -0,0 +1,7 @@
[mysqld]
server-id = 1
#bind-address = 127.0.0.1
#event-scheduler = OFF
#skip-log-bin
#skip-slave-start

View File

@ -2,4 +2,7 @@
systemd:
name: chrony
state: restarted
- name: restart-mariadb
systemd:
name: mariadb

View File

@ -1,2 +1,4 @@
- import_tasks: timeserver.yml
tags: timeserver
- import_tasks: mariadb.yml
tags: mariadb

View File

@ -0,0 +1,41 @@
- name: Install MariaDB packages
apt:
name: mariadb-server
state: present
#install_recommends: no
- name: Set MariaDB custom configuration
copy:
src: "{{ item }}"
dest: /etc/mysql/mariadb.conf.d/
owner: root
group: root
mode: u=rw,g=r,o=r
with_fileglob:
- "files/z9*.cnf"
notify: restart-mariadb
- name: Set MariaDB custom service.d override.conf
copy:
src: mariadb_override.conf
dest: /etc/systemd/system/mariadb.service.d/override.conf
owner: root
group: root
mode: u=rw,g=r,o=r
notify: restart-mariadb
- name: Set MariaDB custom root scripts
copy:
src: "{{ item }}"
dest: /root/scripts
owner: root
group: root
mode: u=rwx,g=rx,o=rx
with_fileglob:
- "files/scripts/*.sh"
notify: restart-mariadb
- name: Set MariaDB README root scripts
copy:
src: files/scripts/README.md
dest: /root/scripts/README.md
owner: root
group: root
mode: u=rw,g=r,o=r
notify: restart-mariadb