forked from percona/proxysql-admin-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxysql-admin
executable file
·2115 lines (1883 loc) · 74.2 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="1.4.14"
declare -i DEBUG=0
#
# 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 SLAVE_NODES=""
declare SLAVE_HOSTNAME=""
declare SLAVE_PORT=""
declare SLAVE_IS_WRITER=""
declare MODE="singlewrite"
declare -i NODE_CHECK_INTERVAL=3000
declare -i WRITE_HOSTGROUP_ID=10
declare -i READ_HOSTGROUP_ID=11
declare WRITE_NODE=""
declare WRITE_NODES=""
declare ENABLE_PRIORITY=""
declare P_HOST_PRIORITY=""
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 USE_EXISTING_MONITOR_PASSWORD=0
declare -i WITH_CLUSTER_APP_USER=1
declare WRITER_IS_READER="ondemand"
#
# Default value for max_connections in mysql_servers
#
declare MAX_CONNECTIONS="1000"
#
# This is the options string to be set in the proxysql_galera_checker
# command. This is only set if MAX_CONNECTIONS is set via the command-line
#
declare MAX_CONNECTIONS_ARGS=""
#-------------------------------------------------------------------------------
#
# 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() {
### Consider adding a select or prompt option for --write-node that will let you select from discovered nodes during setup ###
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 login credentials)
--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=3000 Interval for monitoring node checker script (in milliseconds)
(default: 3000)
--mode=[loadbal|singlewrite] ProxySQL read/write configuration mode
currently supporting: 'loadbal' and 'singlewrite'
(default: 'singlewrite')
--write-node=host_name:port Writer node to accept write statments.
This option is supported only when using --mode=singlewrite
Can accept comma delimited list with the first listed being
the highest priority.
--include-slaves=host_name:port Add specified slave node(s) to ProxySQL, these nodes will go
into the reader hostgroup and will only be put into
the writer hostgroup if all cluster nodes are down (this
depends on the value of --use-slave-as-writer).
Slaves must be read only. Can accept a comma delimited list.
If this is used make sure 'read_only=1' is in the slave's my.cnf
--use-slave-as-writer=<yes/no> If this value is 'yes', then a slave may be used as a writer
if the entire cluster is down. If 'no', then a slave
will not be used as a writer. This option is required
if '--include-slaves' is used.
--writer-is-reader=<value> Defines if the writer node also accepts writes.
Possible values are 'always', 'never', and 'ondemand'.
'ondemand' means that the writer node only accepts reads
if there are no other readers.
(default: 'ondemand')
--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)
--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
--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)
--version, -v Print 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
#
# 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
if [[ $# -ge 7 ]]; then
args=$7
fi
if [[ $# -ge 8 ]]; then
more_options=$8
fi
debug "$lineno" "exec_sql : $user@$hostname:$port ==> $query"
retoutput=$(printf "[client]\nuser=${user//%/%%}\npassword=\"${password//%/%%}\"\nhost=${hostname//%/%%}\nport=${port//%/%%}" \
| mysql --defaults-file=/dev/stdin --protocol=tcp \
--unbuffered --batch --silent ${args} -e "${query}")
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" "${dbgoutput}\n" | 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
# 2: (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 node in the cluster
#
# Globals:
# CLUSTER_USERNAME
# CLUSTER_PASSWORD
# CLUSTER_HOSTNAME
# CLUSTER_PORT
#
# Arguments:
# 1: The SQL query
# 2: Additional arguments to the mysql client for the query
# 3: (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
exec_sql "$lineno" "$CLUSTER_USERNAME" "$CLUSTER_PASSWORD" \
"$CLUSTER_HOSTNAME" "$CLUSTER_PORT" \
"$query" "$args" "$more_options"
return $?
}
# Executes a SQL query on a slave node
#
# Globals:
# CLUSTER_USERNAME
# CLUSTER_PASSWORD
# SLAVE_HOSTNAME
# SLAVE_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 slave_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
exec_sql "$lineno" "$CLUSTER_USERNAME" "$CLUSTER_PASSWORD" \
"$SLAVE_HOSTNAME" "$SLAVE_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" "show tables" >/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"
}
# 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 ( $(dump_arguments "$@") )"
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 ( $(dump_arguments "$@") )"
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
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
while [[ -z "${user_category}_USERNAME" ]]
do
echo -n "No input entered, Enter ${user_description} name: "
read -r ${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
while [[ -z "${user_category}_PASSWORD" ]]
do
read -r -s -p "No input entered, Enter ${user_description} password: " ${user_category}_PASSWORD
done
fi
fi
username=$(eval "echo \$${user_category}_USERNAME")
password=$(eval "echo \$${user_category}_PASSWORD")
if [[ $user_category == "CLUSTER_APP" ]]; then
local check_user
check_user=$(mysql_exec "$LINENO" "SELECT user,host FROM mysql.user where user='$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
local precheck_user
precheck_user=$(proxysql_exec "$LINENO" "SELECT username FROM mysql_users where username='$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 $username@'$USER_HOST_RANGE' IDENTIFIED BY '$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 $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 ('$username','$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
local check_user
check_user=$(proxysql_exec "$LINENO" "SELECT username FROM mysql_users where username='$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."
proxysql_exec "$LINENO" "INSERT INTO mysql_users (username,password,active,default_hostgroup) values ('$username','$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
# loaded to runtime or saving to disk.
#
function proxysql_load_to_runtime_save_to_disk() {
local data_type=$1
local lineno=$2
local reload_from_runtime=0
if [[ $# -ge 3 ]]; then
reload_from_runtime=$3
fi
proxysql_exec "$LINENO" "LOAD ${data_type} TO RUNTIME"
check_cmd $? $lineno "Failed to load the ${data_type} configuration to runtime."\
"\n-- Please check the ProxySQL configuration and status."
debug "$lineno" "Loaded ${data_type} to runtime"
if [[ $reload_from_runtime -eq 1 ]]; then
# This has a specific purpose for the MYSQL USERS
# This will cause the password field to be loaded with the encrypted version
# of the password field
proxysql_exec "$LINENO" "SAVE ${data_type} FROM RUNTIME"
check_cmd $? $lineno "Failed to safe the ${data_type} configuration from the runtime."\
"\n-- Please check the ProxySQL configuration and status."
debug "$lineno" "Saved ${data_type} from runtime"
fi
proxysql_exec "$LINENO" "SAVE ${data_type} TO DISK;"
check_cmd $? $lineno "Failed to save the ${data_type} configuration to disk."\
"\n-- Please check the ProxySQL configuration and status."
debug "$lineno" "Saved ${data_type} to disk"
}
# Auto configure Percona XtraDB Cluster nodes into ProxySQL
#
# Globals:
# READ_HOSTGROUP_ID, WRITE_HOSTGROUP_ID
# CLUSTER_NETWORK
# CLUSTER_PORT
# USER_HOST_RANGE
# MONITOR_USERNAME, MONITOR_PASSWORD
# CLUSTER_APP_USERNAME, CLUSTER_APP_PASSWORD
# CLUSTER_USERNAME, CLUSTER_HOSTNAME
# USE_EXISTING_MONITOR_PASSWORD
# WITH_CLUSTER_APP_USER
# QUICK_DEMO
# SLAVE_NODES
# MODE
# WRITE_NODE
# WRITE_NODES
# P_HOST_PRIORITY
#
# Arguments:
# None
#
function enable_proxysql() {
local host_priority=""
debug $LINENO "enable_proxysql ( $(dump_arguments "$@") )"
# Checking proxysql binary location
if [[ ! -e $(which proxysql 2> /dev/null) ]]; then
error $LINENO "The proxysql binary was not found."\
"\n-- Please install the ProxySQL package."
exit 1
fi
if [[ ! -e $(which proxysql_galera_checker 2> /dev/null) ]] ;then
error $LINENO "The proxysql_galera_checker binary was not found."\
"\n-- Please check the ProxySQL package installation."
exit 1
fi
if [[ ! -e $(which proxysql_node_monitor 2> /dev/null) ]]; then
error $LINENO "The proxysql_node_monitor binary was not found."\
"\n-- Please check the ProxySQL package installation."
exit 1
fi
local proxysql_galera_check
proxysql_galera_check=$(which proxysql_galera_checker)
debug $LINENO "Found the proxysql_galera_checker at $proxysql_galera_check"
# Check for existing proxysql process
proxysql_connection_check
#modifying proxysql-admin.cnf file with command line proxysql user credentials if you dont use --config-file option.
#if [ -z "${CONFIG_FILE}" ]; then
# sed -i "s|[ \t]*PROXYSQL_USERNAME[ \t]*=.*$| PROXYSQL_USERNAME=\"${PROXYSQL_USERNAME}\"|" /etc/proxysql-admin.cnf
# sed -i "s|[ \t]*PROXYSQL_PASSWORD[ \t]*=.*$| PROXYSQL_PASSWORD=\"${PROXYSQL_PASSWORD}\"|" /etc/proxysql-admin.cnf
# sed -i "s|[ \t]*PROXYSQL_HOSTNAME[ \t]*=.*$| PROXYSQL_HOSTNAME=\"${PROXYSQL_HOSTNAME}\"|" /etc/proxysql-admin.cnf
# sed -i "s|[ \t]*PROXYSQL_PORT[ \t]*=.*$| PROXYSQL_PORT=\"${PROXYSQL_PORT}\"|" /etc/proxysql-admin.cnf
#fi
cluster_connection_check
local cluster_name
cluster_name=$(mysql_exec "$LINENO" "select @@wsrep_cluster_name;")
check_cmd $? $LINENO "Could not retrieve the cluster name from the PXC cluster at $CLUSTER_HOSTNAME:$CLUSTER_PORT"\
"\n-- Please check the PXC connection parameters and status."
readonly cluster_name
debug $LINENO "PXC cluster name is : $cluster_name"
local check_hgs
check_hgs=$(proxysql_exec "$LINENO" "SELECT hostgroup_id FROM mysql_servers where hostgroup_id in ($READ_HOSTGROUP_ID,$WRITE_HOSTGROUP_ID)")
if [[ ! -z "$check_hgs" ]]; then
error $LINENO "READ/WRITE hostgroups($READ_HOSTGROUP_ID,$WRITE_HOSTGROUP_ID) are already being used by ProxySQL."\
"\n-- To avoid conflicts, please use different values for the hostgroups."
exit 1
fi
CLUSTER_NETWORK=$(mysql_exec "$LINENO" "show status like 'wsrep_incoming_addresses'" |
awk '{print $2}' |
cut -d'.' -f1)
check_cmd $? $LINENO "Could not retrieve the cluster addresses from PXC."\
"\n-- Please check the PXC connection parameters and status."
if [[ "$CLUSTER_NETWORK" =~ ^[0-9]+$ ]]; then
USER_HOST_RANGE="$CLUSTER_NETWORK.%"
else
USER_HOST_RANGE="%"
fi
echo -e "\nConfiguring the ProxySQL monitoring user."
user_input_check MONITOR "ProxySQL monitor user"
readonly MONITOR_USERNAME
local check_user
check_user=$(mysql_exec "$LINENO" "SELECT user,host FROM mysql.user where user='$MONITOR_USERNAME' and host='$USER_HOST_RANGE';")
check_cmd $? $LINENO "Could not retrieve the monitor user information from PXC."\
"\n-- Please check the PXC cluster connection parameters and status."
if [[ -z "$check_user" ]]; then
# No monitor user found in MySQL, create the monitor user
mysql_exec "$LINENO" "CREATE USER '$MONITOR_USERNAME'@'$USER_HOST_RANGE' IDENTIFIED BY '$MONITOR_PASSWORD';"
check_cmd $? $LINENO "Failed to create the ProxySQL monitoring user."\
"\n-- Please check that '$CLUSTER_USERNAME'@'$CLUSTER_HOSTNAME' has the proper permissions to create the montioring user"
proxysql_exec "$LINENO" "update global_variables set variable_value='$MONITOR_USERNAME' where variable_name='mysql-monitor_username'; update global_variables set variable_value='$MONITOR_PASSWORD' where variable_name='mysql-monitor_password'; "
check_cmd $? $LINENO "Failed to set the mysql-monitor variables in ProxySQL."\
"\n-- Please check the ProxySQL connection parameters and status."
proxysql_load_to_runtime_save_to_disk "MYSQL VARIABLES" $LINENO
echo -e "\nUser '${BD}$MONITOR_USERNAME'@'$USER_HOST_RANGE${NBD}' has been added with USAGE privileges"
else
# Monitor user found in MySQL, do we want to use this existing user?
local check_param
if [[ $USE_EXISTING_MONITOR_PASSWORD -eq 1 ]]; then
# We have a password, so no need to ask for a new password
check_param="n"
else
echo ""
echo -e "The monitoring user is already present in Percona XtraDB Cluster.\n"
read -p "Would you like to enter a new password [y/n] ? " check_param
fi
case $check_param in
y|Y)
if [[ $QUICK_DEMO -eq 0 ]]; then
read -r -s -p "Please enter the password you have assigned to the monitoring user '$MONITOR_USERNAME': " MONITOR_PASSWORD
echo ""
monitor_exec "$LINENO" "$MONITOR_USERNAME" "$MONITOR_PASSWORD" "-Bs" "SELECT 1" >/dev/null
check_cmd $? $LINENO "Failed to connect to Percona XtraDB Cluster using monitor credentials."\
"\n-- Please check MONITOR_USERNAME and MONITOR_PASSWORD"