#!/bin/bash #Reference: https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-centos-7 #Note: Don't use : in passwords, because a wizzard used later, makes use of : as seperators #1: Define variable with the password MYSQL_PASS="Percent-Gope-Dumping-Uninsured-Maybe4" #2: ... if the short hostname is usrXX-hn01 before continuing hostname -s|grep "usr[0-9]\{2\}-hn[0-9]*" if [ $? -ne 0 ]; then echo "This script should be executed on the Head Node only" exit 1 fi #4: Install MySQL as on the webpage curl -sSLO https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm if [ "$(md5sum mysql80-community-release-el7-5.noarch.rpm|sed 's| .*||g')" == "e2bd920ba15cd3d651c1547661c60c7c" ]; then #Note the -y options used, so as per #3 in the assignemet, no user intervention is required yum -y localinstall mysql80-community-release-el7-5.noarch.rpm && yum -y install mysql-server else echo "The md5sum values don't match. We won't continue..." exit 2 fi #5: Enable the service and start it automatically after reboots: sudo systemctl enable mysqld --now #6: Read the content of the temporary password into a variable: temp_pass=$(cat /var/log/mysqld.log |grep -i "A temporary password" |sed "s|.*: ||g") #7: Create the here document cat > secure_mysql.sql <<-EOF ALTER USER 'root'@'localhost' IDENTIFIED BY '$MYSQL_PASS'; DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1'); DELETE FROM mysql.user WHERE User=''; DELETE FROM mysql.db WHERE Db='test' OR Db='test_%'; FLUSH PRIVILEGES; EOF #8: Create a here document ~/my.cnf cat > ~/.my.cnf <