-
-
Save danixland/3257cee25470e20a455ccc69b3add23c to your computer and use it in GitHub Desktop.
Simple bash script to create mysql db, user with generated password
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/bash | |
PASS=$(pwgen -s 40 1) | |
if [ -r /root/.my.cnf ]; then | |
mysql -uroot <<MYSQL_SCRIPT | |
CREATE DATABASE $1; | |
CREATE USER '$1'@'localhost' IDENTIFIED BY '$PASS'; | |
GRANT ALL PRIVILEGES ON $1.* TO '$1'@'localhost'; | |
FLUSH PRIVILEGES; | |
MYSQL_SCRIPT | |
else | |
echo "Please insert the mysql root password:" | |
read mysqlrootpassword | |
mysql -uroot -p${mysqlrootpassword} <<MYSQL_SCRIPT | |
CREATE DATABASE $1; | |
CREATE USER '$1'@'localhost' IDENTIFIED BY '$PASS'; | |
GRANT ALL PRIVILEGES ON $1.* TO '$1'@'localhost'; | |
FLUSH PRIVILEGES; | |
MYSQL_SCRIPT | |
fi | |
echo "MySQL database and user created." | |
echo "Database: $1" | |
echo "Username: $1" | |
echo "Password: $PASS" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment