Execute MySQL Query from linux prompt (not db prompt)

Here’s a clean way to execute MySQL query without evening getting into the DB. Saves a step. But what is important here is that the password for the DB is generated via a script which can be used to connect to the DB.

mysql -uroot -p`python -c "from common_utils import common_lib; print common_lib.mysql_passwd()"` -D dbName -e "select tenant_hostname from tenant_ids"

Similarly, executing a DB backup by getting the password from a script result can be done as follows:

mysqldump -hlocalhost -uroot -p`python -c "from common_utils import common_lib; print common_lib.mysql_passwd()"` dbName tableName --result-file="/tmp/results.sql"
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s