[bitnami/mysql] Feat mysqldump master database (#54038)

* add dump to mysql 8

Signed-off-by: Michel Silva <silva.michel.b@gmail.com>

* add dump to mysql 8.2

Signed-off-by: Michel Silva <silva.michel.b@gmail.com>

* add exemplo on docker compose

Signed-off-by: Michel Silva <silva.michel.b@gmail.com>

* add doc

Signed-off-by: Michel Silva <silva.michel.b@gmail.com>

* Remove blank spaces

Signed-off-by: Michel Silva <silva.michel.b@gmail.com>

* check database is set

Signed-off-by: Michel Silva <silva.michel.b@gmail.com>

* Use DB_DATA_DIR

Signed-off-by: Michel Silva <silva.michel.b@gmail.com>

* use lower case

Signed-off-by: Michel Silva <silva.michel.b@gmail.com>

* update notes

Signed-off-by: Michel Silva <silva.michel.b@gmail.com>

* update comment

Signed-off-by: Michel Silva <silva.michel.b@gmail.com>

* fix if statement

Signed-off-by: Michel Silva <silva.michel.b@gmail.com>

* update info

Signed-off-by: Michel Silva <silva.michel.b@gmail.com>

* remove head

Signed-off-by: Michel Silva <silva.michel.b@gmail.com>

* update info error message

Signed-off-by: Michel Silva <silva.michel.b@gmail.com>

---------

Signed-off-by: Michel Silva <silva.michel.b@gmail.com>
This commit is contained in:
michel-silva
2024-01-16 11:37:42 +01:00
committed by GitHub
parent fdab27f621
commit 79352ad5a9
6 changed files with 139 additions and 0 deletions
@@ -178,6 +178,65 @@ pid_file=${DB_PID_FILE}
EOF
}
########################
# Make a dump on master database and update slave database
# Globals:
# DB_*
# Arguments:
# None
# Returns:
# None
#########################
mysql_exec_initial_dump() {
info "MySQL dump master data start..."
mysql -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" -e 'RESET MASTER;'
databases=("mysql")
if [ -n "$DB_DATABASE" ]; then
databases+=("$DB_DATABASE")
fi
for DB in "${databases[@]}"; do
info "Start dump process database $DB"
if [[ $DB = @(information_schema|performance_schema|sys) ]]; then
info "Skipping default table $DB to be imported"
continue
fi
FILE_LOCATION="$DB_DATA_DIR/dump_$DB.sql"
mysqldump --verbose -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" $DB > $FILE_LOCATION
info "Finish dump database $DB"
if [ $? -eq 0 ]; then
info "Ensure database exists $DB"
mysql -u "$DB_MASTER_ROOT_USER" <<EOF
create database if not exists $DB;
EOF
info "Start import dump database $DB"
mysql_execute $DB < $FILE_LOCATION
info "Finish import dump database $DB"
else
info "Error creating dump"
fi
info "Remove dump file"
rm -f $FILE_LOCATION
info "Finish dump process database $DB"
done
info "MySQL dump master data finish..."
}
########################
# Migrate old custom configuration files
# Globals:
@@ -194,6 +253,11 @@ mysql_configure_replication() {
while ! echo "select 1" | mysql_remote_execute "$DB_MASTER_HOST" "$DB_MASTER_PORT_NUMBER" "mysql" "$DB_MASTER_ROOT_USER" "$DB_MASTER_ROOT_PASSWORD"; do
sleep 1
done
if [[ "$DB_REPLICATION_SLAVE_DUMP" = "true" ]]; then
mysql_exec_initial_dump
fi
debug "Replication master ready!"
debug "Setting the master configuration"
mysql_execute "mysql" <<EOF
@@ -129,6 +129,8 @@ export MYSQL_REPLICATION_PASSWORD="${MYSQL_REPLICATION_PASSWORD:-}"
export DB_REPLICATION_PASSWORD="$MYSQL_REPLICATION_PASSWORD" # only used during the first initialization
# Settings
export MYSQL_REPLICATION_SLAVE_DUMP="${MYSQL_REPLICATION_SLAVE_DUMP:-false}"
export DB_REPLICATION_SLAVE_DUMP="$MYSQL_REPLICATION_SLAVE_DUMP"
export MYSQL_PORT_NUMBER="${MYSQL_PORT_NUMBER:-}"
export DB_PORT_NUMBER="$MYSQL_PORT_NUMBER"
export MYSQL_REPLICATION_MODE="${MYSQL_REPLICATION_MODE:-}"
@@ -178,6 +178,65 @@ pid_file=${DB_PID_FILE}
EOF
}
########################
# Make a dump on master database and update slave database
# Globals:
# DB_*
# Arguments:
# None
# Returns:
# None
#########################
mysql_exec_initial_dump() {
info "MySQL dump master data start..."
mysql -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" -e 'RESET MASTER;'
databases=("mysql")
if [ -n "$DB_DATABASE" ]; then
databases+=("$DB_DATABASE")
fi
for DB in "${databases[@]}"; do
info "Start dump process database $DB"
if [[ $DB = @(information_schema|performance_schema|sys) ]]; then
info "Skipping default table $DB to be imported"
continue
fi
FILE_LOCATION="$DB_DATA_DIR/dump_$DB.sql"
mysqldump --verbose -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" $DB > $FILE_LOCATION
info "Finish dump database $DB"
if [ $? -eq 0 ]; then
info "Ensure database exists $DB"
mysql -u "$DB_MASTER_ROOT_USER" <<EOF
create database if not exists $DB;
EOF
info "Start import dump database $DB"
mysql_execute $DB < $FILE_LOCATION
info "Finish import dump database $DB"
else
info "Error creating dump"
fi
info "Remove dump file"
rm -f $FILE_LOCATION
info "Finish dump process database $DB"
done
info "MySQL dump master data finish..."
}
########################
# Migrate old custom configuration files
# Globals:
@@ -194,6 +253,11 @@ mysql_configure_replication() {
while ! echo "select 1" | mysql_remote_execute "$DB_MASTER_HOST" "$DB_MASTER_PORT_NUMBER" "mysql" "$DB_MASTER_ROOT_USER" "$DB_MASTER_ROOT_PASSWORD"; do
sleep 1
done
if [[ "$DB_REPLICATION_SLAVE_DUMP" = "true" ]]; then
mysql_exec_initial_dump
fi
debug "Replication master ready!"
debug "Setting the master configuration"
mysql_execute "mysql" <<EOF
@@ -129,6 +129,8 @@ export MYSQL_REPLICATION_PASSWORD="${MYSQL_REPLICATION_PASSWORD:-}"
export DB_REPLICATION_PASSWORD="$MYSQL_REPLICATION_PASSWORD" # only used during the first initialization
# Settings
export MYSQL_REPLICATION_SLAVE_DUMP="${MYSQL_REPLICATION_SLAVE_DUMP:-false}"
export DB_REPLICATION_SLAVE_DUMP="$MYSQL_REPLICATION_SLAVE_DUMP"
export MYSQL_PORT_NUMBER="${MYSQL_PORT_NUMBER:-}"
export DB_PORT_NUMBER="$MYSQL_PORT_NUMBER"
export MYSQL_REPLICATION_MODE="${MYSQL_REPLICATION_MODE:-}"
+5
View File
@@ -389,6 +389,11 @@ The above command scales up the number of slaves to `3`. You can scale down in t
> **Note**: You should not scale up/down the number of master nodes. Always have only one master node running.
If your master database is missing some binary files, the replication will break. It's possible to add `MYSQL_REPLICATION_SLAVE_DUMP=true` to make a dump on the master and import it on the slave.
> **Note**: The master database must be only used by this process until the end to avoid missing data.
> **Note**: This process will use "RESET MASTER" on the master database, removing all binary log files that are listed in the index file. It is recommended to make a backup of binary files to avoid possible data loss, in case something goes wrong.
### Configuration file
The image looks for user-defined configurations in `/opt/bitnami/mysql/conf/my_custom.cnf`. Create a file named `my_custom.cnf` and mount it at `/opt/bitnami/mysql/conf/my_custom.cnf`.
@@ -40,6 +40,8 @@ services:
- MYSQL_MASTER_ROOT_PASSWORD=my_root_password
# ALLOW_EMPTY_PASSWORD is recommended only for development.
- ALLOW_EMPTY_PASSWORD=yes
# In case of missing binary files on master, use `true` to reset those binary files. Creating a previous backup is recommended.
- MYSQL_REPLICATION_SLAVE_DUMP=false
healthcheck:
test: ['CMD', '/opt/bitnami/scripts/mysql/healthcheck.sh']
interval: 15s