forked from percona/proxysql-admin-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxysql-admin
executable file
·3716 lines (3309 loc) · 128 KB
/
proxysql-admin
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# This script will assist with configuring ProxySQL (currently only Percona XtraDB cluster in combination with ProxySQL is supported)
# Version 1.0
###############################################################################################
# This program is copyright 2016-2018 Percona LLC and/or its affiliates.
#
# THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, version 2 or later
#
# You should have received a copy of the GNU General Public License version 2
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
#-------------------------------------------------------------------------------
#
# Step 1 : Bash internal configuration
#
set -o nounset # no undefined variables
set -o pipefail # internal pipe failures cause an exit
#bash prompt internal configuration
declare BD=""
declare NBD=""
declare RED=""
declare NRED=""
# Test if stdout and stderr are open to a terminal
if [[ -t 1 ]]; then
BD=$(tput bold)
NBD=$(tput sgr0)
fi
if [[ -t 2 ]]; then
RED=$(tput setaf 1)
NRED=$(tput sgr0)
fi
#-------------------------------------------------------------------------------
#
# Step 2 : Global variables
#
#
# Script parameters/constants
#
readonly PROXYSQL_ADMIN_VERSION="2.0.7"
declare -i DEBUG=0
# default timeout is 10 seconds
declare -i TIMEOUT=10
#
# Global variables used by the script
#
declare CONFIG_FILE="/etc/proxysql-admin.cnf"
declare PROXYSQL_DATADIR=""
declare PROXYSQL_USERNAME=""
declare PROXYSQL_PASSWORD=""
declare PROXYSQL_PORT=""
declare PROXYSQL_HOSTNAME=""
declare CLUSTER_USERNAME=""
declare CLUSTER_PASSWORD=""
declare CLUSTER_HOSTNAME=""
declare CLUSTER_PORT=""
declare CLUSTER_APP_USERNAME=""
declare CLUSTER_APP_PASSWORD=""
declare MONITOR_USERNAME=""
declare MONITOR_PASSWORD=""
# The SAFE_XXX versions are the version that have
# been escaped. The single quotes will have been
# SQL-escaped, which means ' -> ''
# So these are the versions that should be used in any SQL query.
declare SAFE_MONITOR_USERNAME=""
declare SAFE_MONITOR_PASSWORD=""
declare WRITE_NODE=""
declare MODE="singlewrite"
declare -i NODE_CHECK_INTERVAL=-1
declare -i WRITER_HOSTGROUP_ID=-1
declare -i WRITER_HOSTGROUP_COMMAND_LINE=0
declare -i READER_HOSTGROUP_ID=-1
declare -i BACKUP_WRITER_HOSTGROUP_ID=-1
declare -i OFFLINE_HOSTGROUP_ID=-1
declare -i QUICK_DEMO=0
declare -i ENABLE=0
declare -i DISABLE=0
declare -i ADDUSER=0
declare -i SYNCUSERS=0
declare -i SYNCMULTICLUSTERUSERS=0
declare -i ADD_QUERY_RULE=0
declare -i UPDATE_CLUSTER=0
declare -i UPDATE_MYSQL_VERSION=0
declare -i CHECK_IF_ENABLED=0
declare -i FORCE=0
declare -i REPORT_STATUS=0
declare -i USE_EXISTING_MONITOR_PASSWORD=0
declare -i WITH_CLUSTER_APP_USER=1
declare -i DISABLE_UPDATES=0
# If we have to disable updates, store the original values
# so that we can restore them back to these values.
# These variables have the value of 'true' | 'false'
declare ADMIN_CHECKSUM_MYSQL_QUERY_RULES_ORIGINAL_VALUE=""
declare ADMIN_CHECKSUM_MYSQL_SERVERS_ORIGINAL_VALUE=""
declare ADMIN_CHECKSUM_MYSQL_USERS_ORIGINAL_VALUE=""
declare ADMIN_CLUSTER_USERNAME_ORIGINAL_VALUE=""
declare ADMIN_CLUSTER_CHECK_INTERVAL_ORIGINAL_VALUE=""
declare MYSQL_CLIENT_VERSION=""
declare USE_SSL="no"
declare -i USE_SSL_OPTION=0
#
# Default value for max_connections in mysql_servers
#
declare MAX_CONNECTIONS="1000"
#
# Default value for max-transactions-behind
#
declare -i MAX_TRANSACTIONS_BEHIND=100
declare WRITERS_ARE_READERS="backup"
declare -i WRITERS_ARE_READERS_OPTION=-1
declare -i REMOVE_ALL_SERVERS=0
#
# Set these values if a parameter is needed by the operation
# (For example, --disable only needs --writer-hg)
#
declare NEEDS_WRITER_HOSTGROUP=0
declare NEEDS_READER_HOSTGROUP=0
declare NEEDS_BACKUP_WRITER_HOSTGROUP=0
declare NEEDS_OFFLINE_HOSTGROUP=0
#-------------------------------------------------------------------------------
#
# Step 3 : Helper functions
#
function error() {
local lineno=$1
shift
if [[ -n "$lineno" ]]; then
printf "${BD}ERROR${NBD} (line:$lineno) : ${*//%/%%}\n" 1>&2
else
printf "${BD}ERROR${NBD} : ${*//%/%%}\n" 1>&2
fi
}
function warning() {
local lineno=$1
shift
if [[ -n "$lineno" ]]; then
printf "${BD}WARNING${NBD} (line:$lineno) : ${*//%/%%}\n" 1>&2
else
printf "${BD}WARNING${NBD}: ${*//%/%%}\n" 1>&2
fi
}
function debug() {
if [[ $DEBUG -eq 1 ]]; then
local lineno=$1
shift
if [[ -n "$lineno" ]]; then
printf "${RED}${BD}debug (line:$lineno) : ${*//%/%%}${NBD}${NRED}\n" 1>&2
else
printf "${RED}debug: ${*//%/%%}${NRED}\n" 1>&2
fi
fi
}
function dump_arguments() {
local arg_list=""
for arg do
arg_list+=" '$arg'"
done
echo $arg_list
}
#
# Dispay script usage details
#
function usage() {
local path=$0
cat << EOF
Usage: ${path##*/} [ options ]
Options:
--config-file=<config-file> Read login credentials from a configuration file
(command line options override any configuration file values)
--writer-hg=<number> The hostgroup that all traffic will be sent to
by default. Nodes that have 'read-only=0' in MySQL
will be assigned to this hostgroup.
--backup-writer-hg=<number> If the cluster has multiple nodes with 'read-only=0'
and max_writers set, then additional nodes (in excess
of max_writers), will be assigned to this hostgroup.
--reader-hg=<number> The hostgroup that read traffic should be sent to.
Nodes with 'read-only=0' in MySQL will be assigned
to this hostgroup.
--offline-hg=<number> Nodes that are determined to be OFFLINE will
assigned to this hostgroup.
--proxysql-datadir=<datadir> Specify the proxysql data directory location
--proxysql-username=<user_name> ProxySQL service username
--proxysql-password[=<password>] ProxySQL service password
--proxysql-port=<port_num> ProxySQL service port number
--proxysql-hostname=<host_name> ProxySQL service hostname
--cluster-username=<user_name> Percona XtraDB Cluster node username
--cluster-password[=<password>] Percona XtraDB Cluster node password
--cluster-port=<port_num> Percona XtraDB Cluster node port number
--cluster-hostname=<host_name> Percona XtraDB Cluster node hostname
--cluster-app-username=<user_name> Percona XtraDB Cluster node application username
--cluster-app-password[=<password>] Percona XtraDB Cluster node application passwrod
--without-cluster-app-user Configure Percona XtraDB Cluster without application user
--monitor-username=<user_name> Username for monitoring Percona XtraDB Cluster nodes through ProxySQL
--monitor-password[=<password>] Password for monitoring Percona XtraDB Cluster nodes through ProxySQL
--use-existing-monitor-password Do not prompt for a new monitor password if one is provided.
--node-check-interval=<NUMBER> The interval at which the proxy should connect
to the backend servers in order to monitor the
Galera staus of a node (in milliseconds).
(default: 5000)
--mode=[loadbal|singlewrite] ProxySQL read/write configuration mode
currently supporting: 'loadbal' and 'singlewrite'
(default: 'singlewrite')
--write-node=<IPADDRESS>:<PORT> Specifies the node that is to be used for
writes for singlewrite mode. If left unspecified,
the cluster node is then used as the write node.
This only applies when 'mode=singlewrite' is used.
--max-connections=<NUMBER> Value for max_connections in the mysql_servers table.
This is the maximum number of connections that
ProxySQL will open to the backend servers.
(default: 1000)
--max-transactions-behind=<NUMBER> Determines the maximum number of writesets a node
can have queued before the node is SHUNNED to avoid
stale reads.
(default: 100)
--use-ssl=[yes|no] If set to 'yes', then connections between ProxySQL
and the backend servers will use SSL.
(default: no)
--writers-are-readers=[yes|no|backup]
If set to 'yes', then all writers (backup-writers also)
are added to the reader hostgroup.
If set to 'no', then none of the writers (backup-writers also)
will be added to the reader hostgroup.
If set to 'backup', then only the backup-writers
will be added to the reader hostgroup.
(default: backup)
--remove-all-servers When used with --update-cluster, this will remove all
servers belonging to the current cluster before
updating the list.
--add-query-rule Create query rules for synced mysql user. This is applicable only
for singlewrite mode and works only with --syncusers
and --sync-multi-cluster-users options.
--force This option will skip existing configuration checks in mysql_servers,
mysql_users and mysql_galera_hostgroups tables. This option will only
work with 'proxysql-admin --enable'.
--disable-updates Disable admin updates for ProxySQL cluster for the
current operation. The default is to not change the
admin variable settings. If this option is specifed,
these options will be set to false.
(default: updates are not disabled)
--debug Enables additional debug logging.
--help Dispalys this help text.
These options are the possible operations for proxysql-admin.
One of the options below must be provided.
--adduser Adds the Percona XtraDB Cluster application user to the ProxySQL database
--disable, -d Remove any Percona XtraDB Cluster configurations from ProxySQL
--enable, -e Auto-configure Percona XtraDB Cluster nodes into ProxySQL
--update-cluster Updates the cluster membership, adds new cluster nodes
to the configuration.
--update-mysql-version Updates the mysql-server_version variable in ProxySQL with the version
from a node in the cluster.
--quick-demo Setup a quick demo with no authentication
--syncusers Sync user accounts currently configured in MySQL to ProxySQL
May be used with --enable.
(deletes ProxySQL users not in MySQL)
--sync-multi-cluster-users Sync user accounts currently configured in MySQL to ProxySQL
May be used with --enable.
(doesn't delete ProxySQL users not in MySQL)
--is-enabled Checks if the current configuration is enabled in ProxySQL.
--status Returns a status report on the current configuration.
If "--writer-hg=<NUM>" is specified, than the
data corresponding to the galera cluster with that
writer hostgroup is displayed. Otherwise, information
for all clusters will be displayed.
--version, -v Prints the version info
EOF
}
# Checks the return value of the most recent command
#
# Globals:
# None
#
# Arguments:
# 1: the error code of the most recent command
# 2: the lineno where the error occurred
# 3: the error message if the error code is non-zero
#
# Exits the script if the retcode is non-zero.
#
function check_cmd() {
local retcode=$1
local lineno=$2
shift 2
if [[ ${retcode} -ne 0 ]]; then
error "$lineno" $*
exit 1
fi
}
# Check the permissions for a file or directory
#
# Globals:
# None
#
# Arguments:
# 1: the bash test to be applied to the file
# 2: the lineno where this call is invoked (used for errors)
# 3: the path to the file
# 4: (optional) description of the path (mostly used for existence checks)
#
# Exits the script if the permissions test fails.
#
function check_permission() {
local permission=$1
local lineno=$2
local path_to_check=$3
local description=""
if [[ $# -gt 3 ]]; then
description="$4"
fi
if [ ! $permission "$path_to_check" ] ; then
if [[ $permission == "-r" ]]; then
error "$lineno" "You do not have READ permission for: $path_to_check"
elif [[ $permission == "-w" ]]; then
error "$lineno" "You do not have WRITE permission for: $path_to_check"
elif [[ $permission == "-x" ]]; then
error "$lineno" "You do not have EXECUTE permission for: $path_to_check"
elif [[ $permission == "-e" ]]; then
if [[ -n $description ]]; then
error "$lineno" "Could not find the $description: $path_to_check"
else
error "$lineno" "Could not find: $path_to_check"
fi
elif [[ $permission == "-d" ]]; then
if [[ -n $description ]]; then
error "$lineno" "Could not find the $description: $path_to_check"
else
error "$lineno" "Could not find the directory: $path_to_check"
fi
elif [[ $permission == "-f" ]]; then
if [[ -n $description ]]; then
error "$lineno" "Could not find the $description: $path_to_check"
else
error "$lineno" "Could not find the file: $path_to_check"
fi
else
error "$lineno" "You do not have the correct permissions for: $path_to_check"
fi
exit 1
fi
}
# Executes a SQL query with the (fully) specified server
#
# Globals:
# DEBUG
# TIMEOUT
#
# Arguments:
# 1: lineno
# 2: the name of the user
# 3: the user's password
# 4: the hostname of the server
# 5: the port used to connect to the server
# 6: the query to be run
# 7: (optional) arguments to the mysql client
# 8: (optional) additional options, space separated
# Available options:
# "hide_output"
# This will not show the output of the query when DEBUG is set.
# Used to stop the display of sensitve information (such as passwords)
# from being displayed when debugging.
#
function exec_sql() {
local lineno=$1
local user=$2
local password=$3
local hostname=$4
local port=$5
local query=$6
local args=""
local more_options=""
local retvalue
local retoutput
local default_auth=""
if [[ $# -ge 7 ]]; then
args=$7
fi
if [[ $# -ge 8 ]]; then
more_options=$8
fi
debug "$lineno" "exec_sql : $user@$hostname:$port ==> $query"
if [[ $MYSQL_CLIENT_VERSION == "8.0" ]]; then
default_auth="default-auth=mysql_native_password"
fi
retoutput=$(mysql --defaults-file=/dev/stdin --protocol=tcp \
--unbuffered --batch --silent ${args} -e "$query" <<EOF
[client]
user=${user//%/%%}
password="${password//%/%%}"
host=${hostname//%/%%}
port=${port//%/%%}
connect-timeout=${TIMEOUT}
${default_auth}
EOF
)
retvalue=$?
if [[ $DEBUG -eq 1 ]]; then
local number_of_newlines=0
local dbgoutput=$retoutput
if [[ " $more_options " =~ [[:space:]]hide_output[[:space:]] ]]; then
dbgoutput="**** data hidden ****"
fi
if [[ -n $dbgoutput ]]; then
number_of_newlines=$(printf "%s" "${dbgoutput}" | wc -l)
fi
if [[ $retvalue -ne 0 ]]; then
debug "" "--> query failed $retvalue"
elif [[ -z $dbgoutput ]]; then
debug "" "--> query returned $retvalue : <query returned no data>"
elif [[ ${number_of_newlines} -eq 0 ]]; then
debug "" "--> query returned $retvalue : ${dbgoutput}"
else
debug "" "--> query returned $retvalue : <data follows>"
printf "%s\n" "${dbgoutput}" | while IFS= read -r line; do
debug "" "----> $line"
done
fi
fi
printf "%s" "${retoutput}"
return $retvalue
}
# Executes a SQL query on proxysql
#
# Globals:
# PROXYSQL_USERNAME
# PROXYSQL_PASSWORD
# PROXYSQL_HOSTNAME
# PROXYSQL_PORT
#
# Arguments:
# 1: lineno
# 2: The SQL query
# 3: (optional) Additional arguments to the mysql client for the query
# 4: (optional) more options, see exec_sql
#
function proxysql_exec() {
local lineno=$1
local query=$2
local args=""
local more_options=""
if [[ $# -ge 3 ]]; then
args=$3
fi
if [[ -z $args ]]; then
args="--skip-column_names"
fi
if [[ $# -ge 4 ]]; then
more_options=$4
fi
exec_sql "$lineno" "$PROXYSQL_USERNAME" "$PROXYSQL_PASSWORD" \
"$PROXYSQL_HOSTNAME" "$PROXYSQL_PORT" \
"$query" "$args" "$more_options"
return $?
}
# Executes a SQL query on a specific node in the cluster
#
# Globals:
# CLUSTER_USERNAME
# CLUSTER_PASSWORD
#
# Arguments:
# 1: lineno
# 2: cluster host
# 3: cluster port
# 4: The SQL query
# 5: Additional arguments to the mysql client for the query
# 6: (optional) more options, see exec_sql
#
function cluster_exec() {
local lineno=$1
local cluster_host=$2
local cluster_port=$3
local query=$4
local args=""
local more_options=""
if [[ $# -ge 5 ]]; then
args=$5
fi
if [[ $# -ge 6 ]]; then
more_options=$6
fi
exec_sql "$lineno" "$CLUSTER_USERNAME" "$CLUSTER_PASSWORD" \
"$cluster_host" "$cluster_port" \
"$query" "$args" "$more_options"
return $?
}
# Executes a SQL query on the CLUSTER_HOSTNAME/CLUSTER_PORT
# specified in the config file.
#
# Globals:
# CLUSTER_USERNAME
# CLUSTER_PASSWORD
# CLUSTER_HOSTNAME
# CLUSTER_PORT
#
# Arguments:
# 1: lineno
# 2: The SQL query
# 3: Additional arguments to the mysql client for the query
# 4: (optional) more options, see exec_sql
#
function mysql_exec() {
local lineno=$1
local query=$2
local args=""
local more_options=""
if [[ $# -ge 3 ]]; then
args=$3
fi
if [[ $# -ge 4 ]]; then
more_options=$4
fi
cluster_exec "$lineno" "$CLUSTER_HOSTNAME" "$CLUSTER_PORT" \
"$query" "$args" "$more_options"
return $?
}
# Executes a SQL query on a cluster ndde with the monitor credentials
#
# Globals:
# CLUSTER_HOSTNAME
# CLUSTER_PORT
#
# Arguments:
# 1: lineno
# 2: The monitor username
# 3: The monitor password
# 4: Additional arguments to the mysql client for the query
# 5: The SQL query
# 6: (optional) more options, see exec_sql
#
function monitor_exec() {
local lineno=$1
local user=$2
local password=$3
local args=$4
local query=$5
local more_options=""
if [[ $# -ge 6 ]]; then
more_options=$7
fi
exec_sql "$lineno" "$user" "$password" \
"$CLUSTER_HOSTNAME" "$CLUSTER_PORT" \
"$query" "$args" "$more_options"
return $?
}
# Separates the IP address from the port in a network address
# Works for IPv4 and IPv6
#
# Globals:
# None
#
# Params:
# 1. The network address to be parsed
#
# Outputs:
# A string with a space separating the IP address from the port
#
function separate_ip_port_from_address()
{
#
# Break address string into host:port/path parts
#
local address=$1
# Has to have at least one ':' to separate the port from the ip address
if [[ $address =~ : ]]; then
ip_addr=${address%:*}
port=${address##*:}
else
ip_addr=$address
port=""
fi
# Remove any braces that surround the ip address portion
ip_addr=${ip_addr#\[}
ip_addr=${ip_addr%\]}
echo "${ip_addr} ${port}"
}
# Combines the IP address and port into a network address
# Works for IPv4 and IPv6
# (If the IP address is IPv6, the IP portion will have brackets)
#
# Globals:
# None
#
# Params:
# 1: The IP address portion
# 2: The port
#
# Outputs:
# A string containing the full network address
#
function combine_ip_port_into_address()
{
local ip_addr=$1
local port=$2
local addr
if [[ ! $ip_addr =~ \[.*\] && $ip_addr =~ .*:.* ]] ; then
# If there are no brackets and it does have a ':', then add the brackets
# because this is an unbracketed IPv6 address
addr="[${ip_addr}]:${port}"
else
addr="${ip_addr}:${port}"
fi
echo "$addr"
}
# Check proxysql running status
#
# Globals:
# None
#
# Arguments:
# None
#
# Exits if we could not connect to the proxysql instance
#
function proxysql_connection_check() {
proxysql_exec "$LINENO" "SELECT 1" >/dev/null
check_cmd $? "$LINENO" "ProxySQL connection check failed."\
"\n-- Could not connect to ProxySQL at $PROXYSQL_HOSTNAME:$PROXYSQL_PORT"\
"\n-- Please check the ProxySQL connection parameters and status."
debug "$LINENO" "ProxySQL connection check succeeded"
}
# Check the PXC cluster running status
# (well one node in the cluster anyway)
#
# Globals:
# None
#
# Arguments:
# None
#
# Exits if we could not connect to a cluster node
#
function cluster_connection_check() {
mysql_exec "$LINENO" "SELECT @@PORT" >/dev/null
check_cmd $? "$LINENO" "PXC connection check failed."\
"\n-- Could not connect to the PXC cluster at $CLUSTER_HOSTNAME:$CLUSTER_PORT"\
"\n-- Please check the PXC connection parameters and status."
debug "$LINENO" "cluster connection check succeeded"
}
# Check to see if the cluster is in mysql_galera_hostgroups
#
# Globals:
# None
#
# Arguments:
# Parameter 1 : the writer hostgroup id
#
function cluster_in_proxysql_check() {
local write_hg=$1
# Check to see if mysql_galera_hostgroups already has an
# entry for the worker hostgroup
local hostgroup_in_use
hostgroup_in_use=$(proxysql_exec "$LINENO" \
"SELECT count(*)
FROM mysql_galera_hostgroups
WHERE
writer_hostgroup = $write_hg")
check_cmd $? "$LINENO" "Galera hostgroup retrieval failed."\
"\n-- Please check the ProxySQL connection parameters and status."
# If there are no hostgroups with this writer hostgroup id
# then return failure
if [[ $hostgroup_in_use -eq 0 ]]; then
error "$LINENO" "The cluster (with writer hostgroup:$WRITER_HOSTGROUP_ID) has not been configured in ProxySQL"
exit 1
fi
}
# Queries the user for the proxysql connection parameters
#
# Globals:
# PROYXSQL_HOST
# PROXYSQL_PORT
# PROXYSQL_USERNAME
# PROXYSQL_PASSWORD
#
# Arguments:
# None
#
function quickdemo_get_proxysql_params() {
debug "$LINENO" "quickdemo_get_proxysql_params ()"
read -r -p "Do you want to use the default ProxySQL credentials (admin:admin:6032:127.0.0.1) [y/n] ? " check_param
case $check_param in
y|Y)
PROXYSQL_USERNAME="admin"
PROXYSQL_PASSWORD="admin"
PROXYSQL_PORT="6032"
PROXYSQL_HOSTNAME="127.0.0.1"
;;
n|N)
echo ""
echo -n "Enter the ProxySQL user name: "
read -r PROXYSQL_USERNAME
read -r -s -p "Enter the ProxySQL user password: " PROXYSQL_PASSWORD;echo ""
echo -n "Enter the ProxySQL port: "
read -r PROXYSQL_PORT
echo -n "Enter the ProxySQL hostname: "
read -r PROXYSQL_HOSTNAME
echo ""
;;
*)
error "" "Please type [y/n]!"
exit 1
;;
esac
}
# Queries the user for the PXC cluster connection parameters
#
# Globals:
# CLUSTER_HOSTNAME
# CLUSTER_PORT
# CLUSTER_USERNAME
# CLUSTER_PASSWORD
#
# Arguments:
# None
#
function quickdemo_get_cluster_params() {
debug "$LINENO" "quickdemo_get_cluster_params ()"
read -r -p "Do you want to use the default Percona XtraDB Cluster credentials (root::3306:127.0.0.1) [y/n] ? " check_param
case $check_param in
y|Y)
CLUSTER_USERNAME="root"
CLUSTER_PASSWORD=""
CLUSTER_PORT="3306"
CLUSTER_HOSTNAME="127.0.0.1"
;;
n|N)
echo ""
echo -n "Enter the Percona XtraDB Cluster username (super user): "
read -r CLUSTER_USERNAME
read -r -s -p "Enter the Percona XtraDB Cluster user password: " CLUSTER_PASSWORD; echo ""
echo -n "Enter the Percona XtraDB Cluster port: "
read -r CLUSTER_PORT
echo -n "Enter the Percona XtraDB Cluster hostname: "
read -r CLUSTER_HOSTNAME
echo ""
;;
*)
error "" "Please type [y/n]."
exit 1
;;
esac
}
# Checks for certain variables and prompts the user for
# the values if necessary.
#
# Globals:
# QUICK_DEMO
# MONITOR_USERNAME, MONITOR_PASSWORD
# CLUSTER_APP_USERNAME, CLUSTER_APP_PASSWORD
# CLUSTER_HOSTNAME, CLUSTER_USERNAME
# USER_HOST_RANGE
#
# Arguments:
# 1: the category for the variable, this may either be MONITOR or CLUSTER_APP
# 2: a description of the user
# 3: the hostgroup to associate the user with
# (only needed if user_category='CLISTER APP')
#
function user_input_check() {
debug "$LINENO" "user_input_check ( $(dump_arguments "$@") )"
local user_category=$1
local user_description=$2
local hostgroup_id
local username
local password
local safe_username
local safe_password
if [[ $user_category == "CLUSTER_APP" ]]; then
hostgroup_id=$3
fi
username=$(eval "echo \$${user_category}_USERNAME")
password=$(eval "echo \$${user_category}_PASSWORD")
if [[ -z $username ]]; then
read -r -p "Enter ${user_description}name : " ${user_category}_USERNAME
username=$(eval "echo \$${user_category}_USERNAME")
while [[ -z ${username} ]]
do
echo -n "No input entered, Enter ${user_description} name: "
read -r ${user_category}_USERNAME
username=$(eval "echo \$${user_category}_USERNAME")
done
else
if [[ $QUICK_DEMO -eq 0 ]]; then
echo -e "${user_description} name as per command line/config-file is ${BD}$(eval "echo \$${user_category}_USERNAME")${NBD}"
fi
fi
if [[ -z $password ]]; then
if [[ $QUICK_DEMO -eq 0 ]]; then
read -r -s -p "Enter ${user_description} password: " ${user_category}_PASSWORD
password=$(eval "echo \$${user_category}_PASSWORD")
while [[ -z $password ]]
do
read -r -s -p "No input entered, Enter ${user_description} password: " ${user_category}_PASSWORD
password=$(eval "echo \$${user_category}_PASSWORD")
done
fi
fi
username=$(eval "echo \$${user_category}_USERNAME")
password=$(eval "echo \$${user_category}_PASSWORD")
safe_username=${username//\'/\'\'}
safe_password=${password//\'/\'\'}
if [[ $user_category == "CLUSTER_APP" ]]; then
local check_user
check_user=$(mysql_exec "$LINENO" "SELECT user,host FROM mysql.user where user='$safe_username' and host='$USER_HOST_RANGE';")
check_cmd $? "$LINENO" "Failed to retrieve the user information from PXC."\
"\n-- Please check the PXC connection parameters and status."
if [[ -z "$check_user" ]]; then
if [[ $FORCE -eq 1 ]]; then
proxysql_exec "$LINENO" "DELETE FROM mysql_users where username='$safe_username';"
check_cmd $? "$LINENO" "Failed to delete the PXC application user: '$username' from the ProxySQL database."\
"\n-- Please check the ProxySQL connection parameters and status."
fi
local precheck_user
precheck_user=$(proxysql_exec "$LINENO" "SELECT username FROM mysql_users where username='$safe_username'")
check_cmd $? "$LINENO" "Failed to query ProxySQL for the user."\
"\n-- Please check the ProxySQL connection parameters and status."
if [[ -z "$precheck_user" ]]; then
mysql_exec "$LINENO" "CREATE USER '$safe_username'@'$USER_HOST_RANGE' IDENTIFIED WITH mysql_native_password BY '$safe_password';"
check_cmd $? "$LINENO" "Failed to add the PXC application user to PXC: $username" \
"\n-- Please check if '$CLUSTER_USERNAME'@'$CLUSTER_HOSTNAME' has the proper permissions to create the montioring user"
if [[ $QUICK_DEMO -eq 1 ]]; then
mysql_exec "$LINENO" "GRANT ALL ON *.* to '$safe_username'@'$USER_HOST_RANGE'"
check_cmd $? "$LINENO" "Failed to grant permissions to '$username'@'$USER_HOST_RANGE'"\
"\n-- Please check if $CLUSTER_USERNAME@'$CLUSTER_HOSTNAME' has the GRANT privilege"\
"\n-- required to assign the requested permissions"
fi
proxysql_exec "$LINENO" \
"INSERT INTO mysql_users
(username,password,active,default_hostgroup)
VALUES
('$safe_username','$safe_password',1,$hostgroup_id);"
check_cmd $? "$LINENO" "Failed to add the PXC application user: '$username' to the ProxySQL database."\
"\n-- Please check the ProxySQL connection parameters and status."
proxysql_load_to_runtime_save_to_disk "MYSQL USERS" "$LINENO" 1
if [[ $QUICK_DEMO -eq 0 ]]; then
echo -e "\nPercona XtraDB Cluster application user '${BD}$username'@'$USER_HOST_RANGE${NBD}' has been added with ${BD}ALL${NBD} privileges, ${BD}this user is created for testing purposes${NBD}"
else
echo -e "\nPercona XtraDB Cluster application user '${BD}$username'@'$USER_HOST_RANGE${NBD}' has been added with the USAGE privilege, please make sure to the grant appropriate privileges"
fi
else
error "$LINENO" "The application user ${BD}$username${NBD} is already present in the ProxySQL database."
echo -e "-- Note: ProxySQL does not allow duplicate usernames."
exit 1
fi
else
if [[ $FORCE -eq 1 ]]; then
proxysql_exec "$LINENO" "DELETE FROM mysql_users where username='$safe_username';"
check_cmd $? "$LINENO" "Failed to delete the PXC application user: '$safe_username' from the ProxySQL database."\
"\n-- Please check the ProxySQL connection parameters and status."
fi
local check_user
check_user=$(proxysql_exec "$LINENO" "SELECT username FROM mysql_users where username='$safe_username'")
check_cmd $? "$LINENO" "Could not retrieve the users from the ProxySQL database."\
"\n-- Please check the ProxySQL connection parameters and status."
if [[ -z "$check_user" ]]; then
echo -e "\nApplication user '${BD}${username}'@'$USER_HOST_RANGE${NBD}' already present in PXC.\n"
proxysql_exec "$LINENO" "INSERT INTO mysql_users (username,password,active,default_hostgroup) values ('$safe_username','$safe_password',1,$hostgroup_id);"
check_cmd $? "$LINENO" "Failed to add the PXC application user: '$username' to the ProxySQL database."\
"\n-- Please check the ProxySQL connection parameters and status."
proxysql_load_to_runtime_save_to_disk "MYSQL USERS" "$LINENO" 1
else
error "$LINENO" "The application user ${BD}$username${NBD} is already present in the ProxySQL database."
echo -e "-- Note: ProxySQL does not allow duplicate usernames."
exit 1
fi
fi
fi
}
# This will move the configuration from memory to the runtime (load)
# and from memory to disk (save)
#
# Globals:
# None
#
# Arguments:
# 1: the proxysql data that is being loaded and saved
# (for example "MYSQL USERS" or "MYSQL SERVERS").
# 2: the lineno where this function was invoked
#
# This function will exit the program if an error occurs while