Import Database into MySQL
To import a database into MySQL from a .sql file, you can use one of the following methods depending on your environment:
🖥️ Method 1: Using the MySQL Command Line
mysql -u username -p database_name < path/to/your/file.sql
- Replace
usernamewith your MySQL username. - Replace
database_namewith the name of the database you want to import into. - You’ll be prompted for your password.
- Make sure the database already exists. If not, create it first:
mysql -u username -p -e "CREATE DATABASE database_name;"
🧰 Method 2: Using MySQL Workbench
- Open MySQL Workbench and connect to your server.
- Go to File > Open SQL Script and select your
.sqlfile. - The script will open in a new tab. Click Execute (lightning bolt icon) to run it.
- Alternatively, use Server > Data Import and choose “Import from Self-Contained File”.
🐳 Method 3: Inside a Docker Container
If you’re using MySQL in Docker:
docker exec -i mysql-container-name mysql -u username -p database_name < file.sql
- Make sure the
.sqlfile is accessible to the container (e.g., via volume mount).
🧪 Bonus Tips
- If your
.sqlfile includesCREATE DATABASEstatements, you don’t need to pre-create the database. - For large files, consider using
mysqlimportorsourcefrom within the MySQL shell:mysql> USE database_name; mysql> SOURCE /path/to/file.sql;