You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
7.5 KiB

  1. #-----------------------------------------------------------------------------
  2. # Copyright (C) 2002 MySQL AB and Jeremy Cole
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  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.
  13. #
  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
  17. #-----------------------------------------------------------------------------
  18. #-----------------------------------------------------------------------------
  19. # This notice applies to changes, created by or for Novell, Inc.,
  20. # to preexisting works for which notices appear elsewhere in this file.
  21. # Copyright (c) 2003 Novell, Inc. All Rights Reserved.
  22. # This program is free software; you can redistribute it and/or modify
  23. # it under the terms of the GNU General Public License as published by
  24. # the Free Software Foundation; either version 2 of the License, or
  25. # (at your option) any later version.
  26. # This program is distributed in the hope that it will be useful,
  27. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. # GNU General Public License for more details.
  30. # You should have received a copy of the GNU General Public License
  31. # along with this program; if not, write to the Free Software
  32. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  33. #-----------------------------------------------------------------------------
  34. use strict;
  35. use Mysql;
  36. print "MySQL Secure Installation Script\n\n";
  37. print "NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL\n";
  38. print " SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!\n\n";
  39. #-----------------------------------------------------------------------------
  40. # get the current root password
  41. #-----------------------------------------------------------------------------
  42. print "In order to log into MySQL to secure it, we'll need the current\n";
  43. print "password for the root user. If you've just installed MySQL, and\n";
  44. print "you haven't set the root password yet, the password will be blank,\n";
  45. print "so you should just press enter here.\n\n";
  46. print "Enter the current password for root: ";
  47. my $password = <STDIN>;
  48. chomp $password;
  49. print "\n";
  50. my $conn = Mysql->connect("localhost", "mysql", "root", $password)
  51. || die "Unable to connect to MySQL.";
  52. print "OK, successfully used the password, moving on...\n\n";
  53. #-----------------------------------------------------------------------------
  54. # set the root password
  55. #-----------------------------------------------------------------------------
  56. unless ($password)
  57. {
  58. print "Setting the root password ensures that no one can log into MySQL\n";
  59. print "using the root user without the proper authorization.\n\n";
  60. print "Set root password (Y/N)? ";
  61. my $reply = <STDIN>;
  62. chomp $reply;
  63. print "\n";
  64. if ($reply =~ /Y/i)
  65. {
  66. print "New password for root: ";
  67. my $pass1 = <STDIN>;
  68. chomp $pass1;
  69. print "\n";
  70. print "Re-enter new password for root: ";
  71. my $pass2 = <STDIN>;
  72. chomp $pass2;
  73. print "\n";
  74. unless ($pass1 eq $pass2) { die "Sorry, the passwords do not match."; }
  75. unless ($pass1) { die "Sorry, you can't use an empty password here."; }
  76. $conn->query("SET PASSWORD FOR root\@localhost=PASSWORD('$pass1')")
  77. || die "Unable to set password.";
  78. print "OK, successfully set the password, moving on...\n\n";
  79. }
  80. else
  81. {
  82. print "WARNING, the password is not set, moving on...\n\n";
  83. }
  84. }
  85. #-----------------------------------------------------------------------------
  86. # remove anonymous users
  87. #-----------------------------------------------------------------------------
  88. print "By default, a MySQL installation has anonymous users, allowing anyone\n";
  89. print "to log into MySQL without having to have a user account created for\n";
  90. print "them. This is intended only for testing, and to make the installation\n";
  91. print "go a bit smoother. You should remove them before moving into a\n";
  92. print "production environment.\n\n";
  93. print "Remove anonymous users (Y/N)? ";
  94. my $reply = <STDIN>;
  95. chomp $reply;
  96. print "\n";
  97. if ($reply =~ /Y/i)
  98. {
  99. $conn->query("DELETE FROM mysql.user WHERE user=''")
  100. || die "Unable to remove anonymous users.";
  101. print "OK, successfully removed anonymous users, moving on...\n\n";
  102. }
  103. else
  104. {
  105. print "WARNING, the anonymous users have not been removed, moving on...\n\n";
  106. }
  107. #-----------------------------------------------------------------------------
  108. # disallow remote root login
  109. #-----------------------------------------------------------------------------
  110. print "Normally, root should only be allowed to connect from 'localhost'. This\n";
  111. print "ensures that someone cannot guess at the root password from the network.\n\n";
  112. print "Disallow remote root login (Y/N)? ";
  113. my $reply = <STDIN>;
  114. chomp $reply;
  115. print "\n";
  116. if ($reply =~ /Y/i)
  117. {
  118. $conn->query("DELETE FROM mysql.user WHERE user='root' AND host!='localhost'")
  119. || die "Unable to disallow remote root login.";
  120. print "OK, successfully disallowed remote root login, moving on...\n\n";
  121. }
  122. else
  123. {
  124. print "WARNING, remote root login has not been disallowed, moving on...\n\n";
  125. }
  126. #-----------------------------------------------------------------------------
  127. # remove test database
  128. #-----------------------------------------------------------------------------
  129. print "By default, MySQL comes with a database named 'test' that anyone can\n";
  130. print "access. This is intended only for testing, and should be removed\n";
  131. print "before moving into a production environment.\n\n";
  132. print "Remove the test database (Y/N)? ";
  133. my $reply = <STDIN>;
  134. chomp $reply;
  135. print "\n";
  136. if ($reply =~ /Y/i)
  137. {
  138. $conn->query("DROP DATABASE IF EXISTS test")
  139. || die "Unable to remove test database.";
  140. $conn->query("DELETE FROM mysql.db WHERE db='test' OR db='test\\_%'")
  141. || die "Unable to remove access to the test database.";
  142. print "OK, successfully removed the test database, moving on...\n\n";
  143. }
  144. else
  145. {
  146. print "WARNING, the test database has not been removed, moving on...\n\n";
  147. }
  148. #-----------------------------------------------------------------------------
  149. # reload privilege tables
  150. #-----------------------------------------------------------------------------
  151. print "Reloading the privilege tables will ensure that all changes made so far\n";
  152. print "will take effect immediately.\n\n";
  153. print "Reload privilege tables (Y/N)? ";
  154. my $reply = <STDIN>;
  155. chomp $reply;
  156. print "\n";
  157. if ($reply =~ /Y/i)
  158. {
  159. $conn->query("FLUSH PRIVILEGES")
  160. || die "Unable to reload privilege tables.";
  161. print "OK, successfully reloaded privilege tables, moving on...\n\n";
  162. }
  163. else
  164. {
  165. print "WARNING, the privilege tables have not been reloaded, moving on...\n\n";
  166. }
  167. #-----------------------------------------------------------------------------
  168. # done
  169. #-----------------------------------------------------------------------------
  170. print "\n\nAll done! If you've completed all of the above steps, your MySQL\n";
  171. print "installation should now be secure.\n\n";
  172. print "Thanks for using MySQL!\n\n";