MySQL 5.7(实际上是 5.6+)改变了mysql_secure_installation
工作方式。这使得很难找到适用于 Ubuntu 16.04 LTS 的有效、静默、脚本化的安装。如何以脚本化非交互方式安全地安装 MySQL?
答案1
下面的完整脚本可以放入我们称之为“install.sh”的文件中,并通过执行以下操作来执行:
touch install.sh # Create empty file
chmod 700 install.sh # Make executable
nano install.sh # Copy contents into script here
./install.sh # Run it
关于下面的脚本:
- 记得设置 MYSQL_ROOT_PASSWORD将第 4 行的问号替换为您的密码。
- 如果以 root 身份运行,请删除 sudo。
- 该脚本会安装 Expect。如果您取消注释第 50 行,它还会在完成后清除(卸载)Expect。
脚本内容:
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive
MYSQL_ROOT_PASSWORD='?' # SET THIS! Avoid quotes/apostrophes in the password, but do use lowercase + uppercase + numbers + special chars
# Install MySQL
# Suggestion from @dcarrith (http://serverfault.com/a/830352/344471):
echo debconf mysql-server/root_password password $MYSQL_ROOT_PASSWORD | sudo debconf-set-selections
echo debconf mysql-server/root_password_again password $MYSQL_ROOT_PASSWORD | sudo debconf-set-selections
#sudo debconf-set-selections <<< "mysql-server-5.7 mysql-server/root_password password $MYSQL_ROOT_PASSWORD"
#sudo debconf-set-selections <<< "mysql-server-5.7 mysql-server/root_password_again password $MYSQL_ROOT_PASSWORD"
sudo apt-get -qq install mysql-server > /dev/null # Install MySQL quietly
# Install Expect
sudo apt-get -qq install expect > /dev/null
# Build Expect script
tee ~/secure_our_mysql.sh > /dev/null << EOF
spawn $(which mysql_secure_installation)
expect "Enter password for user root:"
send "$MYSQL_ROOT_PASSWORD\r"
expect "Press y|Y for Yes, any other key for No:"
send "y\r"
expect "Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:"
send "2\r"
expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"
send "n\r"
expect "Remove anonymous users? (Press y|Y for Yes, any other key for No) :"
send "y\r"
expect "Disallow root login remotely? (Press y|Y for Yes, any other key for No) :"
send "y\r"
expect "Remove test database and access to it? (Press y|Y for Yes, any other key for No) :"
send "y\r"
expect "Reload privilege tables now? (Press y|Y for Yes, any other key for No) :"
send "y\r"
EOF
# Run Expect script.
# This runs the "mysql_secure_installation" script which removes insecure defaults.
sudo expect ~/secure_our_mysql.sh
# Cleanup
rm -v ~/secure_our_mysql.sh # Remove the generated Expect script
#sudo apt-get -qq purge expect > /dev/null # Uninstall Expect, commented out in case you need Expect
echo "MySQL setup completed. Insecure defaults are gone. Please remove this script manually when you are done with it (or at least remove the MySQL root password that you put inside it."
答案2
MrClean 的回答很棒。但是,当我尝试在 Ubuntu 16.04 服务器(bash 或 fish shell)上运行脚本时,出现了以下错误:
Files/scripts/install_mysql.sh: 7: Files/scripts/install_mysql.sh: Syntax error: redirection unexpected
因此,我将“# Install MySQL”下的两行改为:
# Install MySQL
echo debconf mysql-server/root_password password $MYSQL_ROOT_PASSWORD | \
sudo debconf-set-selections
echo debconf mysql-server/root_password_again password $MYSQL_ROOT_PASSWORD | \
sudo debconf-set-selections
然后,它就像魔法一样起作用了。
答案3
ROOT_SQL_PASS=foo123
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password $ROOT_SQL_PASS"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $ROOT_SQL_PASS"
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server
答案4
我没有进行 mysql_secure 版本检查,但实际上在 Ubuntu 16 中静默安装 MySQL 5.7 本身就是一个挑战,因为我无法让读入的密码正常工作。
因此必须使用默认密码示例 root 进行安装,然后立即将其更改为所需的密码。
希望这可以帮助。
echo "mysql-server-5.7 mysql-server/root_password password root" | sudo debconf-set-selections
echo "mysql-server-5.7 mysql-server/root_password_again password root" | sudo debconf-set-selections
apt-get -y install mysql-server-5.7 mysql-client >> $LOGFILE 2>&1
mysql -u root -proot -e "use mysql; UPDATE user SET authentication_string=PASSWORD('$MYSQLPASSWORD') WHERE User='root'; flush privileges;"
请注意 MySQL 5.7 及更高版本如何实现“authentication_string”来更改 root 密码。
顺便说一句,这是我尝试的无人值守 LAMP + WORDPRESS 安装的一部分,你可以在https://github.com/suraj2410/autowordpressinstall