无法从外部bash脚本正确设置MySQL密码 [英] Can't properly set MySQL password from external bash script
问题描述
我有两个脚本-主要的脚本执行一些不同的操作并调用第二个脚本,第二个脚本安装MySQL.
I have two scripts - The main one that does a few different things and calls the second script, and the second script that installs MySQL.
在我的主脚本中,我做这样的事情:
From my main script I do something like this:
...
read -p "Set the password for the database [min. 4 characters]: " MPASS
# Install MySQL
path/to/setup-mysql.sh "$MPASS"
mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO root@'${IPADDRESS}' IDENTIFIED BY '${MPASS}';"
mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO root@'%' IDENTIFIED BY '${MPASS}';"
service mysql restart
mysql --user="root" --password=${MPASS} -e "SET GLOBAL validate_password_policy = 'LOW'; SET GLOBAL validate_password_length = 4; CREATE USER 'johndoe'@'${IPADDRESS}' IDENTIFIED BY '${MPASS}';"
mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO 'johndoe'@'${IPADDRESS}' IDENTIFIED BY '${MPASS}' WITH GRANT OPTION;"
mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO 'johndoe'@'%' IDENTIFIED BY '${MPASS}' WITH GRANT OPTION;"
mysql --user="root" --password=${MPASS} -e "FLUSH PRIVILEGES;"
setup-mysql.sh
看起来像这样:
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive
debconf-set-selections <<< "mysql-community-server mysql-community-server/data-dir select ''"
debconf-set-selections <<< "mysql-community-server mysql-community-server/root-pass password ${1}"
debconf-set-selections <<< "mysql-community-server mysql-community-server/re-root-pass password ${1}"
apt-get install -y mysql-server
# Start mysql on boot
update-rc.d mysql defaults
# Configure Password Expiration
echo "default_password_lifetime = 0" >> /etc/mysql/my.cnf
# Configure Access Permissions For Root
sed -i '/^bind-address/s/bind-address.*=.*/bind-address = */' /etc/mysql/my.cnf
对我来说,这看起来应该可行,但是,当bash执行此行时:
To me this looks like it should work, however, when bash executes this line:
mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO root@'${IPADDRESS}' IDENTIFIED BY '${MPASS}';"
表示密码错误.当我尝试手动登录时,它将无法正常工作.当我跳过密码字段(mysql -u root
)时,它可以工作.因此根本没有设置密码.
It says that the password is wrong. When I try logging in manually it wont work. And when I skip the password field (mysql -u root
) it works. So the password is not being set at all.
此外,当我在setup-mysql.sh
中执行echo $1
时,它会正确显示MPASS
包含的内容.
Also, when I do echo $1
in setup-mysql.sh
, it properly shows what MPASS
contains.
为什么debconf-set-selections
不能为我的MySQL安装正确设置密码.我真的很困惑.
Why isn't debconf-set-selections
setting the password properly for my MySQL installation. I'm really confused here.
推荐答案
似乎您正在编写MySQL安装后初始化Shell脚本.
It seems that you are writing a MySQL post-installation initialization shell script.
我个人认为:更改您的Shell脚本设计方法.
在脚本中,使用mysql --user="root" --password=${MPASS}
提示错误信息.您可以使用mysql参数 --defaults-file
,将mysql登录用户root
和相应的密码写入临时文件,例如
In your script, you use mysql --user="root" --password=${MPASS}
prompt error info. You could use mysql paramater --defaults-file
, write mysql login user root
and corresponding password into a temp file like
[client]
user=root
password=YOURPASSWORD
假设文件路径为/tmp/mysql.cnf
(分配变量mysql_my_cnf
)
suppose file path is /tmp/mysql.cnf
(assign variable mysql_my_cnf
)
然后使用mysql --defaults-file="$mysql_my_cnf"
登录,它将成功登录.
Then use mysql --defaults-file="$mysql_my_cnf"
login, it will login successfully.
将变量分配给mysql --defaults-file="$mysql_my_cnf"
,例如mysql_command
mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO root@'${IPADDRESS}' IDENTIFIED BY '${MPASS}';"
新方法
${mysql_command} -e "GRANT ALL ON *.* TO root@'${IPADDRESS}' IDENTIFIED BY '${MPASS}';"
您可以尝试.
我写了一个 MySQL及其变体的安装和初始化Shell脚本,对您可能会有帮助.
I wrote a MySQL and its variants installation and initialization shell script, it may be helpful for you.
这篇关于无法从外部bash脚本正确设置MySQL密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!