如何从 shell 脚本自动登录 MySQL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13814413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:44:42  来源:igfitidea点击:

How to auto login in MySQL from a shell script?

mysqlbashshell

提问by KeepZero

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:

我有一个带有密码的用户的 MySQL 服务器。我想在不指定密码的情况下在 shell 脚本中执行一些 SQL 查询,如下所示:

config.sh:

config.sh

MYSQL_ROOT="root"
MYSQL_PASS="password"

mysql.sh:

mysql.sh

source config.sh
mysql -u$MYSQL_ROOT -p$MYSQL_PASS -e "SHOW DATABASES"

How can I simplify the whole process in order to execute SQL queries without specifying the -pand -uargument etc.?

如何简化整个过程以便在不指定-p-u参数等的情况下执行 SQL 查询?

采纳答案by Diego Basch

Try this:

尝试这个:

if [ $MYSQL_PASS ]
then
  mysql -u "$MYSQL_ROOT" -p"$MYSQL_PASS" -e "SHOW DATABASES"
else
  mysql -u "$MYSQL_ROOT" -e "SHOW DATABASES"
fi

回答by MvG

Alternative ways to write these options.

编写这些选项的替代方法。

You can write

你可以写

mysql -u "$MYSQL_ROOT" -p"$MYSQL_PASS" -e "SHOW DATABASES"

If [password is] given, there must be no space between --password=or -pand the password following it. If no password option is specified, the default is to send no password.

如果给出 [password is],则--password=-p和后面的密码之间不能有空格。如果未指定密码选项,则默认为不发送密码。

to pass empty strings as separate arguments. Your comment below indicates that the client will still ask for a password, though. Probably it interprets the empty argument as a database name and not as the password. So you could try the following instead:

将空字符串作为单独的参数传递。不过,您在下面的评论表明客户仍会要求输入密码。可能它将空参数解释为数据库名称而不是密码。因此,您可以尝试以下操作:

mysql --user="$MYSQL_ROOT" --password="$MYSQL_PASS" -e "SHOW DATABASES"

.my.cnf file

.my.cnf 文件

But even if there is a way, I'd still suggest you use a ~/.my.cnffile instead. Arguments on the command line are likely included in a process listing generated by ps -A -ocmd, so other users can see them. The .my.cnffile, on the other hand, can (and should) be made readable only by you (using chmod 0600 ~/.my.cnf), and will be used automatically. Have that file include the following lines:

但即使有办法,我仍然建议您改用~/.my.cnf文件。命令行上的参数可能包含在由 生成的进程列表中ps -A -ocmd,因此其他用户可以看到它们。.my.cnf另一方面,该文件可以(并且应该)仅由您(使用chmod 0600 ~/.my.cnf)读取,并将自动使用。让该文件包含以下几行:

[client]
user=root
password=

Then a simple mysql -e "SHOW DATABASES"will suffice, as the client will obtain its credentials from that file.

然后一个简单的mysql -e "SHOW DATABASES"就足够了,因为客户端将从该文件中获取其凭据。

See 6.1.2.1. End-User Guidelines for Password Securityfor the various ways in which you can provide a password, and their respective benefits and drawbacks. See 4.2.3.3. Using Option Filesfor general information on this .my.cnffile

6.1.2.1。提供密码的各种方式及其各自的优点和缺点的最终用户密码安全指南。见4.2.3.3。使用选项文件获取有关此.my.cnf文件的一般信息

回答by marco

As MvG suggested (recommended in the MySQL manual 4.2.2 connectingand 6.1.2.1 security guidelines) you should use a file. The password on the command line may be unsafe since ps may show it to other users. The file does not have to be .my.cnf, can be an ad-hoc option file for your script in a temporary file:

正如 MvG 建议的(在 MySQL 手册4.2.2 连接6.1.2.1 安全指南中推荐)你应该使用一个文件。命令行上的密码可能不安全,因为 ps 可能会将其显示给其他用户。该文件不必是 .my.cnf,可以是临时文件中脚本的临时选项文件:

OPTFILE="$(mktemp -q --tmpdir "${inname}.XXXXXX")$"
trap 'rm -f "$OPTFILE"' EXIT
chmod 0600 "$OPTFILE"
cat >"$OPTFILE" <<EOF
[client]
password="${MYSQL_PASS}"
EOF
mysql --user="$MYSQL_ROOT" --defaults-extra-file="$OPTFILE" -e "SHOW DATABASES"

The first lines create a safe temp file, then put the options then use it. trap will protect you form OPTFILE lying around in case of interrupts.

第一行创建一个安全的临时文件,然后放置选项然后使用它。陷阱将保护您形成 OPTFILE 的形式,以防发生中断。

回答by med116

Here is a little bash script you can use to get a shell really quickly

这是一个小 bash 脚本,您可以使用它来非常快速地获得 shell

It opens up a shell so you can manually execute queries. Very convenient.

它会打开一个外壳,以便您可以手动执行查询。很方便。

  #!/bin/bash
  DB_USER='your_db_username'
  DB_PASS='your_password'
  DB='database_name'
  echo 'logging into db $DB as $DB_USER'
  mysql -u "$DB_USER" --password="$DB_PASS" --database="$DB"

回答by Valerio Bozz

To auto login in MySQL using Debian GNU/Linux distributions (like Ubuntu, Xubuntu, Linux Mint, gNewSense, etc. etc.) you can simply use the already existing file /etc/mysql/debian.cnfthat contains valid superuser credentials.

要使用 Debian GNU/Linux 发行版(如 Ubuntu、Xubuntu、Linux Mint、gNewSense 等)自动登录 MySQL,您只需使用/etc/mysql/debian.cnf包含有效超级用户凭据的现有文件即可。

So, you can try to auto login with this simple command:

因此,您可以尝试使用以下简单命令自动登录:

sudo mysql --defaults-file=/etc/mysql/debian.cnf

If it works, to avoid to remember the above command you can also create some Bash aliases. You can do it pasting these commands in your user terminal:

如果有效,为了避免记住上面的命令,您还可以创建一些 Bash 别名。您可以在用户终端中粘贴这些命令:

echo ''                                                                       >> ~/.bashrc
echo "# Auto-login in MySQL"                                                  >> ~/.bashrc
echo "# From https://stackoverflow.com/a/33584904/3451846"                    >> ~/.bashrc
echo "alias mysql='sudo mysql --defaults-file=/etc/mysql/debian.cnf'"         >> ~/.bashrc
echo "alias mysqldump='sudo mysqldump --defaults-file=/etc/mysql/debian.cnf'" >> ~/.bashrc
. ~/.bashrc

And then just type this to auto login:

然后只需输入以下内容即可自动登录:

mysql