Created
August 16, 2011 14:45
-
-
Save alexrinass/1149249 to your computer and use it in GitHub Desktop.
Create new MySQL user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
NEWUSER="$1" | |
PASSWORD="$2" | |
if [ -z "$NEWUSER" ]; then | |
echo "Usage: addmysqluser USERNAME [PASSWORD]" | |
exit 0 | |
fi | |
if [ -z "$PASSWORD" ]; then | |
PASSWORD=$(pwgen --secure) | |
fi | |
RESULT=$(echo "SELECT user FROM \`user\`;" |mysql5 mysql |grep $NEWUSER) | |
if [ $? == 0 ]; then | |
echo "The mysql user '$NEWUSER' already exists." | |
exit 1 | |
fi | |
mysql5 -e "GRANT ALL ON \`%_$NEWUSER\`.* TO '$NEWUSER'@'localhost' IDENTIFIED BY '$PASSWORD'" | |
#if [ $? > 0 ]; then | |
# echo "Could not create new MySQL user." | |
# exit 1 | |
#fi | |
echo "Added user '$NEWUSER' with password '$PASSWORD', granting all privileges for database '%_$NEWUSER'." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment