Monday, July 4, 2022

How to execute MySQL command from command line

 Being able to execute a MySQL command from command line or from a Shell Script is very useful sometime.  A good example would be to schedule a particular MySQL SQL command to be executed in crontab.

Here is the syntax:

mysql -u root -p 123password123 -D mydatabase -e "SELECT * FROM product WHERE edu_price > 0 INTO OUTFILE '/tmp/edu.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY 'n';"

Break down details of the individual options:

-u (user)

-p (password)

-D (database)

 -e (execute command)


No comments:

Post a Comment