How to run a MySQL command from the command line

How to run a MySQL query using the command line:

Use -e to execute the command. Here is what that will look like:

mysql -u  -p -e "create database database_name"

Here is another more complex example. In this case a query needed to be created to reset a CRON that synchronized with a foreign system. We needed to override this query on a regular basis.

mysql -uusername -ppassword database_name -e "update domains set registrar='abc' where domain not like '%net.za' and domain not like '%web.za' and domain not like '%co.za' and domain not like '%.org.za' and registrar = 'xyz'"

The beauty of these one liners is you can easily add them as a cron, e.g.:

sh reset_register.sh

Reference

Run a single MySQL query from the command line

 

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top