mysql commands in shell script

They have: 1 posts

Joined: Mar 2007

I need to create a shell script that will be run from cron to acccess a mysql database and execute a query.

I don't have much experience with shell scripting and I have only been able to get the script to login to mysql. However, once the script logs in, it does not run the querry, it only returns a mysql command prompt.

The script needs to login to the database and delete all rows in a table based on a search of one field.

Can anyone give me any suggestions on this.

Thanks

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

This may not be the most efficient answer, but it's what I do - write a PHP script that executes the query and call it by putting...

php -q path/to/file.php

...in the cron tab.

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

I just looked at the mysql man page and saw this:

You can execute SQL statements in a script file (batch file) like this:
shell> mysql db_name < script.sql > output.tab
'

That may be what you are looking for.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.