-
-
Save shukebeta/93738d18c87510ab84aeb1e1d4c1fe0a to your computer and use it in GitHub Desktop.
This is a CentOS8 version for MySQL8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Automate mysql secure installation for Redhat/CentOS systems | |
# | |
# - You can set a password for root accounts. | |
# - You can remove root accounts that are accessible from outside the local host. | |
# - You can remove anonymous-user accounts. | |
# - You can remove the test database (which by default can be accessed by all users, even anonymous users), | |
# and privileges that permit anyone to access databases with names that start with test_. | |
# For details see documentation: http://dev.mysql.com/doc/refman/8.0/en/mysql-secure-installation.html | |
# | |
# Tested on CentOS 8.1 | |
# | |
# Usage: | |
# Setup mysql root password: ./mysql_secure.sh 'your_new_root_password' | |
# Change mysql root password: ./mysql_secure.sh 'your_old_root_password' 'your_new_root_password'" | |
# | |
# Delete package expect when script is done | |
# 0 - No; | |
# 1 - Yes. | |
PURGE_EXPECT_WHEN_DONE=0 | |
NEW_MYSQL_PASSWORD="Change it to your own password, it must be strong enough, such as !@#$%^&^*&234567nnll" | |
# | |
# Check the bash shell script is being run by root | |
# | |
if [[ $EUID -ne 0 ]]; then | |
echo "This script must be run as root" 1>&2 | |
exit 1 | |
fi | |
# | |
# Check is expect package installed | |
# | |
if ! rpm -q "expect"; then | |
echo "Can't find expect. Trying install it..." | |
dnf -y install expect | |
fi | |
SECURE_MYSQL=$(expect -c " | |
set timeout 3 | |
spawn mysql_secure_installation | |
expect \"Would you like to setup VALIDATE PASSWORD component?\" | |
send \"y\r\" | |
expect \"Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:\" | |
send \"2\r\" | |
expect \"New password:\" | |
send \"$NEW_MYSQL_PASSWORD\r\" | |
expect \"Re-enter new password:\" | |
send \"$NEW_MYSQL_PASSWORD\r\" | |
expect \"Do you wish to continue with the password provided?\" | |
send \"y\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 | |
") | |
# | |
# Execution mysql_secure_installation | |
# | |
echo "${SECURE_MYSQL}" | |
if [ "${PURGE_EXPECT_WHEN_DONE}" -eq 1 ]; then | |
# Uninstalling expect package | |
dnf -y remove expect | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment