Skip to content

Nuface Blog

隨意隨手記 Casual Notes

Menu
  • Home
  • About
  • Services
  • Blog
  • Contact
  • Privacy Policy
  • Login
Menu

Importing MariaDB SQL Files Inside Docker Containers — Practical Notes

Posted on 2025-11-052025-11-05 by Rico

This post documents how to import .sql or .sql.gz backup files into MariaDB (version 12.1.1) running inside a Docker container, including common mistakes and their fixes.


1. Background

When managing databases in containerized environments, it’s common to deploy MariaDB inside Docker and occasionally import database backups for restoration or migration.

Example environment:

ItemValue
Container nameblognufacedb
MariaDB version12.1.1-MariaDB
Usernamenuface
Passwordabcd123
Database namenuface
SQL filenuface_2025-09-16.sql

2. The Original Command

docker exec -i blognufacedb mariadb -u nuface -p 'abcd123' --default-character-set=utf8mb4 nuface < nuface_2025-09-16.sql

After execution, the terminal displayed a long list of help text:

mariadb from 12.1.1-MariaDB, client 15.2 for Linux (x86_64) using EditLine wrapper
Usage: mariadb [OPTIONS] [database]
...

MariaDB did not actually import anything—it simply printed the help message.


3. Root Cause

The issue was:

There is an extra space after -p.

MariaDB interprets 'abcd123' as a database name, not a password.
This causes the command arguments to shift and results in the help screen being displayed.


4. Correct Import Commands

✅ Method 1: Use -p'password' (No space!)

docker exec -i blognufacedb \
  mariadb -u nuface -p'abcd123' --default-character-set=utf8mb4 nuface \
  < nuface_2025-09-16.sql

✅ Method 2: Use --password=

docker exec -i blognufacedb \
  mariadb -u nuface --password='abcd123' --default-character-set=utf8mb4 nuface \
  < nuface_2025-09-16.sql

✅ Method 3: Use Environment Variable (Recommended)

This avoids exposing your password in the command line or shell history.

docker exec -i -e MYSQL_PWD='abcd123' \
  blognufacedb mariadb -u nuface --default-character-set=utf8mb4 nuface \
  < nuface_2025-09-16.sql

5. Importing a Compressed File (.sql.gz)

gunzip -c nuface_2025-09-16.sql.gz | \
docker exec -i -e MYSQL_PWD='abcd123' \
  blognufacedb mariadb -u nuface --default-character-set=utf8mb4 nuface

6. Advanced Options (for Large Databases)

To handle large imports more efficiently, add:

--max-allowed-packet=512M \
--init-command="SET time_zone='+08:00'; SET FOREIGN_KEY_CHECKS=0;"

Example:

docker exec -i -e MYSQL_PWD='abcd123' \
  blognufacedb mariadb -u nuface --default-character-set=utf8mb4 nuface \
  --max-allowed-packet=512M \
  --init-command="SET time_zone='+08:00'; SET FOREIGN_KEY_CHECKS=0;" \
  < nuface_2025-09-16.sql

7. Tips and Troubleshooting

  • If your dump file already includes CREATE DATABASE or USE nuface;, you can omit the database name in the command.
  • If you encounter: ERROR 1227 (42000): Access denied It’s usually due to insufficient privileges (e.g., DEFINER statements in views or routines). Use a user with CREATE ROUTINE or SUPER privileges.
  • To verify connectivity before import: docker exec -it blognufacedb mariadb -u nuface -p'abcd123' -e "SELECT VERSION();"

8. Summary

When importing SQL files into MariaDB running inside Docker, most issues stem from incorrect parameter syntax.
Keep these points in mind:

Key PointExplanation
No space after -pOtherwise, MariaDB misinterprets the argument
Use MYSQL_PWD for securityKeeps passwords out of command history
You can import .gz files directly with `gunzip -cdocker exec -i`
Add --max-allowed-packet and --init-command for large filesImproves reliability

📌 Related Articles

  • mysqldump Export Syntax Notes (MySQL / MariaDB 5.5)
  • Best Practices for MariaDB Backup and Restore in Docker

Recent Posts

  • Enable Logrotate for Dovecot in Docker: Prevent Huge Log Files and Disk Overflow
  • 在 Docker Dovecot 中啟用 Logrotate:避免 log 爆量、磁碟被塞滿的最佳做法
  • How to Choose Suricata RuleSets on OPNsense — Practical Guide & Best Recommendations
  • OPNsense Suricata 使用指南 — 規則(RuleSets)該怎麼選?最佳實務與推薦設定
  • Proxmox VE + Proxmox Backup Server Integration & Cross-Node Restore Guide

Recent Comments

No comments to show.

Archives

  • November 2025
  • October 2025

Categories

  • AI
  • Apache
  • Cybersecurity
  • Database
  • DNS
  • Docker
  • Fail2Ban
  • FileSystem
  • Firewall
  • Linux
  • LLM
  • Mail
  • N8N
  • OpenLdap
  • OPNsense
  • PHP
  • QoS
  • Samba
  • Switch
  • Virtualization
  • VPN
  • WordPress
© 2025 Nuface Blog | Powered by Superbs Personal Blog theme