为什么MySQL root密码为空在shell脚本中不起作用?

多年来,Debian / Ubuntu系统管理员已使用debconf-set-selections实用程序在APT开始实际安装软件包之前为MySQL根用户输入密码(例如apt install mysql-server)。

但是,如果不使用此实用程序安装MySQL,则root用户在mysql.users表中将有一个空白的密码字段,这意味着您可以在命令行中sudo mysql -u root而不输入密码。

如果这行得通,为什么在shell脚本中不行?

例如:

## this works fine in shell scripts (MySQL root user will have a blank password) ##
apt update
apt install mysql-server-8.0

## this only works when typed into the shell (not within a script) ##
mysql --user=root -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'ABC123';"
mysql --user=root -e "FLUSH PRIVILEGES;"

P.S。我知道mysql_secure_installation向导和手动查询(替代),但我想知道为什么在Bash脚本等中使用空白密码会导致MySQL访问失败。它似乎与普通密码无关诸如用户,绑定地址,协议,主机,套接字或常规权限之类的问题。

除了debconf-set-selections和简陋的expect猜测并检查之外,还有解决方案吗?

iCMS 回答:为什么MySQL root密码为空在shell脚本中不起作用?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/1907720.html

大家都在问