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

前端之家收集整理的这篇文章主要介绍了在Ubuntu 16.04 LTS上安装MySQL 5.7的非交互式(静默)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
MySQL 5.7(实际上是5.6)改变了MysqL_secure_installation的工作方式.这使得很难找到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

关于以下脚本:

>请记住通过用密码替换第4行上的问号来设置MysqL_ROOT_PASSWORD.
>如果以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."
原文链接:https://www.f2er.com/ubuntu/347918.html

猜你在找的Ubuntu相关文章