Web Hosting Talk







View Full Version : Running SQL commands from shell


Franki
09-03-2002, 02:47 PM
Hello guys. I want to run some SQL commands from shell, for importing tables into a mySQL database . Could someone please tell me the structure of the command?

Matt Lightner
09-03-2002, 03:03 PM
Assuming that you have your mySQL commands stored in a file in your shell's current working directory named "filaname.sql" , the command to import the file into your database would be as follows:

cat filename.sql | mysql -u <your mySQL username> -p <database name>

You would need to replace "<your mySQL username>" and "<database name>" with the real values, of course. Also, when you run that command, you will be prompted for that user's password.

Hope that helps.

ffeingol
09-03-2002, 03:06 PM
No offense Matt, but you can do it easier than that:

mysql -u USER-NAME -p DATABAE-NAME < /path/to/some-file.sql

Frank

Matt Lightner
09-03-2002, 03:11 PM
Originally posted by ffeingol
No offense Matt, but you can do it easier than that:

mysql -u USER-NAME -p DATABAE-NAME < /path/to/some-file.sql

Frank I wouldn't say that's easier (saves you typing all of 3 letters), but either way works fine. Not many people know that the < character redirects the contents of the filename on the right to the command on the left. Piping the output of a cat makes this more evident.

Franki
09-04-2002, 04:51 AM
Is there any way to give the password automaticcaly from the command? I want that to run as cron job, not at real time, so I won't be able to enter the password.

Tazzman
09-04-2002, 05:54 AM
You should just be able to add that command line to your cron jobs.

ffeingol
09-04-2002, 07:31 AM
Franki,

Sure, you can give the password:

mysql -uDB-USER -PPASSWORD DATABASE-NAME < /path/to/file.sql

Just make sure you chmod the file correctly so everyone on the box can not see the password.

Frank

chuckt101
09-04-2002, 08:08 AM
Originally posted by Site5-Matt
I wouldn't say that's easier (saves you typing all of 3 letters), but either way works fine. Not many people know that the < character redirects the contents of the filename on the right to the command on the left. Piping the output of a cat makes this more evident.

lol not really.. i think piping is more confusing to newbies...

:stickout