How do you run a single query through mysql from t

2019-03-08 12:10发布

I'm looking to be able to run a single query on a remote server in a scripted task.

For example, intuitively, I would imagine it would go something like:

mysql -uroot -p -hslavedb.mydomain.com mydb_production "select * from users;"

5条回答
祖国的老花朵
2楼-- · 2019-03-08 12:43
mysql -uroot -p -hslavedb.mydomain.com mydb_production -e "select * from users;"

From the usage printout:

-e, --execute=name
Execute command and quit. (Disables --force and history file)

查看更多
一纸荒年 Trace。
3楼-- · 2019-03-08 12:44
mysql -u <user> -p -e "select * from schema.table"
查看更多
爷、活的狠高调
4楼-- · 2019-03-08 12:52

If it's a query you run often, you can store it in a file. Then any time you want to run it:

mysql < thefile

(with all the login and database flags of course)

查看更多
狗以群分
5楼-- · 2019-03-08 12:54
echo "select * from users;" | mysql -uroot -p -hslavedb.mydomain.com mydb_production
查看更多
你好瞎i
6楼-- · 2019-03-08 13:07

here's how you can do it with a cool shell trick:

mysql -uroot -p -hslavedb.mydomain.com mydb_production <<< 'select * from users'

'<<<' instructs the shell to take whatever follows it as stdin, similar to piping from echo.

use the -t flag to enable table-format output

查看更多
登录 后发表回答