Created
September 24, 2020 21:04
-
-
Save revenkroz/5da11d35d0b76be9633119e0d271bc0c to your computer and use it in GitHub Desktop.
Create USER and DB following format ${USER}_db
This file contains hidden or 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 | |
| if [ -z "$1" ] | |
| then | |
| echo "Database name is not specified." | |
| exit | |
| fi | |
| USER=$1 | |
| PASS=`pwgen -s 16 1` | |
| DB_NAME=${USER}_db | |
| # if mysql >= 8.0 | |
| COLLATION=utf8mb4_0900_ai_ci | |
| # else | |
| # COLLATION=utf8mb4_unicode_ci | |
| mysql -uroot <<MYSQL_SCRIPT | |
| CREATE DATABASE $DB_NAME DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE $COLLATION; | |
| CREATE USER '$USER'@'localhost' IDENTIFIED BY '$PASS'; | |
| GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$USER'@'localhost'; | |
| FLUSH PRIVILEGES; | |
| MYSQL_SCRIPT | |
| echo "Done." | |
| echo "Database: $DB_NAME" | |
| echo "Username: $USER" | |
| echo "Password: $PASS" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment