3
# Copyright (C) 2002 MySQL AB and Jeremy Cole
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; version 2 of the License.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28
case `echo "testing\c"`,`echo -n testing` in
29
*c*,-n*) echo_n= echo_c= ;;
30
*c*,*) echo_n=-n echo_c= ;;
31
*) echo_n= echo_c='\c' ;;
36
touch $config $command
37
chmod 600 $config $command
42
mysql --defaults-file=$config <$command
47
echo "# mysql_secure_installation config file" >$config
48
echo "[mysql]" >>$config
49
echo "user=root" >>$config
50
echo "password=$rootpass" >>$config
55
while [ $status -eq 1 ]; do
57
echo $echo_n "Enter current password for root (enter for none): $echo_c"
61
if [ "x$password" = "x" ]; then
71
echo "OK, successfully used password, moving on..."
77
echo $echo_n "New password: $echo_c"
80
echo $echo_n "Re-enter new password: $echo_c"
85
if [ "$password1" != "$password2" ]; then
86
echo "Sorry, passwords do not match."
91
if [ "$password1" = "" ]; then
92
echo "Sorry, you can't use an empty password here."
97
do_query "UPDATE mysql.user SET Password=PASSWORD('$password1') WHERE User='root';"
99
echo "Password updated successfully!"
100
echo "Reloading privilege tables.."
101
if ! reload_privilege_tables; then
108
echo "Password update failed!"
115
remove_anonymous_users() {
116
do_query "DELETE FROM mysql.user WHERE User='';"
117
if [ $? -eq 0 ]; then
127
remove_remote_root() {
128
do_query "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';"
129
if [ $? -eq 0 ]; then
136
remove_test_database() {
137
echo " - Dropping test database..."
138
do_query "DROP DATABASE test;"
139
if [ $? -eq 0 ]; then
142
echo " ... Failed! Not critical, keep moving..."
145
echo " - Removing privileges on test database..."
146
do_query "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'"
147
if [ $? -eq 0 ]; then
150
echo " ... Failed! Not critical, keep moving..."
156
reload_privilege_tables() {
157
do_query "FLUSH PRIVILEGES;"
158
if [ $? -eq 0 ]; then
177
echo "Cleaning up..."
178
rm -f $config $command
182
# The actual script starts here
191
echo "NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL"
192
echo " SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!"
196
echo "In order to log into MySQL to secure it, we'll need the current"
197
echo "password for the root user. If you've just installed MySQL, and"
198
echo "you haven't set the root password yet, the password will be blank,"
199
echo "so you should just press enter here."
206
# Set the root password
209
echo "Setting the root password ensures that nobody can log into the MySQL"
210
echo "root user without the proper authorisation."
213
if [ $hadpass -eq 0 ]; then
214
echo $echo_n "Set root password? [Y/n] $echo_c"
216
echo "You already have a root password set, so you can safely answer 'n'."
218
echo $echo_n "Change the root password? [Y/n] $echo_c"
222
if [ "$reply" = "n" ]; then
223
echo " ... skipping."
226
while [ $status -eq 1 ]; do
235
# Remove anonymous users
238
echo "By default, a MySQL installation has an anonymous user, allowing anyone"
239
echo "to log into MySQL without having to have a user account created for"
240
echo "them. This is intended only for testing, and to make the installation"
241
echo "go a bit smoother. You should remove them before moving into a"
242
echo "production environment."
245
echo $echo_n "Remove anonymous users? [Y/n] $echo_c"
248
if [ "$reply" = "n" ]; then
249
echo " ... skipping."
251
remove_anonymous_users
257
# Disallow remote root login
260
echo "Normally, root should only be allowed to connect from 'localhost'. This"
261
echo "ensures that someone cannot guess at the root password from the network."
264
echo $echo_n "Disallow root login remotely? [Y/n] $echo_c"
266
if [ "$reply" = "n" ]; then
267
echo " ... skipping."
275
# Remove test database
278
echo "By default, MySQL comes with a database named 'test' that anyone can"
279
echo "access. This is also intended only for testing, and should be removed"
280
echo "before moving into a production environment."
283
echo $echo_n "Remove test database and access to it? [Y/n] $echo_c"
285
if [ "$reply" = "n" ]; then
286
echo " ... skipping."
294
# Reload privilege tables
297
echo "Reloading the privilege tables will ensure that all changes made so far"
298
echo "will take effect immediately."
301
echo $echo_n "Reload privilege tables now? [Y/n] $echo_c"
303
if [ "$reply" = "n" ]; then
304
echo " ... skipping."
306
reload_privilege_tables
315
echo "All done! If you've completed all of the above steps, your MySQL"
316
echo "installation should now be secure."
318
echo "Thanks for using MySQL!"