-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.sh
62 lines (60 loc) · 2.53 KB
/
db.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash
# Bash script written by Saad Ismail - me@saadismail.net
# If /root/.my.cnf exists then it won't ask for root password
if [ -f /root/.my.cnf ]; then
echo "Please enter the NAME of the new MySQL database! (example: database1)"
read dbname
echo "Please enter the MySQL database CHARACTER SET! (example: latin1, utf8, ...)"
echo "Enter utf8 if you don't know what you are doing"
read charset
echo "Creating new MySQL database..."
mysql -e "CREATE DATABASE ${dbname} /*\!40100 DEFAULT CHARACTER SET ${charset} */;"
echo "Database successfully created!"
echo "Showing existing databases..."
mysql -e "show databases;"
echo ""
echo "Please enter the NAME of the new MySQL database user! (example: user1)"
read username
echo "Please enter the PASSWORD for the new MySQL database user!"
echo "Note: password will be hidden when typing"
read -s userpass
echo "Creating new user..."
mysql -e "CREATE USER ${username}@localhost IDENTIFIED BY '${userpass}';"
echo "User successfully created!"
echo ""
echo "Granting ALL privileges on ${dbname} to ${username}!"
mysql -e "GRANT ALL PRIVILEGES ON ${dbname}.* TO '${username}'@'localhost';"
mysql -e "FLUSH PRIVILEGES;"
echo "You're good now :)"
exit
# If /root/.my.cnf doesn't exist then it'll ask for root password
else
echo "Please enter root user MySQL password!"
echo "Note: password will be hidden when typing"
read -s rootpasswd
echo "Please enter the NAME of the new MySQL database! (example: database1)"
read dbname
echo "Please enter the MySQL database CHARACTER SET! (example: latin1, utf8, ...)"
echo "Enter utf8 if you don't know what you are doing"
read charset
echo "Creating new MySQL database..."
mysql -uroot -p${rootpasswd} -e "CREATE DATABASE ${dbname} /*\!40100 DEFAULT CHARACTER SET ${charset} */;"
echo "Database successfully created!"
echo "Showing existing databases..."
mysql -uroot -p${rootpasswd} -e "show databases;"
echo ""
echo "Please enter the NAME of the new MySQL database user! (example: user1)"
read username
echo "Please enter the PASSWORD for the new MySQL database user!"
echo "Note: password will be hidden when typing"
read -s userpass
echo "Creating new user..."
mysql -uroot -p${rootpasswd} -e "CREATE USER ${username}@localhost IDENTIFIED BY '${userpass}';"
echo "User successfully created!"
echo ""
echo "Granting ALL privileges on ${dbname} to ${username}!"
mysql -uroot -p${rootpasswd} -e "GRANT ALL PRIVILEGES ON ${dbname}.* TO '${username}'@'localhost';"
mysql -uroot -p${rootpasswd} -e "FLUSH PRIVILEGES;"
echo "You're good now :)"
exit
fi