-
Notifications
You must be signed in to change notification settings - Fork 3
/
database.sh
75 lines (65 loc) · 1.4 KB
/
database.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
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/sh
# Author: Michael Goff <Michael.Goff@Quantum.com>
# Licence: MIT
# Copyright (c) 2015, Quantum Corp.
usage()
{
cat <<EOF
Usage:
$0 -h
$0 <command>
Description:
Handle starting, configuring, and stopping the database for shippable
Commands:
start - start the mysql service and config with a test user with no password
stop - stop the mysql service
EOF
}
while getopts ":h" opt
do
case "${opt}" in
h ) usage; exit 0 ;;
\?) echo "unrecognized option: $OPTARG" 1>&2
exit 1
;;
esac
done
shift $((OPTIND-1))
if [ "$#" -ne 1 ]; then
echo "Invalid Params: Requires a command argument" 1>&2
usage
exit 1
fi
COMMAND=$1
shift
case "$COMMAND" in
start)
/usr/bin/mysqld_safe > /dev/null 2>&1 &
sleep 5
started=false
for try in {1..10}; do
if mysql -e "SELECT true" >/dev/null 2>&1; then
started=true
break
fi
echo "Waiting for mysql to start..."
sleep 5
done
if $started; then
echo "mysql started successfully"
else
echo "mysql failed to start!" 1>&2
exit 1
fi
mysql -u root -e "DROP USER 'test'@'localhost';" || true
mysql -u root -e "FLUSH PRIVILEGES; CREATE USER 'test'@'localhost'; GRANT ALL PRIVILEGES ON *.* TO 'test'@'localhost' WITH GRANT OPTION;"
;;
stop)
mysqladmin shutdown
;;
*)
echo "Unknown command: ${COMMAND}" 1>&2
usage
exit 1
;;
esac