简体   繁体   English

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

[英]How to auto login in MySQL from a shell script?

I have a MySQL server with an user with a password.我有一个带有密码的用户的 MySQL 服务器。 I want to execute some SQL queries in shell scripts without specifying the password like this:我想在不指定密码的情况下在 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 -p and -u argument etc.?如何在不指定-p-u参数等的情况下简化整个过程以执行 SQL 查询?

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 -p and the password following it.如果给出 [password is],则--password=-p与其后面的密码之间不能有空格。 If no password option is specified, the default is to send no password.如果未指定密码选项,则默认为不发送密码。

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.cnf file instead.但即使有办法,我仍然建议您改用~/.my.cnf文件。 Arguments on the command line are likely included in a process listing generated by ps -A -ocmd , so other users can see them.命令行上的参数可能包含在ps -A -ocmd生成的进程列表中,因此其他用户可以看到它们。 The .my.cnf file, on the other hand, can (and should) be made readable only by you (using chmod 0600 ~/.my.cnf ), and will be used automatically.另一方面, .my.cnf文件可以(并且应该)只有您可以读取(使用chmod 0600 ~/.my.cnf ),并将自动使用。 Have that file include the following lines:让该文件包含以下几行:

[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.6.1.2.1。 End-User Guidelines for Password Security for the various ways in which you can provide a password, and their respective benefits and drawbacks. 提供密码的各种方式及其各自的优点和缺点的最终用户密码安全指南 See 4.2.3.3.4.2.3.3。 Using Option Files for general information on this .my.cnf file使用选项文件获取有关此.my.cnf文件的一般信息

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

As MvG suggested (recommended in the MySQL manual 4.2.2 connecting and 6.1.2.1 security guidelines ) you should use a file.正如 MvG 建议的(在 MySQL 手册4.2.2 连接6.1.2.1 安全指南中推荐)你应该使用一个文件。 The password on the command line may be unsafe since ps may show it to other users.命令行上的密码可能不安全,因为 ps 可能会将其显示给其他用户。 The file does not have to be .my.cnf, can be an ad-hoc option file for your script in a temporary file:该文件不必是 .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 的形式,以防发生中断。

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.它会打开一个 shell,以便您可以手动执行查询。 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"

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.cnf that 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.如果有效,为了避免记住上面的命令,您还可以创建一些 Bash 别名。 You can do it pasting these commands in your user terminal:您可以在用户终端中粘贴这些命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM