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?
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
andUSE 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.
bin
folder of mysql server.source databasefilename.sql
and EnterGo to the directory where you have the MySQL executable.
-u
for username and-p
to prompt for the password:The following steps help to upload
file.sql
to the MySQL database.Step 1: Upload
file.sql.zip
to any directory and unzip thereNote:
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.
We can use this command to import SQL from command line:
For example, if the username is
root
and password ispassword
. And you have a database name asbank
and the SQL file isbank.sql
. Then, simply do like this: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:And if your are in the
Project
directory and your SQL file is in theDesktop
directory. If you want to access it from theProject
directory then you can do like this:Sometimes the port defined as well as the server IP address of that database also matters...