-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql.sh
161 lines (161 loc) · 4.01 KB
/
mysql.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/bash
#chkconfig: 35 80 15
#description: mysql daemon
#Source function library.
. /etc/rc.d/init.d/functions
my_port={{getv "port"}}
base_dir={{getv "basedir"}}
conf_file={{getv "conf_file"}}
my_user={{getv "my_user"}}
my_sock={{getv "my_sock"}}
create_user={{getv "create_user"}}
create_password={{getv "create_password"}}
start() {
status
REVAL=$?
if [ $REVAL == 1 ];then
nohup ${base_dir}/bin/mysqld_safe --defaults-file=${conf_file} >/dev/null 2>&1 &
sleep 3
while(true)
do
status
REVAL=$?
if [ $REVAL == 0 ];then
break
else
echo "wait mysql start"
sleep 3
fi
done
else
echo "mysql already running"
fi
}
stop() {
status
REVAL=$?
if [ $REVAL == 0 ];then
${base_dir}/bin/mysqladmin -u ${my_user} -S ${my_sock} shutdown
sleep 3
while(true)
do
status
REVAL=$?
if [ $REVAL == 1 ];then
break
else
echo "wait mysql stop"
sleep 3
fi
done
else
echo "mysql already stop"
fi
}
status() {
netstat -nltp | grep ${my_port}
REVAL=$?
if [ $REVAL == 0 ];then
echo "mysql is running"
return 0
else
echo "mysql is not running"
return 1
fi
}
init() {
status
REVAL=$?
if [ $REVAL == 1 ];then
if [ ! -d "${base_dir}/data" ]; then
if [ ! -d "${base_dir}/tmp" ]; then
mkdir ${base_dir}/tmp
cd ${base_dir}
echo "mysql begin to init"
${base_dir}/bin/mysqld --defaults-file=${conf_file} --initialize-insecure --user=mysql
sleep 3
while(true)
do
if [[ -d "${base_dir}/data" && -d "${base_dir}/tmp" ]]; then
chown -R mysql ${base_dir}/.
echo "mysql has been init"
break
else
echo "wait mysql init"
sleep 3
fi
done
else
echo "tmpdir already exists"
fi
else
echo "datadir already exists"
fi
else
echo "mysql already running"
fi
}
clean() {
status
REVAL=$?
if [ $REVAL == 1 ];then
if [ ! -d "${base_dir}/data" ]; then
echo "datadir is not exists"
else
if [ ! -d "${base_dir}/tmp" ]; then
echo "tmpdir is not exists"
else
if [ ! -d "/data/trash" ]; then
mkdir /data/trash
fi
date=`date +"%s"`
mv ${base_dir}/data ${base_dir}/data${date}
mv ${base_dir}/tmp ${base_dir}/tmp${date}
mv ${base_dir}/data${date} /data/trash/
mv ${base_dir}/tmp${date} /data/trash/
if [[ ! -d "${base_dir}/data" && ! -d "${base_dir}/tmp" ]]; then
echo "data&tmp has been moved to /data/trash , remember to clean the trash"
fi
fi
fi
else
echo "mysql is running , can't be cleaned"
fi
}
grant() {
mysql -u ${my_user} -S ${my_sock} -e "create user '${create_user}'@'%' identified by '${create_password}';grant all privileges on *.* to '${create_user}'@'%' identified by '${create_password}';flush privileges;select 'grant success';"
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
init)
init
;;
clean)
clean
;;
recovery)
stop
clean
init
start
;;
grant)
grant
;;
*)
echo $"Usage: $0 {start|stop|restart|status|init|clean|recovery|grant}"
RETVAL=2
esac
exit $RETVAL