Advertisement
Guest User

Untitled

a guest
May 19th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.82 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # addmysqluser (and database)
  4. # (c) Thore Boedecker
  5. #
  6. # Created:          23/3/2010 by Thore Boedecker
  7. # Description:      Adds a user to mysql and a database for him if wanted
  8. # Last Modified:    13/3/2010
  9. # Modify Reason:    Addes some more options
  10.  
  11. if [ `whoami` = "root" ]; then
  12.     echo "Please enter root-password for mysql:"
  13.     stty_orig=`stty -g`
  14.     stty -echo
  15.     read rootsql
  16.     stty $stty_orig
  17.     echo "Please enter a name for the new user:"
  18.     read sqluser
  19.     echo "Please specify a password for the new '$sqluser':"
  20.     stty -echo
  21.     read sqlpw
  22.     stty $stty_orig
  23.     echo "How do you want the user to be able to login to mysql?"
  24.     echo "1 - from localhost"
  25.     echo "2 - from internet and localhost"
  26.     echo "Enter the number of your choice."
  27.     read login
  28.     if [ $login = "1" ]; then
  29.         host="localhost"
  30.     elif [ $login = "2" ]; then
  31.         host="%"
  32.     else    echo "Unrecognized input, please re-run the script! (Nothing yet done)"
  33.             exit 1
  34.     fi
  35.     echo "Do you want to add a database for the new user? <y/n>"
  36.     read newdb
  37.     if [ $ndb = "y" -o $ndb = "yes" -o $ndb = "Y" -o $ndb = "Yes" -o $ndb = "YES" ]; then
  38.         echo "Please enter a name for the new database:"
  39.         read db
  40.         ndb="1"
  41.     elif [ $ndb = "n" -o $ndb = "no" -o $ndb = "N" -o $ndb = "No" -o $ndb = "NO" ]; then
  42.         echo "Skipping...."
  43.         ndb="0"
  44.     else
  45.         echo "Unrecognized input, please re-run the script! (Nothing yet done)"
  46.         exit 2
  47.     fi
  48.     echo "Check your input:"
  49.     echo "---------------------------------------"
  50.     echo "mysql-username: $sqluser"
  51.     if [ $host = "localhost" ]; then
  52.         echo "mysql-login: only from localhost"
  53.     elif [ $host = "%" ]; then
  54.         echo "mysql-login: from all (incl. internet)"
  55.     else
  56.         echo "Unknown login-host, it's recommended to re-run the script!"
  57.         exit 3
  58.     fi
  59.     if [ $ndb = "1" ]; then
  60.         echo "new database: $db"
  61.     elif [ $ndb = "0" ]; then
  62.         echo "no new databases will be created..."
  63.     else
  64.         echo "Unknown input for new database, please re-run the script! (Nothing yet done)"
  65.     fi
  66.     echo "These things will be created on your mysql-server."
  67.     echo "Do want to execute the neccessary steps by now? <y/n>"
  68.     read exe
  69.     if [ $exe = "y" -o $exe = "yes" -o $exe = "Y" -o $exe = "Yes" -o $exe = "YES" ]; then
  70.         echo "Executing mysql commands..."
  71.         echo "#mysql --user=root --password=$rootsql --execute="CREATE USER '$sqluser'@'%' IDENTIFIED BY '$sqlpw'""
  72.         echo "#mysql --user=root --password=$rootsql --execute="GRANT USAGE ON * . * TO '$sqluser'@'%'""
  73.         echo "#mysql --user=root --password=$rootsql --execute="CREATE DATABASE $db""
  74.         echo "#mysql --user=root --password=$rootsql --execute="GRANT ALL PRIVILEGES ON $db.* TO '$sqluser'@'%' WITH GRANT OPTION""
  75.         echo "#mysql --user=root --password=$rootsql --execute="FLUSH PRIVILEGES""
  76.         echo "All steps done!"
  77.     elif [ $exe = "n" -o $exe = "no" -o $exe = "N" -o $exe = "No" -o $exe = "NO" ]; then
  78.         echo "Aborting..."
  79.         exit 4
  80.     else    echo "Unrecognized input, please re-run the script! (Nothing yet done)"
  81.             exit 5
  82.     fi
  83.     echo "---------------------------------------"
  84.     echo "Summary:"
  85.     echo "---------------------------------------"
  86.     if [ $host = "localhost" ]; then
  87.         echo "mysql-host: localhost"
  88.     elif [ $host = "%" ]; then
  89.         IP=`LANG=en_US ifconfig  | sed -n '/^eth0/{n;s/.*addr:\(.*\)\sBcast.*/\1/;p;q}' | sed 's/.$//'`
  90.         echo "mysql-host: $IP"
  91.     else
  92.         echo "Unkown login-host, this should not happen!"
  93.     fi
  94.     mysqlport = $(cat `find /etc -name "my.cnf"` | grep "port" | grep "=" | awk '{print $3}' | tail -n 1)
  95.     echo "mysq-port: $mysqlport"
  96.     echo "mysql-username: $sqluser"
  97.     echo "mysql-password for '$sqluser': $sqlpw"
  98.     if [ $ndb = "y" -o $ndb = "yes" -o $ndb = "Y" -o $ndb = "Yes" ]; then
  99.         echo "mysql-database: $db"
  100.     fi
  101.     echo "---------------------------------------"
  102.     echo "---------------------------------------"
  103.     echo "You maybe want to save these information!"
  104. else
  105.     echo "Please run the script as root!"
  106. fi
  107. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement