Guest User

Untitled

a guest
Jul 3rd, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. config=".my.cnf.$$"
  2. command=".mysql.$$"
  3.  
  4. rootpass=""
  5. echo_n=
  6. echo_c=
  7.  
  8. set_echo_compat() {
  9. case `echo "testing\c"`,`echo -n testing` in
  10. *c*,-n*) echo_n= echo_c= ;;
  11. *c*,*) echo_n=-n echo_c= ;;
  12. *) echo_n= echo_c='\c' ;;
  13. esac
  14. }
  15.  
  16. prepare() {
  17. touch $config $command
  18. chmod 600 $config $command
  19. }
  20.  
  21. do_query() {
  22. echo $1 >$command
  23. mysql --defaults-file=$config <$command
  24. return $?
  25. }
  26.  
  27. make_config() {
  28. echo "# mysql_secure_installation config file" >$config
  29. echo "[mysql]" >>$config
  30. echo "user=root" >>$config
  31. echo "password=$rootpass" >>$config
  32. }
  33.  
  34. set_root_password() {
  35. password1=${mysqlserver_password}
  36. do_query "UPDATE mysql.user SET Password=PASSWORD('$password1') WHERE User='root';"
  37.  
  38. if [ $? -eq 0 ]; then
  39. echo "Password updated successfully!"
  40. echo "Reloading privilege tables.."
  41. if ! reload_privilege_tables; then
  42. exit 1
  43. fi
  44. echo
  45. rootpass=$password1
  46. make_config
  47. else
  48. echo "Password update failed!"
  49. exit 1
  50. fi
  51.  
  52. return 0
  53. }
  54.  
  55. remove_anonymous_users() {
  56. do_query "DELETE FROM mysql.user WHERE User='';"
  57. if [ $? -eq 0 ]; then
  58. echo " ... Success!"
  59. else
  60. echo " ... Failed!"
  61. exit 1
  62. fi
  63.  
  64. return 0
  65. }
  66.  
  67. remove_remote_root() {
  68. do_query "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';"
  69. if [ $? -eq 0 ]; then
  70. echo " ... Success!"
  71. else
  72. echo " ... Failed!"
  73. fi
  74. }
  75.  
  76. remove_test_database() {
  77. echo " - Dropping test database..."
  78. do_query "DROP DATABASE test;"
  79. if [ $? -eq 0 ]; then
  80. echo " ... Success!"
  81. else
  82. echo " ... Failed! Not critical, keep moving..."
  83. fi
  84.  
  85. echo " - Removing privileges on test database..."
  86. do_query "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'"
  87. if [ $? -eq 0 ]; then
  88. echo " ... Success!"
  89. else
  90. echo " ... Failed! Not critical, keep moving..."
  91. fi
  92.  
  93. return 0
  94. }
  95.  
  96. reload_privilege_tables() {
  97. do_query "FLUSH PRIVILEGES;"
  98. if [ $? -eq 0 ]; then
  99. echo " ... Success!"
  100. return 0
  101. else
  102. echo " ... Failed!"
  103. return 1
  104. fi
  105. }
  106.  
  107. interrupt() {
  108. echo
  109. echo "Aborting!"
  110. echo
  111. cleanup
  112. exit 1
  113. }
  114.  
  115. cleanup() {
  116. echo "Cleaning up..."
  117. rm -f $config $command
  118. }
  119.  
  120. # The actual script starts here
  121.  
  122. DEBIAN_FRONTEND=noninteractive apt-get -y -qq install mysql-server
  123. mysql_install_db
  124.  
  125. prepare
  126. set_echo_compat
  127. password='${mysqlserver_password}'
  128. set_root_password
  129. remove_anonymous_users
  130. remove_remote_root
  131. remove_test_database
  132. reload_privilege_tables
  133. cleanup
Add Comment
Please, Sign In to add comment