如何无人值守安装Mysql8.0?

如何无人值守安装Mysql8.0?

我有一个 bash 安装脚本,无需任何提示即可自动安装 mysql。

wget -c https://dev.mysql.com/get/mysql-apt-config_0.8.16-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.16-1_all.deb
sudo apt update
sudo apt install -y mysql-server
sudo systemctl start mysql.service
sudo systemctl enable mysql.service

之后我希望执行以下操作来确保安装的安全:

SECURE_MYSQL=$(expect -c "
set timeout 10
spawn mysql_secure_installation
expect \"Would you like to setup VALIDATE PASSWORD plugin?\"
send \"n\r\"
expect \"New password:\"
send \"$DB_ROOT_PASSWORD\r\"
expect \"Re-enter new password:\"
send \"$DB_ROOT_PASSWORD\r\"
expect \"Remove anonymous users?\"
send \"y\r\"
expect \"Disallow root login remotely?\"
send \"y\r\"
expect \"Remove test database and access to it?\"
send \"y\r\"
expect \"Reload privilege tables now?\"
send \"y\r\"
expect eof
")
echo "$SECURE_MYSQL"

最新的 Debian Buster 10.9 似乎发生了一些变化。安装时sudo apt install -y mysql-server它要求我输入 root 密码。后来尝试运行时它崩溃了mysql_secure_installation,因为 root 密码已经设置了。

请问我怎样才能使安装无需任何提示即可进行交互?

文档说:

确保记住您设置的 root 密码。如果用户希望稍后设置密码,可以将对话框中的密码字段留空,然后按“确定”即可;在这种情况下,对于使用 Unix 套接字文件的连接,将通过套接字对等凭据可插入身份验证对服务器的 root 访问权限进行身份验证。您可以稍后使用程序 mysql_secure_installation 设置 root 密码。

如何在安装时将密码留空?

答案1

我找到了解决方案。问题是环境变量DEBIAN_FRONTEND=noninteractive没有传递给 sudo。这就是需要 -E 的地方。

export DEBIAN_FRONTEND=noninteractive
wget -c https://dev.mysql.com/get/${MYSQL_8_file}
sudo -E dpkg -i ${MYSQL_8_file}
sudo apt update
sudo -E apt install -y mysql-server

享受。

相关内容