Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #! /bin/sh
  2. #
  3. # Author: Bert Van Vreckem <bert.vanvreckem@gmail.com>
  4. #
  5. # A non-interactive replacement for mysql_secure_installation
  6. #
  7. # Tested on CentOS 6, CentOS 7, Ubuntu 12.04 LTS (Precise Pangolin), Ubuntu
  8. # 14.04 LTS (Trusty Tahr).
  9. #
  10. # Slighlty modified for MySQL 5.7 - Robert Brisita <[first-initial][last name] at gmail dot com>
  11.  
  12. set -o errexit # abort on nonzero exitstatus
  13. set -o nounset # abort on unbound variable
  14.  
  15. #{{{ Functions
  16.  
  17. usage() {
  18. cat << _EOF_
  19.  
  20. Usage: ${0} "ROOT PASSWORD"
  21.  
  22. with "ROOT PASSWORD" the desired password for the database root user.
  23.  
  24. Use quotes if your password contains spaces or other special characters.
  25. _EOF_
  26. }
  27.  
  28. # Predicate that returns exit status 0 if the database root password
  29. # is set, a nonzero exit status otherwise.
  30. is_mysql_root_password_set() {
  31. ! mysqladmin --user=root status > /dev/null 2>&1
  32. }
  33.  
  34. # Predicate that returns exit status 0 if the mysql(1) command is available,
  35. # nonzero exit status otherwise.
  36. is_mysql_command_available() {
  37. which mysql > /dev/null 2>&1
  38. }
  39.  
  40. #}}}
  41. #{{{ Command line parsing
  42.  
  43. if [ "$#" -ne "1" ]; then
  44. echo "Expected 1 argument, got $#" >&2
  45. usage
  46. exit 2
  47. fi
  48.  
  49. #}}}
  50. #{{{ Variables
  51. db_root_password="${1}"
  52. #}}}
  53.  
  54. # Script proper
  55.  
  56. if ! is_mysql_command_available; then
  57. echo "The MySQL/MariaDB client mysql(1) is not installed."
  58. exit 1
  59. fi
  60.  
  61. if is_mysql_root_password_set; then
  62. echo "Database root password already set"
  63. exit 0
  64. fi
  65.  
  66. mysql --user=root <<_EOF_
  67. UPDATE mysql.user SET authentication_string=PASSWORD('${db_root_password}') WHERE User='root';
  68. DELETE FROM mysql.user WHERE User='';
  69. DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
  70. DROP DATABASE IF EXISTS test;
  71. DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
  72. FLUSH PRIVILEGES;
  73. _EOF_
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement