Lưu ý:
- Không cần tắt nginx
- Không cần bảo trì các sites đang có trên S1 nhé
Bước 1. Trên S1 (master) dump
Nếu S1 database ít
mysqldump --single-transaction --routines --triggers --events --all-databases --gtid > /mnt/data/s1_all.sqlcat > /mnt/data/mysqldump_all_db_skip_cache_data.sh <<'EOF'#!/usr/bin/env bashset -euo pipefail
# ================== CONFIG ==================BASE_DIR="/mnt/data"OUT_SQL="${BASE_DIR}/seed_all_nocachedata.sql"OUT_LOG="${BASE_DIR}/seed_all_nocachedata.log"
# Set GZIP=1 khi chạy nếu muốn nénGZIP="${GZIP:-0}"
# Exclude system databasesEXCLUDE_DBS_REGEX='^(information_schema|performance_schema|mysql|sys)$'# ============================================
log() { echo "[$(date +'%F %T')] $*" | tee -a "${OUT_LOG}" >&2}
append_sql() { if [[ "${GZIP}" == "1" ]]; then gzip -c >> "${OUT_SQL}.gz" else cat >> "${OUT_SQL}" fi}
# Prepare output filesmkdir -p "${BASE_DIR}": > "${OUT_LOG}"
if [[ "${GZIP}" == "1" ]]; then : > "${OUT_SQL}.gz"else : > "${OUT_SQL}"fi
log "Starting MariaDB seed dump"log "Output directory: ${BASE_DIR}"log "Compression: ${GZIP}"
# Get list of databasesDBS="$(mysql -N -e "SHOW DATABASES;" | grep -Ev "${EXCLUDE_DBS_REGEX}" || true)"
if [[ -z "${DBS}" ]]; then log "ERROR: No user databases found." exit 2fi
log "Databases to process:"echo "${DBS}" | sed 's/^/ - /' | tee -a "${OUT_LOG}" >&2
# Header{ echo "/*! Seed dump generated at $(date -u +'%F %T UTC') */" echo "SET sql_log_bin=0;" echo "SET FOREIGN_KEY_CHECKS=0;" echo} | append_sql
# Process each databasewhile IFS= read -r DB; do [[ -z "${DB}" ]] && continue log "Processing database: ${DB}"
CACHE_TABLES="$(mysql -N -e " SELECT table_name FROM information_schema.tables WHERE table_schema='${DB}' AND table_name LIKE 'cache\\_%' ORDER BY table_name; " || true)"
CACHE_COUNT="$(echo "${CACHE_TABLES}" | sed '/^$/d' | wc -l | tr -d ' ')" log " cache_* tables found: ${CACHE_COUNT}"
IGNORE_ARGS=() if [[ -n "${CACHE_TABLES}" ]]; then while IFS= read -r t; do [[ -z "${t}" ]] && continue IGNORE_ARGS+=("--ignore-table=${DB}.${t}") done <<< "${CACHE_TABLES}" fi
{ echo echo "/* ===== DATABASE: ${DB} ===== */" echo } | append_sql
# Dump schema + data except cache_* mysqldump \ --single-transaction \ --routines --triggers --events \ --hex-blob \ --databases "${DB}" \ "${IGNORE_ARGS[@]}" \ | append_sql
# Dump schema only for cache_* tables if [[ -n "${CACHE_TABLES}" ]]; then { echo echo "/* --- cache_* schema only for ${DB} --- */" } | append_sql
mysqldump \ --no-data \ --routines=0 --triggers=0 --events=0 \ "${DB}" \ ${CACHE_TABLES} \ | append_sql fi
done <<< "${DBS}"
# Footer{ echo echo "SET FOREIGN_KEY_CHECKS=1;" echo "SET sql_log_bin=1;" echo "/*! End of seed dump */"} | append_sql
if [[ "${GZIP}" == "1" ]]; then log "Completed successfully: ${OUT_SQL}.gz"else log "Completed successfully: ${OUT_SQL}"fiEOF
chmod +x /mnt/data/mysqldump_all_db_skip_cache_data.sh
Rồi chạy:
bash /mnt/data/mysqldump_all_db_skip_cache_data.sh
Bước 2. Copy sang S2
scp /mnt/data/seed_all_nocachedata.sql drvdt@192.168.1.3:/mnt/data/scp root@192.168.1.6:/mnt/data/seed_all_nocachedata.sql /mnt/data/
root@ai:/mnt/data# scp root@192.168.1.6:/mnt/data/seed_all_nocachedata.sql /mnt/data/seed_all_nocachedata.sql 100% 23GB 107.6MB/s 03:40 root@ai:/mnt/data#
Bước 3. Import trên S2
mariadb --binary-mode --show-warnings < /mnt/data/seed_all_nocachedata.sql
/*! Seed dump generated at 2026-01-03 03:02:28 UTC */SET sql_log_bin=0--------------ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Seed dump generated at 2026-01-03 03:02:28 UTC */SET sql_log_bin=0' at line 1Lỗi này là do phần header trong file dump của bạn không “hợp khẩu vị” khi mysql parse ngay từ dòng 1.
Cụ thể, câu SET sql_log_bin=0 trong file đang thiếu dấu ; (log lỗi đã in ra đúng như vậy), nên MySQL client gom nó với phần trước và báo lỗi cú pháp tại line 1.
Cách xử lý nhanh và an toàn nhất: tạo một bản “fixed” ngay trên S2, chỉ sửa các dòng SET ... ở đầu/cuối file (không đụng dữ liệu).
cp /mnt/data/seed_all_nocachedata.sql /mnt/data/seed_all_nocachedata.fixed.sql
# Dòng 1: đổi /*! ... */ thành comment thường để chắc chắn không bị parse như "special comment"sed -i '1s@^/\*!.*\*/@-- Seed dump header@' /mnt/data/seed_all_nocachedata.fixed.sql
# Bổ sung dấu ; nếu thiếu (đầu file)sed -i 's/^SET sql_log_bin=0$/SET sql_log_bin=0;/' /mnt/data/seed_all_nocachedata.fixed.sqlsed -i 's/^SET FOREIGN_KEY_CHECKS=0$/SET FOREIGN_KEY_CHECKS=0;/' /mnt/data/seed_all_nocachedata.fixed.sql
# Bổ sung dấu ; nếu thiếu (cuối file)sed -i 's/^SET FOREIGN_KEY_CHECKS=1$/SET FOREIGN_KEY_CHECKS=1;/' /mnt/data/seed_all_nocachedata.fixed.sqlsed -i 's/^SET sql_log_bin=1$/SET sql_log_bin=1;/' /mnt/data/seed_all_nocachedata.fixed.sql
Sau đó chạy lại với file đã fix:
mariadb --binary-mode --show-warnings < /mnt/data/seed_all_nocachedata.fixed.sql
root@ai:~# mariadb -e "SHOW DATABASES;"
- Đăng nhập để gửi ý kiến