Bashスクリプトを使用してCentosにMYSQL 5.7をインストールする

Bashスクリプトを使用してCentosにMYSQL 5.7をインストールする

私はCentos 7を使用しており、vagrantの設定中にmysql 5.7をインストールするスクリプトを作成しようとしています。ルートパスワードを手動で変更する方法を知っていますが、スクリプトにどのように書きますか?

私はすでにこれを持っています:

wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
yum -y localinstall mysql57-community-release-el7-7.noarch.rpm
yum -y install mysql-community-server
service mysqld start

私が知っている手動操作は次のとおりです。一時パスワードを取得する

grep 'temporary' /var/log/mysqld.log

その後、メッセージが表示されたら入力してpassと入力しました。

mysql -u root -p
Enter Passwword:

その後、パスを変更するか、 mysql_secure_installation を実行します。

ALTER USER 'root'@'localhost' IDENTIFIED BY 'Newhakase-labs123@';
flush privileges;

ベストアンサー1

Google CloudのCentOS 7.5インスタンスに対してこれらのコマンドをテストしました。スクリプトの実行が完了したら、新しいパスワードを使用してデータベースサーバーにログインできます。

#!/bin/bash
# Description: Set up MySQL Community Release 5.7

# Get the repo RPM and install it.
wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm 
yum -y install ./mysql57-community-release-el7-7.noarch.rpm 

# Install the server and start it
yum -y install mysql-community-server 
systemctl start mysqld 

# Get the temporary password
temp_password=$(grep password /var/log/mysqld.log | awk '{print $NF}')

# Set up a batch file with the SQL commands
echo "ALTER USER 'root'@'localhost' IDENTIFIED BY 'Newhakase-labs123@'; flush privileges;" > reset_pass.sql

# Log in to the server with the temporary password, and pass the SQL file to it.
mysql -u root --password="$temp_password" --connect-expired-password < reset_pass.sql

おすすめ記事