web-dev-qa-db-ja.com

Ubuntu 16.04 LTSでのMySQL 5.7の非インタラクティブ(サイレント)インストール

MySQL 5.7(実際には5.6+)はmysql_secure_installation機能します。これにより、Ubuntu 16.04 LTSの動作するサイレントスクリプトインストールを見つけるのが困難になりました。スクリプト化された非インタラクティブな方法でMySQLをどのように安全にインストールしますか?

4

以下の完全なスクリプトは、「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. set MYSQL_ROOT_PASSWORDの4行目の疑問符をパスワードに置き換えることを忘れないでください。
  2. ルートとして実行している場合は、Sudoを削除します。
  3. スクリプトは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."
7

MrCleanの答えは素晴らしかった。しかし、Ubuntu 16.04サーバー(bashまたはfish Shell)でスクリプトを実行しようとすると、次のエラーが発生しました。

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

そこで、「#Install MySQL」の下の2行を代わりに次のように変更しました。

# 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

それから、それは魅力のように働きました。

1
dcarrith

私はmysql_secureバージョンのチェックをしませんでしたが、Ubuntu 16でのMySQL 5.7のサイレントインストール自体は、unputとして読み込まれたパスワードを機能させることができなかったため、それ自体が課題でした。

したがって、デフォルトのパスワードexample 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」をどのように実装するかに注意してください。

ところでこれは無人のLAMPの一部でした+ WORDPRESS私が試みていたインストールです https://github.com/suraj2410/autowordpressinstall で見つけることができます

0
Suraj N
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
0
Matt