-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmysql_secure.sh
70 lines (53 loc) · 1.45 KB
/
mysql_secure.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
# Check the bash shell script is being run by root
if [[ $EUID -ne 0 ]]; then
echo "[ERROR] This script must be run as root" 1>&2
exit 1
fi
# Check input params
if [ -n "${1}" -a -z "${2}" ]; then
# Setup root password
CURRENT_MYSQL_PASSWORD=''
NEW_MYSQL_PASSWORD="${1}"
elif [ -n "${1}" -a -n "${2}" ]; then
# Change existing root password
CURRENT_MYSQL_PASSWORD="${1}"
NEW_MYSQL_PASSWORD="${2}"
else
echo "Usage:"
echo " Setup MariaDB root password: ${0} 'your_current_root_password'"
echo " Change MariaDB root password: ${0} 'your_old_root_password' 'your_new_root_password'"
exit 1
fi
# Check expect package installed
if ! command -v expect &> /dev/null
then
echo "[ERROR] expect could not be found"
exit 1
fi
SECURE_MYSQL=$(expect -c "
set timeout 3
spawn mysql_secure_installation
expect \"Enter current password for root (enter for none):\"
send \"$CURRENT_MYSQL_PASSWORD\r\"
expect \"unix_socket authentication?\"
send \"y\r\"
expect \"root password?\"
send \"y\r\"
expect \"New password:\"
send \"$NEW_MYSQL_PASSWORD\r\"
expect \"Re-enter new password:\"
send \"$NEW_MYSQL_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
")
# Execute mysql_secure_installation
echo "${SECURE_MYSQL}"
exit 0