Skip to content

Instantly share code, notes, and snippets.

@alexrinass
Created August 16, 2011 14:45
Show Gist options
  • Save alexrinass/1149249 to your computer and use it in GitHub Desktop.
Save alexrinass/1149249 to your computer and use it in GitHub Desktop.
Create new MySQL user
#!/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