1

Is it possible to use a shell script to run this following command?

mysql -u user
use mydb
delete from mydata where row='xx'
2

This is a job for a "here document":

mysql -u user << END_CMDS
use mydb
delete from mydata where row='xx'
END_CMDS

The "here document" is a way of putting the SQL in-line inside the script itself. I'm not sure how the user ID's MySQL password plays into this. You'll have to experiment if that user has a password.

The mysql invocation can be indented in your shell script, but do not indent the token that ends your "here document", which is END_CMDS in my example above.

|improve this answer|||||
2

That command could help you:

mysql -u user -p mydb --execute="delete from mydata where row='xx'"

|improve this answer|||||
  • it's asking me for password and I don't know what password I should enter. – AGamePlayer Oct 13 '15 at 6:38
0

You may be interested to examine some of the scripts on my mythtv-tools web page. They are reasonably good examples of running mysql commands from a shell script, and of building up moderately complicated mysql commands (e.g. with multiple WHERE clauses) based on getopt option parsing.

They all use the same basic technique of constructing an SQL string and piping it into mysql or mysql --batch.

None of them use the --user or --password options, as any user on the system can trawl through /proc (or just run ps) and see the login and password in the command-line options. Instead they rely on the existence of a ~/.my.cnf file containing login credentials.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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