This repository has been archived by the owner on Apr 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql-checkuser
executable file
·105 lines (89 loc) · 2.51 KB
/
mysql-checkuser
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
NO_ARGS=0
USAGE="`basename $0` -u username [-p password] [-h hostname] [-n dbname] [-m root-pw]"
ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
COL_RED=$ESC_SEQ"31;01m"
COL_GREEN=$ESC_SEQ"32;01m"
COL_YELLOW=$ESC_SEQ"33;01m"
COL_BLUE=$ESC_SEQ"34;01m"
COL_MAGENTA=$ESC_SEQ"35;01m"
COL_CYAN=$ESC_SEQ"36;01m"
if [ $# -eq "$NO_ARGS" ] # Script invoked with no command-line args?
then
echo $USAGE
cat <<EOF
Connects to the mysql host specified as root and checks for the existance of the
named DB and user. The DB connection will be made as root using the environment
variable MYSQL_ROOT_PASSWORD. If the environment variable is not set it will
prompt for a password on the command line.
If the user or database exists it exists with a code of -1.
The username is mandatory and the password and dbname will default to the
username for this check if not specified. The hostname will default to local
host. If the user and database do not exist this script will create a database
and user matching the specified details and give that user full permissions
over the new database
EOF
exit 1
fi
while getopts "u:p:h:n:m:" options; do
case $options in
u) user="$OPTARG";;
p) pass="$OPTARG";;
h) host="$OPTARG";;
n) name="$OPTARG";;
m) MYSQL_ROOT_PASSWORD="$OPTARG";;
esac
done
if [ -z $user ]
then
echo $USAGE
exit 1
fi
if [ -z $pass ]
then
pass=$user
fi
if [ -z $host ]
then
host=localhost
fi
if [ -z $name ]
then
name=$user
fi
if [ -z $MYSQL_ROOT_PASSWORD ]
then
echo -e $COL_CYAN"Enter the root password for your MySQL instance:"$COL_RESET
stty -echo
read mysqlroot
stty echo
else
mysqlroot=$MYSQL_ROOT_PASSWORD
fi
# echo -e "\nuser=$user\npass=$pass\nhost=$host\nname=$name\nroot=$mysqlroot\n"
# check connectivity
dbcheck=`mysql -u root -p$mysqlroot -h $host -e "show databases;"`
if [ $? != "0" ]
then
echo -e $COL_RED"Cannot connect to database as root"$COL_RESET
exit 1
fi
# check database
dbcheck=`mysql -u root -p$mysqlroot -h $host -s -e "show databases;"|grep $name`
if [ -z $dbcheck ]
then
echo -e $COL_GREEN"Database $name does not exist"$COL_RESET
else
echo -e $COL_RED"Database $name exists"$COL_RESET
exit -1
fi
# check check user
dbcheck=`mysql -u root -p$mysqlroot -h $host -D mysql -s -e 'select User from user where User="$user";'|grep $user`
if [ -z $dbcheck ]
then
echo -e $COL_GREEN"User $user does not exist"$COL_RESET
else
echo -e $COL_RED"User $user exists"$COL_RESET
exit -1
fi