forked from faizalrf/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployDb
239 lines (201 loc) · 6.14 KB
/
deployDb
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
Version=$1
PortNumber=$2
UserName=${USER}
SirtSize=$4
FileName=$(ls *${Version}*.tar.gz)
clear
if [[ ${UserName} == "root" ]]
then
echo "ROOT user not allowed for deployment, Aborting!!!"
echo
exit
fi
CreateServerCNF() {
DataDir=$1/data
RedoLogs=$1/redo_logs
LogDir=$1/logs
TmpDir=$1/tmp
ShirtSize=$2
mkdir -p ${DataDir} ${RedoLogs} ${LogDir} ${TmpDir}
if [ ! -d ${DataDir} ] || [ ! -d ${RedoLogs} ] || [ ! -d ${LogDir} ] || [ ! -d ${TmpDir} ]
then
echo "Failed to create folder(s) ${DataDir} ${RedoLogs} ${LogDir} ${TmpDir}"
echo "Aborting!!!"
echo
exit -1
fi
cat > ${ServerCNF} <<EOF
[mysqld]
datadir=${DataDir}
log_error=${LogDir}/mariadb.err
tmpdir=${TmpDir}
innodb_log_group_home_dir=${RedoLogs}
socket=${SocketPath}
innodb_buffer_pool_instances=4
innodb_log_files_in_group=4
innodb_flush_log_at_trx_commit=2
port=${PortNumber}
EOF
echo Shirt Size is ${ShirtSize}
if [[ ${ShirtSize} == "TINY" ]]
then
cat >> ${ServerCNF} <<EOF
#TINY Configuration
innodb_buffer_pool_size = 512M
innodb_log_file_size=128M
innodb_log_buffer_size=128M
max_allowed_packet=64M
innodb_io_capacity = 2000
table_open_cache = 2000
table_definition_cache = 2000
EOF
elif [[ ${ShirtSize} == "SMALL" ]]
then
cat >> ${ServerCNF} <<EOF
#SMALL Configuration
innodb_buffer_pool_size = 16G
innodb_log_file_size=750M
innodb_log_buffer_size=256M
max_allowed_packet=128M
nnodb_io_capacity = 2000
table_open_cache = 2000
table_definition_cache = 2000
EOF
elif [[ ${ShirtSize} == "MEDIUM" ]]
then
cat >> ${ServerCNF} <<EOF
#MEDIUM Configuration
innodb_buffer_pool_size = 64G
innodb_log_file_size=1G
innodb_log_buffer_size=1G
max_allowed_packet=256M
innodb_io_capacity = 2000
table_open_cache = 3000
table_definition_cache = 3000
EOF
elif [[ ${ShirtSize} == "LARGE" ]]
then
cat >> ${ServerCNF} <<EOF
#LARGE Configuration
innodb_buffer_pool_size = 128G
innodb_log_file_size=4G
innodb_log_buffer_size=2G
max_allowed_packet=512M
innodb_io_capacity = 2000
table_open_cache = 4000
table_definition_cache = 4000
EOF
else
echo "#DEFAULT Configuration selected" >> ${ServerCNF}
fi
}
if [[ $# -ne 4 ]]
then
echo "Missing arguments!"
echo "usage: deployDb <version> <port> <shirt size>"
echo
echo "*** Make sure that the tar.gz file is in the same location as this script ***"
echo "If deploying mariadb.10.3.8.tar.gz using 3306 port with SMALL configuration"
echo
echo "shell> ./deployDb 10.3.8 3306 /MariaDB SMALL"
echo Shirt Size can be: TINY, SMALL, MEDIUM, LARGE or DEFAULT
echo
echo Multiple versions or instances of MariaDB can be deployed on the same server using different ports
echo
echo For instance:
echo "shell> ./deployDb 10.3 3306 /MariaDB SMALL"
echo "* Make sure the folder specified above already exists with sufficient permission to write *"
echo
exit -1
fi
if [ ! -f ${FileName} ]
then
echo "No Such Version tarball found for ${Version}"
echo
exit -1
fi
export DirName=${FileName%.*.*}
export BasePath=$(pwd)
export AliasName=mariadb.${PortNumber}
export DestinationDir=${BasePath}/${AliasName}
export ServerCNF=${DestinationDir}/server.cnf
export SocketPath=/tmp/${AliasName}.sock
DestinationDataDir=$3/${AliasName}
ParentDestinationDir=$(dirname "${DestinationDataDir}")
if [ -f ${DestinationDataDir} ] || [ ! -d ${ParentDestinationDir} ]
then
echo "Invalid Destination Path ${ParentDestinationDir}"
echo
exit
fi
if [ -d ${DestinationDir} ] || [ -d ${DestinationDataDir} ]
then
echo "MariaDB folder(s) ${DestinationDir} or ${DestinationDataDir} already exist!"
echo
exit
fi
mkdir -p ${DestinationDir}
mkdir -p ${DestinationDataDir}
if [ ! -d ${DestinationDir} ] || [ ! -d ${DestinationDataDir} ]
then
echo "FAILED to create folder(s) ${DestinationDir} or ${DestinationDataDir}"
echo
exit
fi
if [ -d ${BasePath}/${DirName} ]
then
echo "${BasePath}/${DirName} already existsi, SKIP untar!"
echo
fi
echo "extracting ${FileName} into ${DestinationDir} folder"
tar -zxvf ${FileName} -C ${DestinationDir}
if [ ! -d ${DestinationDir}/${DirName} ]
then
echo "Unable to Extract the ${FileName}"
exit
fi
mv ${DestinationDir}/${DirName}/* ${DestinationDir}/
rm -rf ${DestinationDir}/${DirName}
cd ${DestinationDir}
# Create server.cnf
CreateServerCNF ${DestinationDataDir} ${SirtSize}
installDBScriptPath=$(find . -name mysql_install_db)
echo
echo "****************"
echo "${installDBScriptPath}/mysql_install_db --defaults-file=${ServerCNF}"
echo "****************"
echo
${installDBScriptPath} --defaults-file=${ServerCNF}
#Identify and kill if the process is already runninf on the port
ps -ef | grep mysqld | grep -i "\-\-port=${PortNumber}" | grep -v grep | awk '{print $2}' | xargs kill 2> /dev/null
echo "#Start" > ../${AliasName}.sh
echo "MySQLProcess=\$(ps -ef | grep mysqld | grep -i '\-\-port=${PortNumber}' | grep -v grep | awk '{print \$2}')" >> ../${AliasName}.sh
echo "if [ ! \${MySQLProcess} ]" >> ../${AliasName}.sh
echo "then" >> ../${AliasName}.sh
echo " ${DestinationDir}/bin/mysqld_safe --defaults-file=${ServerCNF} &" >> ../${AliasName}.sh
echo " sleep 5" >> ../${AliasName}.sh
echo "fi" >> ../${AliasName}.sh
echo "${DestinationDir}/bin/mysql -uroot -p -P${PortNumber} -S${SocketPath}" >> ../${AliasName}.sh
echo "${DestinationDir}/bin/mysqladmin -uroot -p -P${PortNumber} -S${SocketPath} shutdown" > ../${AliasName}.shutdown.sh
chmod 750 ../${AliasName}.sh
chmod 750 ../${AliasName}.shutdown.sh
echo "Starting up the instance!"
${DestinationDir}/bin/mysqld_safe --defaults-file=${ServerCNF} &
echo
echo "***********************************************"
echo "Securing Database running at Port ${PortNumber}"
echo "***********************************************"
echo
sleep 10
#Hardening
${DestinationDir}/bin/mysql -uroot -P${PortNumber} -S${SocketPath} << EOF
UPDATE mysql.user SET Password=PASSWORD('P@ssw0rd') WHERE User='root';
DELETE FROM mysql.user WHERE User='';
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
DROP DATABASE test;
DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%';
FLUSH PRIVILEGES;
EOF
echo "------------------------------------------------"
echo "Execute ${AliasName}.sh to login to ${AliasName}"
echo "------------------------------------------------"