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 Ser
Sometimes the port defined as well as the server IP address of that database also matters...
mysql -u user -p user -h <Server IP> -P<port> (DBNAME) < DB.sql
We can use this command to import SQL from command line:
mysql -u username -p password db_name < file.sql
For example, if the username is root
and password is password
. And you have a database name as bank
and the SQL file is bank.sql
. Then, simply do like this:
mysql -u root -p password bank < bank.sql
Remember where your SQL file is. If your SQL file is in the Desktop
folder/directory then go the desktop directory and enter the command like this:
~ ? cd Desktop
~/Desktop ? mysql -u root -p password bank < bank.sql
And if your are in the Project
directory and your SQL file is in the Desktop
directory. If you want to access it from the Project
directory then you can do like this:
~/Project ? mysql -u root -p password bank < ~/Desktop/bank.sql
To import a single database, use the following command.
mysql -u username -p password dbname < dump.sql
To import multiple database dumps, use the following command.
mysql -u username -p password < dump.sql
You do not need to specify the name of the database on the command line if the .sql file contains CREATE DATABASE IF NOT EXISTS db_name
and USE db_name
statements.
Just make sure you are connecting with a user that has the permissions to create the database, if the database mentioned in the .sql file does not exist.
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.
I kept running into the problem where the database wasn't created.
I fixed it like this
mysql -u root -e "CREATE DATABASE db_name"
mysql db_name --force < import_script.sql