Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- #
- # addmysqluser (and database)
- # (c) Thore Boedecker
- #
- # Created: 23/3/2010 by Thore Boedecker
- # Description: Adds a user to mysql and a database for him if wanted
- # Last Modified: 13/3/2010
- # Modify Reason: Addes some more options
- if [ `whoami` = "root" ]; then
- echo "Please enter root-password for mysql:"
- stty_orig=`stty -g`
- stty -echo
- read rootsql
- stty $stty_orig
- echo "Please enter a name for the new user:"
- read sqluser
- echo "Please specify a password for the new '$sqluser':"
- stty -echo
- read sqlpw
- stty $stty_orig
- echo "How do you want the user to be able to login to mysql?"
- echo "1 - from localhost"
- echo "2 - from internet and localhost"
- echo "Enter the number of your choice."
- read login
- if [ $login = "1" ]; then
- host="localhost"
- elif [ $login = "2" ]; then
- host="%"
- else echo "Unrecognized input, please re-run the script! (Nothing yet done)"
- exit 1
- fi
- echo "Do you want to add a database for the new user? <y/n>"
- read newdb
- if [ $ndb = "y" -o $ndb = "yes" -o $ndb = "Y" -o $ndb = "Yes" -o $ndb = "YES" ]; then
- echo "Please enter a name for the new database:"
- read db
- ndb="1"
- elif [ $ndb = "n" -o $ndb = "no" -o $ndb = "N" -o $ndb = "No" -o $ndb = "NO" ]; then
- echo "Skipping...."
- ndb="0"
- else
- echo "Unrecognized input, please re-run the script! (Nothing yet done)"
- exit 2
- fi
- echo "Check your input:"
- echo "---------------------------------------"
- echo "mysql-username: $sqluser"
- if [ $host = "localhost" ]; then
- echo "mysql-login: only from localhost"
- elif [ $host = "%" ]; then
- echo "mysql-login: from all (incl. internet)"
- else
- echo "Unknown login-host, it's recommended to re-run the script!"
- exit 3
- fi
- if [ $ndb = "1" ]; then
- echo "new database: $db"
- elif [ $ndb = "0" ]; then
- echo "no new databases will be created..."
- else
- echo "Unknown input for new database, please re-run the script! (Nothing yet done)"
- fi
- echo "These things will be created on your mysql-server."
- echo "Do want to execute the neccessary steps by now? <y/n>"
- read exe
- if [ $exe = "y" -o $exe = "yes" -o $exe = "Y" -o $exe = "Yes" -o $exe = "YES" ]; then
- echo "Executing mysql commands..."
- echo "#mysql --user=root --password=$rootsql --execute="CREATE USER '$sqluser'@'%' IDENTIFIED BY '$sqlpw'""
- echo "#mysql --user=root --password=$rootsql --execute="GRANT USAGE ON * . * TO '$sqluser'@'%'""
- echo "#mysql --user=root --password=$rootsql --execute="CREATE DATABASE $db""
- echo "#mysql --user=root --password=$rootsql --execute="GRANT ALL PRIVILEGES ON $db.* TO '$sqluser'@'%' WITH GRANT OPTION""
- echo "#mysql --user=root --password=$rootsql --execute="FLUSH PRIVILEGES""
- echo "All steps done!"
- elif [ $exe = "n" -o $exe = "no" -o $exe = "N" -o $exe = "No" -o $exe = "NO" ]; then
- echo "Aborting..."
- exit 4
- else echo "Unrecognized input, please re-run the script! (Nothing yet done)"
- exit 5
- fi
- echo "---------------------------------------"
- echo "Summary:"
- echo "---------------------------------------"
- if [ $host = "localhost" ]; then
- echo "mysql-host: localhost"
- elif [ $host = "%" ]; then
- IP=`LANG=en_US ifconfig | sed -n '/^eth0/{n;s/.*addr:\(.*\)\sBcast.*/\1/;p;q}' | sed 's/.$//'`
- echo "mysql-host: $IP"
- else
- echo "Unkown login-host, this should not happen!"
- fi
- mysqlport = $(cat `find /etc -name "my.cnf"` | grep "port" | grep "=" | awk '{print $3}' | tail -n 1)
- echo "mysq-port: $mysqlport"
- echo "mysql-username: $sqluser"
- echo "mysql-password for '$sqluser': $sqlpw"
- if [ $ndb = "y" -o $ndb = "yes" -o $ndb = "Y" -o $ndb = "Yes" ]; then
- echo "mysql-database: $db"
- fi
- echo "---------------------------------------"
- echo "---------------------------------------"
- echo "You maybe want to save these information!"
- else
- echo "Please run the script as root!"
- fi
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement