多年来,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
猜测并检查之外,还有解决方案吗?