-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_percona
310 lines (275 loc) · 8.43 KB
/
install_percona
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/bin/bash
#########################################
# Global Variables
#########################################
declare START_TIMEOUT=120 # Timeout to wait for PS or PXC to start
declare SUDO=sudo # Used to check if sudo is installed or if we are running as root
declare PACKAGES_TO_INSTALL= # List of packages to install
declare VAR_MODE=ps # Which package the script will install
declare VAR_MODE_STRING="Percona Server" # Full string name of package the script will install
declare logfile="install.log"
#######################################
# Show script usage info.
#######################################
usage() {
cat <<EOF
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] [-m] [-i] [-b]
This tool is used to install Percona Server, Percona XtraDB Cluster and Percona XtraBackup.
Available options:
-h, --help Print this help and exit
-v, --verbose Print script debug info
-m [ps|pxc], --mode=[ps|pxc] Which database server the script will install. Default ps"
-i, --interactive Run the script interactively to prompt the user for configuration values (not implemented)
-b, --backup Install Percona XtraBackup package in addition to database server
EOF
exit
}
#######################################
# Accept and parse script's params.
#######################################
parse_params() {
local long_argument_list=(
"mode:"
"backup"
"help"
"verbose"
)
go_out="$(getopt \
--options=m:bvh \
--longoptions "$(printf "%s," "${long_argument_list[@]}")" \
--name="$(basename "$0")" -- "$@")"
eval set -- "$go_out"
if [[ $go_out == " --" ]];then
usage
exit 1
fi
while [[ $# -gt 0 ]];
do
arg="$1"
case "$arg" in
--) shift; break;;
-h | --help) usage ;;
-v | --verbose) set -x
shift
;;
-m | --mode)
VAR_MODE="$2"
if [[ ! $VAR_MODE =~ ^(ps|pxc)$ ]]; then
echo "[Error] Invalid --mode passed: '$VAR_MODE'"
echo "Please choose one of these modes: ps or pxc"
exit 1
fi
shift 2
;;
-b | --backup) install_backup=true
msg "${BLUE}Request: ${NOFORMAT} install Percona XtraBakcup"
shift
;;
*)
shift
;;
esac
done
return 0
}
check_command() {
command -v "${@}" 1>/dev/null
}
#######################################
# Runs command as root.
#######################################
run_root() {
sh='sh -c'
if [ "$(id -un)" != 'root' ]; then
if check_command sudo; then
sh='sudo -E sh -c'
elif check_command su; then
sh='su -c'
else
die "${RED}ERROR: ${NOFORMAT} root rights needed to run \"${*}\" command"
fi
fi
${sh} "${@}" &>>${logfile}
}
#######################################
# Defines colours for output messages.
#######################################
setup_colors() {
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m'
BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m'
else
NOFORMAT='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW=''
fi
}
#######################################
# Prints message to stderr with new line at the end.
#######################################
msg() {
echo >&2 -e "${1-}"
}
#######################################
# Prints message and exit with code.
# Arguments:
# message string;
# exit code.
# Outputs:
# writes message to stderr.
#######################################
die() {
local msg=${1}
local code=${2-1} # default exit status 1
msg "${msg}"
exit "${code}"
}
##########################################
# Install Percona Repository and define
# service name
##########################################
function install_repo_and_define_service()
{
msg "${ORANGE}Starting ${NOFORMAT} 'Percona Release' Repo configuraiton"
if check_command yum ; then
if [ ! -f /usr/bin/sudo ]; then
if [[ $(id -u) -eq 0 ]]; then
SUDO=""
else
yum install -y sudo
fi
fi
msg " Installing Repository"
run_root "yum install -y https://repo.percona.com/yum/percona-release-latest.noarch.rpm which"
# Disable mysql module
if check_command dnf
then
msg " Disabling mysql in 'dnf'"
run_root "dnf module disable -y mysql"
fi
else
if [ ! -f /usr/bin/sudo ]; then
if [[ $(id -u) -eq 0 ]];
then
SUDO=""
else
apt update
apt install -y sudo
fi
fi
run_root "export DEBIAN_FRONTEND=noninteractive"
run_root "apt update"
run_root "apt install -y gnupg2 lsb-release curl"
run_root "curl https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb -o percona-release_latest.$(lsb_release -sc)_all.deb"
run_root "dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb"
fi
msg "${GREEN}Success! ${NOFORMAT} 'Percona Release' Repo configured"
}
#########################################
# Install Percona XtraBackup
#########################################
install_pxb() {
msg "${ORANGE}Starting ${NOFORMAT} installation of Percona XtraBackup"
if check_command yum; then
if run_root "yum install -y percona-xtrabackup-80"; then
msg "${GREEN}Completed ${NOFORMAT} installation of Percona XtraBackup"
else
msg "${RED}FAILED ${NOFORMAT} instatlation of Percona XtraBackup, check ${logfile} for details"
fi
else
if run_root "apt-get install -y percona-xtrabackup-80"; then
msg "${GREEN}Completed ${NOFORMAT} installation of Percona XtraBackup"
else
msg "${RED}FAILED ${NOFORMAT} instatlation of Percona XtraBackup, check ${logfile} for details"
fi
fi
}
#########################################
# Install PS or PXC
#########################################
function install_packages()
{
if [[ $VAR_MODE =~ ^ps$ ]]; then
VAR_MODE_STRING="Percona Server"
run_root "percona-release setup -y ps80"
PACKAGES_TO_INSTALL="${PACKAGES_TO_INSTALL} percona-server-server"
elif [[ $VAR_MODE =~ ^pxc$ ]]; then
VAR_MODE_STRING="Percona XtraDB Cluster"
run_root "percona-release setup -y pxc80"
PACKAGES_TO_INSTALL="${PACKAGES_TO_INSTALL} percona-xtradb-cluster"
fi
msg "${ORANGE}Updating ${NOFORMAT} and installing software for ${VAR_MODE_STRING}"
if check_command yum ; then
run_root "yum install -y ${PACKAGES_TO_INSTALL}"
# Adjust running user to be mysql
printf "user\t = mysql\n" >> /etc/my.cnf
else
run_root "DEBIAN_FRONTEND=noninteractive apt-get install -y ${PACKAGES_TO_INSTALL}"
# Adjust running user to be mysql
printf "user\t = mysql\n" >> /etc/mysql/mysql.conf.d/mysqld.cnf
fi
msg "${GREEN}Completed ${NOFORMAT} installation"
}
#########################################
# Start server, validate it's running
#########################################
function start_server()
{
msg "${ORANGE}Starting ${NOFORMAT} ${VAR_MODE_STRING} services"
# Check mysqld has been initialized
[ "$(ls -1 /var/lib/mysql | wc -l)" = 0 ] && run_root "mysqld --initialize-insecure"
# Start Server
if check_command mysqld
then
if check_command systemctl
then
run_root "systemctl start mysql"
elif check_command service
then
run_root "service mysql start"
fi
# Check if mysqld is running
if ! mysqladmin -u root ping > /dev/null 2>&1;
then
run_root "mysqld &"
fi
for i in $(seq ${START_TIMEOUT});
do
if ! mysqladmin -u root ping > /dev/null 2>&1;
then
sleep 1;
else
break;
fi
done
if [[ ${i} -ne ${START_TIMEOUT} ]];
then
msg "${GREEN}Success!! ${NOFORMAT} ${VAR_MODE_STRING} Installed!"
msg ""
msg "To connect to your server, type:"
msg "mysql -u root"
msg ""
msg "Consider installing PMM to simplify monitoring and managing your database!"
msg "https://www.percona.com/software/pmm/quickstart"
mgs ""
exit 0
fi
fi
echo "${RED}FAILED: ${NOFORMAT}Error Installing ${VAR_MODE_STRING}"
exit 1
}
############################
# Start of Program
############################
main() {
setup_colors
#install_ps
install_repo_and_define_service
if [ ${install_backup} ] ; then
install_pxb
fi
install_packages
start_server
exit 0
}
parse_params "${@}"
main
die "Thank you for using Percona Software!" 0