Skip to content

Instantly share code, notes, and snippets.

@azeemhassni
Last active January 20, 2019 15:13
Show Gist options
  • Save azeemhassni/da44f4b026485e56ccaafe6624b2fe9d to your computer and use it in GitHub Desktop.
Save azeemhassni/da44f4b026485e56ccaafe6624b2fe9d to your computer and use it in GitHub Desktop.
Add user to mysql
FILENAME='./mysql_add_user.tmp.sql'
touch FILENAME;
echo "CREATE USER '$1'@'localhost' IDENTIFIED BY '$2';" >> FILENAME;
echo "GRANT ALL PRIVILEGES ON * . * TO '$1'@'localhost';" >> FILENAME;
echo "FLUSH PRIVILEGES;" >> FILENAME;
echo " 🔊 Adding $1 to mysql users";
sudo mysql -u root -p < FILENAME;
rm FILENAME;
echo " ✅ Added $1 to mysql users";
# For MySQL >= 8.0
FILENAME='$HOME/mysql_add_user.tmp.sql'
touch FILENAME;
echo "CREATE USER '$1'@'localhost' IDENTIFIED WITH mysql_native_password BY '$2';" >> FILENAME;
echo "GRANT ALL PRIVILEGES ON * . * TO '$1'@'localhost';" >> FILENAME;
echo "FLUSH PRIVILEGES;" >> FILENAME;
echo " 🔊 Adding $1 to mysql users";
sudo mysql -u root < FILENAME;
rm FILENAME;
echo " Added $1 to mysql users";
@azeemhassni
Copy link
Author

Usage

To use this gist follow the following steps.

  1. Download this gist somewhere on your computer
  2. Make it executable using chmod +x mysql_add_user.sh command
  3. Run it like ./mysql_add_user.sh username_to_add password_for_the_user

for example if john wants add him as mysql user, He'll execute

$  ./mysql_add_user.sh john secret

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment