如何从 shell 脚本自动登录 MySQL? [英] How to auto login in MySQL from a shell script?
本文介绍了如何从 shell 脚本自动登录 MySQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有密码的用户的 MySQL 服务器.我想在 shell 脚本中执行一些 SQL 查询而不像这样指定密码:
I have a MySQL server with an user with a password. I want to execute some SQL queries in shell scripts without specifying the password like this:
config.sh
:
MYSQL_ROOT="root"
MYSQL_PASS="password"
mysql.sh
:
source config.sh
mysql -u$MYSQL_ROOT -p$MYSQL_PASS -e "SHOW DATABASES"
如何在不指定 -p
和 -u
参数等的情况下简化整个过程以执行 SQL 查询?
How can I simplify the whole process in order to execute SQL queries without specifying the -p
and -u
argument etc.?
推荐答案
试试这个:
if [ $MYSQL_PASS ]
then
mysql -u "$MYSQL_ROOT" -p"$MYSQL_PASS" -e "SHOW DATABASES"
else
mysql -u "$MYSQL_ROOT" -e "SHOW DATABASES"
fi
这篇关于如何从 shell 脚本自动登录 MySQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文