Centos

使用 Centos 的 bash 腳本安裝 MYSQL 5.7

  • October 23, 2018

我正在使用 Centos 7 並嘗試編寫一個腳本,該腳本將在 vagrant 設置期間安裝 mysql 5.7。我知道如何手動更改 root 密碼,但是如何在腳本中編寫呢?

我已經有了這個:

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;

我針對 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

引用自:https://unix.stackexchange.com/questions/476802