28

I have a MySQL file, db.sql. I have tried to import it using:

mysql -uroot -p[password] db < db.sql

All I get is a listing of mysql commands, or I get a syntax error. The weird thing is I used this file last week and, as far as I know, I'm doing it the same way.

I create the database, then in command line enter the above but it's not working. I've tried being inside mysql and just at command line and nothing seems to be working.

Is there something I should be doing differently in windows or MySQL5? I don't know how the heck I got it to work the first time...

TIA

7 Answers 7

62

Try this instead:

mysql -u root -p
(prompts for password)
use db;
source db.sql
5
  • 3
    You're my hero. Worked perfectly.
    – user80151
    Nov 21, 2009 at 2:08
  • 1
    don't forget to upload .sql file to bin folder :) Aug 14, 2019 at 15:04
  • @PayamKhaninejad Note the context of the question. The OP already has the file right there.
    – wallyk
    Aug 14, 2019 at 16:54
  • @wallyk in case someone like me came from Google Aug 15, 2019 at 3:37
  • Yes, the alternative way worked for me thank you! Dec 15, 2022 at 5:31
5

I found out it is different to run this command from Windows Command Line (cmd.exe) and Windows PowerShell.

Using CMD.exe the command works okay, but in PowerShell I get this error:

mysql -uroot exampledb < exampledb.sql

The '<' operator is reserved for future use.

1
  • 3
    You can't use the < on Windows PowerShell (see here)
    – tvdias
    Aug 8, 2012 at 15:30
1

If you are already logged in the try this it will be very useful, but depend upon the MySQL version, it works on MySQL 5.0

For log in if you are not already logged in.

mysql>[your password]

Other wise, use the database to which you want to import the SQLDump file by command.

mysql>use [your database name]

And then give source the database Dump file path as blow command(If not works the copy Dump database file to the bin folder where the MySQL installed for eg. "C:/programfiles/mysql/mqlserver5.0/bin")

mysql> source [dataBasePath+name.sql or dataBaseName.sql]

0

Not sure if your example was a typo or not, but for starters you need to have a space in between your flags and their values, roughly like this:

mysql -u root -p [password] db < db.sql
2
  • I've tried it both ways. I still just get a long listing of all the commands.
    – user80151
    Nov 21, 2009 at 1:56
  • You can't use the < on Windows PowerShell (see here)
    – tvdias
    Aug 8, 2012 at 15:29
0

I've been using PHP script called "BigDump": http://www.ozerov.de/bigdump.php

0

This perfectly works

mysql>[your password]

Other wise, use the database to which you want to import the SQLDump file by command.

mysql>use [your database name]

And then give source the database Dump file path as blow command(If not works the copy Dump database file to the bin folder where the MySQL installed for eg. "C:/programfiles/mysql/mqlserver5.0/bin")

mysql> source [dataBasePath+name.sql or dataBaseName.sql]EG: source C:.....sql

I am using mysql server 5.5

0

In Windows PowerShell, you can pipe in the contents like so:

Get-Content db.sql | mysql -u root -p [password]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.