When I was working with the MySql command prompt, I was wondering how to import an available sql file into the available database.
Overview
This short guide shows two simple ways to import an .sql file into an existing MySQL database from the command line:
- Using the mysql client interactive prompt with the SOURCE command
- From the operating-system shell using input redirection (<)
Prerequisites
- MySQL client (mysql) installed and on your PATH
- Database already created (you will import into an existing database)
- Credentials (username and password) with permissions to create/modify tables in the target database
- Path to the .sql file you want to import (absolute or relative)
Method 1 — inside the mysql client using SOURCE
- Open the MySQL command prompt and select the target database. In the example below the database is named
smartcart:
mysql> use smartcart;
Database changed
- Use the
SOURCEcommand with the path to your.sqlfile. The example usesf:/items.sql:
mysql> source f:/items.sql;
Query OK, 0 rows affected (0.07 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.76 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
- Verify that the new table(s) from the SQL file were added to the database:
mysql> show tables;
+---------------------+
| Tables_in_smartcart |
+---------------------+
| items |
| users |
+---------------------+
2 rows in set (0.00 sec)
mysql> select * from items;
Empty set (0.06 sec)
Notes for Method 1:
- The path you pass to
SOURCEcan be absolute or relative to the server/your client working directory. On Windows you can use drive letters (e.g.f:/items.sql) or surround paths with quotes if they contain spaces. SOURCEruns all SQL statements in the file as if you typed them into the client.
Method 2 — from the OS shell using input redirection
You can import an SQL file directly from your command shell without first entering the mysql prompt. This is often faster for large files.
Example:
# From a Unix/Linux/macOS or Windows PowerShell/CMD shell
mysql -u username -p smartcart < f:/items.sql
# You will be prompted for the MySQL password
Notes for Method 2:
- Replace
usernamewith your MySQL user andsmartcartwith the target database name. - If you omit the database name you can still import statements that include
CREATE DATABASE/USEstatements. - On Windows, ensure the path syntax is correct (you can also use double backslashes or forward slashes).
Troubleshooting and tips
- Permissions: make sure the user you connect with has the required permissions on the target database.
- File path: if you see "file not found" errors, verify the path and quoting. Try an absolute path.
- Empty table after import: the file may only contain schema (CREATE TABLE) statements or no INSERT statements. Open the .sql file to confirm it contains INSERTs if you expect data.
- Errors during import: the mysql client will print errors as it executes statements. Rerun the import after fixing the SQL or adjust the error-handling behavior (for automation, consider
mysql --forceto continue past errors). - Character set issues: if you see strange characters, specify
--default-character-set=utf8mb4(or appropriate charset) when invokingmysql.
Summary
Both SOURCE (inside the mysql client) and shell redirection (mysql < file.sql) are valid ways to import an .sql file. Use the method that fits your workflow: interactive debugging inside mysql or direct shell import for automation/large files.
That's it. Happy coding.
No comments:
Post a Comment