Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #! /bin/sh
  2.  
  3. set -o errexit # abort on nonzero exitstatus
  4. set -o nounset # abort on unbound variable
  5.  
  6. #{{{ Functions
  7.  
  8. usage() {
  9. cat << _EOF_
  10.  
  11. Usage: ${0} "ROOT PASSWORD"
  12.  
  13. with "ROOT PASSWORD" the desired password for the database root user.
  14.  
  15. Use quotes if your password contains spaces or other special characters.
  16. _EOF_
  17. }
  18.  
  19. # Predicate that returns exit status 0 if the database root password
  20. # is set, a nonzero exit status otherwise.
  21. is_mysql_root_password_set() {
  22. ! mysqladmin --user=root status > /dev/null 2>&1
  23. }
  24.  
  25. # Predicate that returns exit status 0 if the mysql(1) command is available,
  26. # nonzero exit status otherwise.
  27. is_mysql_command_available() {
  28. which mysql > /dev/null 2>&1
  29. }
  30.  
  31. #}}}
  32. #{{{ Command line parsing
  33.  
  34. if [ "$#" -ne "1" ]; then
  35. echo "Expected 1 argument, got $#" >&2
  36. usage
  37. exit 2
  38. fi
  39.  
  40. #}}}
  41. #{{{ Variables
  42. db_root_password="${1}"
  43. #}}}
  44.  
  45. # Script proper
  46.  
  47. if ! is_mysql_command_available; then
  48. echo "The MySQL/MariaDB client mysql(1) is not installed."
  49. exit 1
  50. fi
  51.  
  52. if is_mysql_root_password_set; then
  53. echo "Database root password already set"
  54. exit 0
  55. fi
  56.  
  57. mysql --user=root <<_EOF_
  58. UPDATE mysql.user SET Password=PASSWORD('${db_root_password}') WHERE User='root';
  59. DELETE FROM mysql.user WHERE User='';
  60. DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
  61. DROP DATABASE IF EXISTS test;
  62. DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
  63. FLUSH PRIVILEGES;
  64. _EOF_
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement