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
Try:
mysql -u username -p database_name < file.sql
Check MySQL Options.
Note-1: It is better to use the full path of the SQL file file.sql
.
Note-2: Use -R
and --triggers
to keep the routines and triggers of original database. They are not copied by default.
Note-3 You may have to create the (empty) database from mysql if it doesn't exist already and the exported SQL don't contain CREATE DATABASE
(exported with --no-create-db
or -n
option), before you can import it.
If you already have the database, use the following to import the dump
or the sql
file:
mysql -u username -p database_name < file.sql
if you don't you need to create the relevant database(empty) in MySQL, for that first log on to the MySQL
console by running the following command in terminal or in cmd
mysql -u userName -p;
And when prompted provide the password.
Next, create a database and use it:
mysql>create database yourDatabaseName;
mysql>use yourDatabaseName;
Then import the sql
or the dump
file to the database from
mysql> source pathToYourSQLFile;
Note: if your terminal is not in the location where the dump
or sql
file exists, use the relative path in above.
Go to drive:
command: d:
MySQL login
command: c:\xampp\mysql\bin\mysql -u root -p
It will ask for pwd. Enter it:
pwd
Select the database
use DbName;
Provide the file name
\.DbName.sql
Go to the directory where you have MySQL.
c:\mysql\bin\> mysql -u username -p password database_name <
filename.sql
Also to dump all databases, use the -all-databases
option, and no databases’ name needs to be specified anymore.
mysqldump -u username -ppassword –all-databases > dump.sql
Or you can use some GUI clients like SQLyog to do this.
Add the --force option:
mysql -u username -p database_name --force < file.sql
A common use of mysqldump is for making a backup of an entire database:
shell> mysqldump db_name > backup-file.sql
You can load the dump file back into the server like this:
UNIX
shell> mysql db_name < backup-file.sql
The same in Windows command prompt:
mysql -p -u [user] [database] < backup-file.sql
PowerShell
C:\> cmd.exe /c "mysql -u root -p db_name < backup-file.sql"
MySQL command line
mysql> use db_name;
mysql> source backup-file.sql;