Ubuntu 16.04 LTS上的非交互(静默)安装MySQL 5.7

MySQL 5.7(实际上是5.6+)改变了mysql_secure_installation工作方式。 这使得很难find一个工作,沉默,脚本安装Ubuntu 16.04 LTS。 如何安全地以脚本非交互方式安装MySQL?

下面的完整脚本可以放到一个文件中,我们将调用“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 

关于下面的脚本:

  1. 请记住通过将您的密码replace第4行上的问号来设置MYSQL_ROOT_PASSWORD
  2. 如果以root身份运行,则删除sudo。
  3. 脚本安装Expect。 它也可以清除(卸载)预计完成后,如果你取消注释50行。

脚本内容:

 #!/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." 

克莱恩先生的回答很好。 但是,试图在Ubuntu 16.04服务器(bash或fish shell)上运行脚本时遇到以下错误:

 Files/scripts/install_mysql.sh: 7: Files/scripts/install_mysql.sh: Syntax error: redirection unexpected 

所以,我将“#安装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 

然后,它就像一个魅力。

我没有去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密码。

顺便说一句,这是一个无人照pipe的LAMP + WORDPRESS安装的一部分,我试图find你可以在https://github.com/suraj2410/autowordpressinstall