2823

I have a .sql file with an export from phpMyAdmin. I want to import it into a different server using the command line.

I have a Windows Server 2008 R2 installation. I placed the .sql file on the C drive, and I tried this command

database_name < file.sql

It is not working. I get syntax errors.

  • How can I import this file without a problem?
  • Do I need to create a database first?
6
  • 9
    possible duplicate of Restore MYSQL Dump File with Command Line Commented Jul 16, 2013 at 0:47
  • 2
    possible duplicate of stackoverflow.com/questions/11407349/… Commented Aug 29, 2017 at 14:01
  • 1
    Can you share a reproducable example? database < file.sql does not look like any command to me, and if you see some syntax errors, please share them Commented Dec 10, 2019 at 11:28
  • 1
    After I have checked all answers below, I must say you missed a very important clue for those people who wants to help. You failed to specify the exact command when you dump data out of the database. Commented Oct 10, 2020 at 16:26
  • 2
    "I get syntax errors" - what does that mean? Commented Oct 29, 2021 at 15:30

58 Answers 58

1
2
7

I thought it could be useful for those who are using Mac OS X:

/Applications/xampp/xamppfiles/bin/mysql -u root -p database < database.sql

Replace xampp with mamp or other web servers.

Sign up to request clarification or add additional context in comments.

Comments

6

For backup purposes, make a BAT file and run this BAT file using Task Scheduler. It will take a backup of the database; just copy the following line and paste in Notepad and then save the .bat file, and run it on your system.

@echo off
for /f "tokens=1" %%i in ('date /t') do set DATE_DOW=%%i
for /f "tokens=2" %%i in ('date /t') do set DATE_DAY=%%i
for /f %%i in ('echo %date_day:/=-%') do set DATE_DAY=%%i
for /f %%i in ('time /t') do set DATE_TIME=%%i
for /f %%i in ('echo %date_time::=-%') do set DATE_TIME=%%i

"C:\Program Files\MySQL\mysql server 5.5\bin\mysqldump" -u username -ppassword mysql>C:/%DATE_DAY%_%DATE_TIME%_database.sql

Comments

6

I'm using Windows 10 with PowerShell 5, and I found almost all "Unix-like" solutions not working for me.

mysql -u[username] [database-name] < my-database.sql

Output:

At line:1 char:31
+ mysql -u[username] [database-name] < my-database.sql
+                               ~
The '<' operator is reserved for future use.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : RedirectionNotSupported

I ends up using this command:

type my-database.sql | mysql -u[username] -h[localhost] -p [database-name]

And it works perfectly, and hopefully it helps.

Thanks to @Francesco Casula's answer, BTW.

Comments

5

To import a database via the terminal

Navigate to folder where the .sql file is located

Then run the below command:

mysql -u database_user_name -p database_name < sql_file_name.sql

It will ask for a password. Enter the database password. It will take a few seconds to import the data into the database.

Comments

5

This line imports the dump file in the local database, under Linux.

mysql -u dbuser -p'password including spaces' dbname < path/to/dump_file.sql

This line imports the dump file in the remote database, under Linux. Note: -P is for the port and is required if the MySQL port is different than the default.

mysql -h dbhost -u dbuser -p'password including spaces' -P 3306 dbname < path/to/dump_file.sql

Note: the password includes spaces and this is the reason of the single quotes. Just change the path style for using the command under Windows (C:\windows\path\dump_file.sql).

Comments

4

The following steps help to upload file.sql to the MySQL database.

Step 1: Upload file.sql.zip to any directory and unzip there
Note: sudo apt-get install unzip : sudo apt-get unzip file.sql.zip
Step 2: Now navigate to that directory. Example: cd /var/www/html

Step 3: mysql -u username -p database-name < file.sql
Enter the password and wait till uploading is completed.

Comments

4

If importing data into a Docker container use the following command. Adjust user(-u), database(-D), port(-P) and host(-h) to fit your configuration.

mysql -u root -D database_name -P 4406 -h localhost --protocol=tcp -p < sample_dump.sql

1 Comment

I needed the host name to mention because localhost was not the DB host name in my case. So this syntax helped me. Voting it up.
4
mysql -u myuser -p mydatabase < mydata.sql

Replace myuser with your MySQL username, mydatabase with the name of your MySQL database, and mydata.sql with the name of your SQL file.
Make sure that the SQL file is properly formatted and does not contain any syntax errors that could cause issues during import.

Comments

3

Using MySQL Secure Shell:

mysqlsh -u <username> -p -h <host> -D <database name> -f dump.sql

Comments

3

If you use XAMPP on the windows, first, you must manually create the database and then run the following commands:

cd C:\xampp\mysql\bin && 
mysql -u YOUR_USERNAME -p YOUR_DATABASE_NAME < PATH_TO_YOUR_SQL_FILE\YOUR_SQL_FILE.sql

And then enter the password

Comments

3

Simple. Just use this command in cmd:

use databasename
\. C:/test/data.mysql

1 Comment

There isn't a "use" in cmd (Windows) as far as I know (though there is NET USE). Do you mean inside the MySQL client? Or something else? Can you elaborate? Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).
3

The fastest way

I've used the solution below to import a 100GB database (not a typo) of almost 300 million records in less than 10 hours, so it's safe to say it will rip through the average SQL dump in no time.

First, you should make sure your database is making the most of your hardware - most out-of-the-box SQL installations are set up to be as lightweight (and therefore inefficient) as possible. Assuming your database is using the InnoDB engine (which it is if you are using XAMPP), edit the following variables in your my.ini file:

innodb_buffer_pool_size=8G // Change to 50-75% of your system's RAM, depending on how much you need to keep free
innodb_log_file_size=2G // Change to 25% of the above value
innodb_log_buffer_size=8M

If you're using another database engine then you'll need to find the equivalent configuration options and tune them accordingly.

Once you've made those configuration changes, restart your server and run the following in the command-line:

mysql -u root -e "CREATE DATABASE IF NOT EXISTS <database_name>; USE <database_name>; \
SET FOREIGN_KEY_CHECKS = 0; SET UNIQUE_CHECKS = 0; SET AUTOCOMMIT = 0; \
source <file_name>; SET UNIQUE_CHECKS = 1; SET FOREIGN_KEY_CHECKS = 1; COMMIT;"

Although this uses the mysql CLI tool it's just executing plain MySQL so you can alternatively copy-paste the SQL code from it directly into your database tool's SQL tab (eg. PHPMyAdmin).

Comments

2

You can use:

mysql -u<user> -p<pass> <db> < db.sql

Example:

mysql -uroot -proot db < db.sql

Comments

2

For Windows OS, you can use the below command to import data from an SQL dump.

C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -u<> -p<> DBName < filelocation\query.sql

Where -u is the username, and -p is the MySQL password. Then enter your password and wait for data to import.

Comments

2
  • Create a database in MySQL.

  • Then go to your computer directory C:\xampp\mysql\bin, write cmd in the address bar, and hit Enter.

  • Unzip your SQL file

  • Then write: mysql -u root -p dbname and press Enter.

  • Write: source sql.file. Like Source C:\xampp\htdocs\amarbazarltd\st1159.sql

  • Done

3 Comments

Upvote for the Windows trick of getting cmd to open in the correct directory.
Re "go to your computer directory": Do you mean in File Explorer?
@PeterMortensen Yes File Explorer
1

In Ubuntu

 mysql -u root -p
 CREATE database dbname;
 use dbname;
 source /home/computername/Downloads/merchantapp.sql
 exit;

In Windows

Download the SQL file and save it in C:\xampp\mysql\bin.

After that, open the command prompt with C:\xampp\mysql\bin:

 C:\xampp\mysql\bin> mysql -u username -p database_name < file.sql

1 Comment

But this is not from the command line in the spirit of the question. It is using the MySQL shell interactively.
1

If you are importing to your local database server, you can do the following:

mysql -u database_user -p < database_file.sql

For a remote database server do the follwing:

mysql -u database_user -p -h remote_server_url < database_file.sql

Comments

1

Export particular databases:

mysqldump --user=root --host=localhost --port=3306 --password=test -B CCR KIT > ccr_kit_local.sql

This will export CCR and KIT databases...

Import all exported databases to a particular MySQL instance (you have to be where your dump file is):

mysql --user=root --host=localhost --port=3306 --password=test < ccr_kit_local.sql

Comments

1
USE yourdb; // Database name  

SOURCE D:/your folder path/Folder/filetoimport.sql;

1 Comment

What if there are spaces in the SQL file path?
1
  1. copy the database file to C:\xampp\mysql\bin

  2. open a terminal from the same path

  3. type in the terminal

     .\mysql.exe -uroot
    
  4. type use DB_NAME; // Change to your DB Name

  5. type source DB_FILE.sql;

Comments

1

If you are using MAMP on Mac OS X, this may be helpful:

/applications/MAMP/library/bin/mysql -u MYSQL_USER -p DATABASE_NAME < path/to/database_sql/FILE.sql

MYSQL_USER is root by default.

Comments

1

If your folder has multiple SQL files, and you've installed Git Bash you can use this command to import multiple files:

cd /my-project/data

cat *.sql | /c/xampp/mysql/bin/mysql -u root -p 1234 myProjectDbName

Comments

0

If you are using XAMPP then go to folder xapppmysqlbin. Open cmd here and paste this:

mysql -u root -p dbname < dbfilename.sql

Comments

0

You can use these steps as easily.

  1. Download the SQL file into your "mysql/bin" folder.

  2. Open the "mysql/bin" folder using CMD.

  3. If not exists required database, then create the database first.

  4. Type this in the CMD and run:

    mysql -u <user> -p<password> <dbname> < file.sql

    "file.sql" is an SQL file that you want to insert into the target database. examples: If your "password" is "1234", "user" is "root", and "dbname" is "test":

     mysql -u root -p1234 test < file.sql
    

    If your "password" is null & "user" is "root" & "dbname" is "test"

     mysql -u root test < file.sql
    
  5. Check the target data successfully uploaded or not.

This method can be used to upload the large size data using SQL files in the CMD.

Make sure in step 4, if you use that password, insert "-p" as a single word without any spaces.

Comments

0

Most of the answers include > or < characters which is not a proper method for all the cases. I recommend using mysqlimport while you may make the dump file using mysqldump.

These tools will be installed with the mysql service and both are available for backup and restore in a database or multiple databases in MySQL.

Here is the way you could leverage it for importing to the mysql

mysqlimport -u database_admin -p database_name ~/path/to/dump_file.sql

In case you do not have it, please install it via:

sudo apt update sudo apt install mysql-client

In the same way, you make a backup to a dump file as follows:

mysqldump [options] --result-file=dump_file.sql

Comments

0

Duplicate answer

Many of the aforementioned solutions are OS specific and require multiple steps. The best solution will be to use a one line command to create the database, select the database, and import the database, by using the command line parameter -e, which stands for execute, and it allows the user to execute application console related and database management system related commands.

[PATH_TO_MYSQL]\mysql -u YOUR_USER_ID -p -e 'CREATE DATABASE [DATABASE_NAME];USE [DATABASE_NAME];SOURCE [DATABASE_NAME_PATH];'

# USE '/' AS THE PATH SEPARATOR CHARACTER WITHIN THE SOURCE QUERY ON ALL OPERATING SYSTEMS

# (e.g.) 
mysql -u root -p -e "CREATE DATABASE Theta_FTP;USE Theta_FTP;SOURCE C:/Users/teodo/Desktop/Theta_FTP.sql;"

Comments

-3

Try this:

cd C:\xampp\mysql\bin
mysql -u root -p database_name --force < C:\file.sql

1 Comment

Please add some explanation to your answer - why did the given call resolve syntax errors?
-4
  1. Go to your wamp or xampp directory

    Example

    cd d:/wamp/bin/mysql/mysql5.7.24/bin
    
  2. mysql -u root -p DATABASENAME < PATHYOUDATABASE_FILE

Comments

1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.