bash 带有特殊字符的 Mysql 命令行密码不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46663630/
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-09-18 16:31:28  来源:igfitidea点击:

Mysql command line password with special characters is not working

mysqlbash

提问by myprogram

I've the following bash script to upgrade my database schema. Script reads hostname and database password from command line.

我有以下 bash 脚本来升级我的数据库架构。脚本从命令行读取主机名和数据库密码。

The problem is here that if the password is alphanumerice.g r00tthen script works. But if password contains special characters e.g pa**w0rd, then script does not work and directly exits. Please help me with this. Thanks.

问题在于,如果密码是字母数字,例如r00t,则脚本可以工作。但如果密码包含特殊字符,例如pa**w0rd,则脚本不起作用并直接退出。请帮我解决一下这个。谢谢。

#!/bin/bash

echo "Enter hostname."
read -p "Hostname [localhost]: " DB_HOST
DB_HOST=${DB_HOST:-localhost}

echo "Enter MySQL root password"
DB_PASS=
while [[ $DB_PASS = "" ]]; do
   read -sp "Password: " DB_PASS
done

MYSQL="mysql --force --connect-timeout=90 --host=$DB_HOST -u root --password=${DB_PASS}"

# Apply schema updates. My DBName is "mydb"
# Upgrade schema file is stored in "mysql" folder

$MYSQL mydb -e exit > /dev/null 2>&1 && $MYSQL mydb < "../mysql/upgrade_schema_v.2.1.sql"

回答by Dheeraj Gupta

Logging into mysql using bash

使用 bash 登录 mysql

For ubuntu or linux shell try to use command

对于 ubuntu 或 linux shell 尝试使用命令

mysql -u username -p'p@ssw()rD'

for remote host login use

用于远程主机登录使用

mysql -h hostname -u user -p'password'

回答by Ben

This is occurring because you are using shell GLOB (wildcard) characters in the password, and in Bash (or on Linux generally) wildcards are expanded by the shell.

发生这种情况是因为您在密码中使用 shell GLOB(通配符)字符,并且在 Bash(或通常在 Linux 上)中,shell 扩展了通配符。

The safest and most reliable solution is to not use shell wildcard characters or other characters interpreted by the shell in the password. You should also avoid spaces. There are plenty of other characters.

最安全和最可靠的解决方案是不要在密码中使用 shell 通配符或其他由 shell 解释的字符。您还应该避免使用空格。还有很多其他角色。

Here are the ones you should avoid:

以下是你应该避免的:

" ' $ , [ ] * ? { } ~ # % \ < > | ^ ;

" ' $ , [ ] * ? { } ~ # % \ < > | ^ ;

Here are the ones it is usually safe to use:

以下是通常可以安全使用的方法:

: @ . , / + - ! =

:@。, / + - !=

To ensure the password is still secure, make it longer. As an example:

为确保密码仍然安全,请延长密码。举个例子:

K@3amvv7l1wz1192sjqhym

K@3amvv7l1wz1192sjqhym

This meets old-fashioned password complexity rules, because upper, lower, numbers and special characters are in the first four, the remainder is randomly generated but avoids any problematic characters.

这符合老式的密码复杂性规则,因为大、小、数字和特殊字符在前四位,余数是随机生成的,但避免了任何有问题的字符。

However if you must use them, you can quote the password parameter with single quotes - though you will still run in to trouble if the password contains single quotes!

但是,如果您必须使用它们,您可以用单引号引用密码参数 - 尽管如果密码包含单引号,您仍然会遇到麻烦!

回答by Eric Renouf

Variables are best used for data, not code. The layers of variables make it hard to protect the expansion when you want some parts of the expansion (i.e., you want your command line to be word split on the tokens you want), but don't want all the other effects. The solution is to not store the code in a string. Instead, use a function like:

变量最好用于数据,而不是代码。当您想要扩展的某些部分(即,您希望命令行在所需的标记上进行分词)但不想要所有其他效果时,变量层使保护扩展变得困难。解决方案是不要将代码存储在字符串中。相反,使用如下函数:

do_mysql() {
    host=""
    pass=""
    mysql --force --connect-timeout=90 --host="$host" -u root --password="$pass" "$@"
}

then you can run it with extra arguments like

然后你可以用额外的参数来运行它,比如

do_mysql "$DB_HOST" "$DB_PASS" -e exit > /dev/null && do_mysql "$DB_HOST" "$DB_PASS" < "../mysql/upgrade_schema_v.2.1.sql"

Though it would also be better not to use upper case for your variables. Doing so makes it so you could collide with environment variables and accidentally change things you don't intend to change (as the number of people who accidentally reset PATHcan attest).

尽管最好不要对变量使用大写。这样做会使您与环境变量发生冲突并意外更改您不打算更改的内容(正如意外重置的人数PATH可以证明的那样)。

回答by Yusuf Hassan

Try enclosing your password in single quotes.

尝试将您的密码用单引号括起来。

If it's pa**w0rd, use 'pa**w0rd'

如果是pa**w0rd,请使用'pa**w0rd'