-
Notifications
You must be signed in to change notification settings - Fork 2
/
mysql-create-db-and-user.sh
41 lines (31 loc) · 1.07 KB
/
mysql-create-db-and-user.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash
if [ -z $1 ]; then # no hay parametros en la llamada, los pedimos por pantalla:
read -r -e -p "Name of new DataBase: " dbname
read -r -e -p "Name of new DB User: " dbuser
read -s -p "New password for DB User $dbuser: " dbpass
echo "" # we need a new line after the password
read -s -p "Retype new password for user $dbuser: " dbpass2
echo "" # we need a new line after the password
if [ $dbpass != $dbpass2 ]; then
echo -e "\e[91mSorry, passwords do not match.\e[0m"
exit 1
fi
elif [ $# -ne 3 ]; then # hay parametros, pero no hay 3
echo "Usage: $0 dbname dbuser dbpass"
exit 65 #bad args
else
dbname=$1
dbuser=$2
dbpass=$3
fi
# Install MySQL/MariaDB if it is not installed yet:
if [ ! -x "$(command -v mysql)" ]; then
./mysql-install.sh
fi
Q1="CREATE DATABASE IF NOT EXISTS $dbname;"
Q2="GRANT ALL PRIVILEGES ON $dbname.* TO '$dbuser'@'%' IDENTIFIED BY '$dbpass';"
Q3="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}"
echo "Please, type MySQL's root password:"
mysql -uroot -p -e "$SQL"
echo "DB $dbname created with all privileges granted to DB user $dbuser"