Chad Don Cooper Projects and ponderings…

17Nov/170

Automated install of MySQL on CentOS 7

Run the scirpt below using the first argument for the root user password.

#!/bin/bash

NEW_MYSQL_ROOT_PASSWORD=$1

rpm -Uvh https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
yum install mysql-server expect -y
systemctl enable mysqld
systemctl start mysqld

TMP_MYSQL_ROOT_PASSWORD=$(grep -oP 'root@localhost: (.+)$' /var/log/mysqld.log)
TMP_MYSQL_ROOT_PASSWORD="${TMP_MYSQL_ROOT_PASSWORD/root@localhost: /}"

SECURE_MYSQL=$(expect -c "
set timeout 10
spawn mysql_secure_installation
expect \"Enter current password for root:\"
send \"$TMP_MYSQL_ROOT_PASSWORD\r\"
expect \"New password:\"
send \"$NEW_MYSQL_ROOT_PASSWORD\r\"
expect \"Re-enter new password:\"
send \"$NEW_MYSQL_ROOT_PASSWORD\r\"
expect \"Change the password for root\"
send \"y\r\"
expect \"New password:\"
send \"$NEW_MYSQL_ROOT_PASSWORD\r\"
expect \"Re-enter new password:\"
send \"$NEW_MYSQL_ROOT_PASSWORD\r\"
expect \"Remove anonymous users?\"
send \"y\r\"
expect \"Disallow root login remotely?\"
send \"y\r\"
expect \"Remove test database and access to it?\"
send \"y\r\"
expect \"Reload privilege tables now?\"
send \"y\r\"
expect eof
")

echo "$SECURE_MYSQL"

yum remove expect -y

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.