[bitnami/mysql] Fix mysqldump master database (#55126)

* fix mysql dump database

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

* remove comment

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

* fix: linter issue SC2046

Signed-off-by: Alberto Otero Lorenzo <lorenzoa@vmware.com>

---------

Signed-off-by: Michel Silva <silva.michel.b@gmail.com>
Signed-off-by: Alberto Otero Lorenzo <lorenzoa@vmware.com>
Co-authored-by: Alberto Otero Lorenzo <lorenzoa@vmware.com>
This commit is contained in:
michel-silva 2024-02-26 12:34:07 -03:00 committed by GitHub
parent e08d01576e
commit 4f52344e54
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 80 additions and 57 deletions

View File

@ -189,35 +189,45 @@ EOF
#########################
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
DUMP_FILE="$DB_DATA_DIR/dump_$DB.sql"
if mysqldump --verbose -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" "$DB" > "$DUMP_FILE"; then
info "Finish dump database $DB"
info "Ensure database exists $DB"
mysql -u "$DB_MASTER_ROOT_USER" <<EOF
create database if not exists $DB;
info "LOCK MASTER DATABASES FOR WRITE OPERATIONS..."
mysql -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" -se 'FLUSH TABLES WITH READ LOCK;'
info "SHOW MASTER STATUS..."
read -r MYSQL_FILE MYSQL_POSITION <<< "$(mysql -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" -se 'SHOW MASTER STATUS;' | awk 'NR==1 {print $1, $2}')"
info "File: $MYSQL_FILE and Position: $MYSQL_POSITION"
info "Start dump process databases"
FILE_LOCATION="$DB_DATA_DIR/dump_all_databases.sql"
mysqldump --verbose --all-databases -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" > $FILE_LOCATION
info "Finish dump databases"
info "UNLOCK MASTER DATABASES FOR WRITE OPERATIONS..."
mysql -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" -se 'UNLOCK TABLES;'
info "Start import dump databases"
mysql_execute < $FILE_LOCATION
info "Finish import dump databases"
mysql_execute "mysql" <<EOF
CHANGE MASTER TO MASTER_HOST='$DB_MASTER_HOST',
MASTER_PORT=$DB_MASTER_PORT_NUMBER,
MASTER_USER='$DB_REPLICATION_USER',
MASTER_PASSWORD='$DB_REPLICATION_PASSWORD',
MASTER_DELAY=$DB_MASTER_DELAY,
MASTER_LOG_FILE='$MYSQL_FILE',
MASTER_LOG_POS=$MYSQL_POSITION,
MASTER_CONNECT_RETRY=10;
EOF
info "Start import dump database $DB"
mysql_execute "$DB" < "$DUMP_FILE"
info "Finish import dump database $DB"
else
info "Error creating dump"
fi
info "Remove dump file"
rm -f "$DUMP_FILE"
info "Finish dump process database $DB"
done
info "Remove dump file"
rm -f $FILE_LOCATION
info "Finish dump process databases"
info "MySQL dump master data finish..."
}
@ -240,7 +250,7 @@ mysql_configure_replication() {
if [[ "$DB_REPLICATION_SLAVE_DUMP" = "true" ]]; then
mysql_exec_initial_dump
fi
else
debug "Replication master ready!"
debug "Setting the master configuration"
@ -252,6 +262,8 @@ MASTER_PASSWORD='$DB_REPLICATION_PASSWORD',
MASTER_DELAY=$DB_MASTER_DELAY,
MASTER_CONNECT_RETRY=10;
EOF
fi
elif [[ "$DB_REPLICATION_MODE" = "master" ]]; then
info "Configuring replication in master node"
if [[ -n "$DB_REPLICATION_USER" ]]; then

View File

@ -189,35 +189,45 @@ EOF
#########################
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
DUMP_FILE="$DB_DATA_DIR/dump_$DB.sql"
if mysqldump --verbose -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" "$DB" > "$DUMP_FILE"; then
info "Finish dump database $DB"
info "Ensure database exists $DB"
mysql -u "$DB_MASTER_ROOT_USER" <<EOF
create database if not exists $DB;
info "LOCK MASTER DATABASES FOR WRITE OPERATIONS..."
mysql -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" -se 'FLUSH TABLES WITH READ LOCK;'
info "SHOW MASTER STATUS..."
read -r MYSQL_FILE MYSQL_POSITION <<< "$(mysql -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" -se 'SHOW MASTER STATUS;' | awk 'NR==1 {print $1, $2}')"
info "File: $MYSQL_FILE and Position: $MYSQL_POSITION"
info "Start dump process databases"
FILE_LOCATION="$DB_DATA_DIR/dump_all_databases.sql"
mysqldump --verbose --all-databases -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" > $FILE_LOCATION
info "Finish dump databases"
info "UNLOCK MASTER DATABASES FOR WRITE OPERATIONS..."
mysql -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" -se 'UNLOCK TABLES;'
info "Start import dump databases"
mysql_execute < $FILE_LOCATION
info "Finish import dump databases"
mysql_execute "mysql" <<EOF
CHANGE MASTER TO MASTER_HOST='$DB_MASTER_HOST',
MASTER_PORT=$DB_MASTER_PORT_NUMBER,
MASTER_USER='$DB_REPLICATION_USER',
MASTER_PASSWORD='$DB_REPLICATION_PASSWORD',
MASTER_DELAY=$DB_MASTER_DELAY,
MASTER_LOG_FILE='$MYSQL_FILE',
MASTER_LOG_POS=$MYSQL_POSITION,
MASTER_CONNECT_RETRY=10;
EOF
info "Start import dump database $DB"
mysql_execute "$DB" < "$DUMP_FILE"
info "Finish import dump database $DB"
else
info "Error creating dump"
fi
info "Remove dump file"
rm -f "$DUMP_FILE"
info "Finish dump process database $DB"
done
info "Remove dump file"
rm -f $FILE_LOCATION
info "Finish dump process databases"
info "MySQL dump master data finish..."
}
@ -240,7 +250,7 @@ mysql_configure_replication() {
if [[ "$DB_REPLICATION_SLAVE_DUMP" = "true" ]]; then
mysql_exec_initial_dump
fi
else
debug "Replication master ready!"
debug "Setting the master configuration"
@ -252,6 +262,8 @@ MASTER_PASSWORD='$DB_REPLICATION_PASSWORD',
MASTER_DELAY=$DB_MASTER_DELAY,
MASTER_CONNECT_RETRY=10;
EOF
fi
elif [[ "$DB_REPLICATION_MODE" = "master" ]]; then
info "Configuring replication in master node"
if [[ -n "$DB_REPLICATION_USER" ]]; then

View File

@ -426,7 +426,6 @@ The above command scales up the number of slaves to `3`. You can scale down in t
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