Jump to content

Import SQL file command line

- - - - -

  • Please log in to reply
17 replies to this topic

#1
Ronin

Ronin

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 309 posts
I have a SQL generated file and I would like to import it to MySQL via the command line. Can anyone post the method to do this?

#2
mylegoh

mylegoh

    Newbie

  • Members
  • PipPip
  • 25 posts
From msdos:
mysql -u username -p database_name < textfilewithsqlstatments.sql

From unix:
similar, but it has been a while so I cannot remember the exact syntax, it should not be too different.

Edited by John, 15 July 2009 - 05:56 AM.


#3
Ronin

Ronin

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 309 posts
yeah, it is the same command in Linux. I forgot to post that I figured this out afterwards.

#4
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
Except the password doesn't need to be there. Just:

mysql -u username -p < textfile.sql

#5
Atmaweapon

Atmaweapon

    Newbie

  • Members
  • Pip
  • 1 posts
i had to include the database name:
mysql -u <username> -p <database> <textfile.sql>

#6
zerostar

zerostar

    Newbie

  • Members
  • PipPip
  • 12 posts
heya :)

I am copying a .sql db form a production server to a dev server. Do I have to place the .sql file in a certain directory to do the import from the cmd line? I can't seem to be able to complete the import.

I'm running the latest revision of wampserver.

thanks!

#7
Guest_Jordan_*

Guest_Jordan_*
  • Guests
If you execute it as:

mysql -u <username> -p <database> textfile.sql

Then the file needs to be in the same directory you are currently in. If you know where the file is you can do:

mysql -u <username> -p <database> /path/to/file/textfile.sql

#8
zerostar

zerostar

    Newbie

  • Members
  • PipPip
  • 12 posts
Thanks for the reply :)

I'm still getting an ERROR 1604 (syntax). Here is what I'm entering at the mysql> prompt:

mysql -u username -p databasename < dbtoimport.sql;

is this correct?

btw - this is from a sqldump from the Prod server.

#9
Guest_Jordan_*

Guest_Jordan_*
  • Guests
No, remove the ;

#10
zerostar

zerostar

    Newbie

  • Members
  • PipPip
  • 12 posts
hmm...tried that but it just goes to the next line. sorry 'bout the n00bness ;)

I already created the DB and I'm just trying to import the structure and data from the dump file - is this procedure correct? They are named the same - would that present this problem?

#11
zerostar

zerostar

    Newbie

  • Members
  • PipPip
  • 12 posts
ok figured out what my issue was :o and yes - n00bness at work...

I had already logged into mysql.exe when I was trying to execute the command. I had to back out to the bin directory and then run the command.

up and running (*yay!*)
::
[ z ]

#12
Crane

Crane

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 398 posts
From inside mysql you can paste the entire contents of the SQL dump. that will also work although I've not tested it.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users