11#! /usr/bin/env bash
2- env | sort | grep " POD\|HOST\|NAME"
2+ env | sort | grep " POD\|HOST\|NAME\|SSL"
3+
34args=$@
45NAMESPACE=" $POD_NAMESPACE "
56USER=" $MYSQL_ROOT_USERNAME "
@@ -60,6 +61,19 @@ function wait_for_mysqld_running() {
6061}
6162
6263joining_for_first_time=1
64+
65+ function alter_user(){
66+ local mysql=" $mysql_header --host=$localhost "
67+ local ssl_require=" "
68+ local user=" $1 "
69+ if [[ " ${REQUIRE_SSL:- } " == " TRUE" ]]; then
70+ ssl_require=" REQUIRE SSL"
71+ else
72+ ssl_require=" REQUIRE NONE"
73+ fi
74+ retry 120 ${mysql} -N -e " SET SQL_LOG_BIN=0;ALTER USER '$user '@'%' $ssl_require ;"
75+ }
76+
6377function create_replication_user() {
6478 # https://mariadb.com/kb/en/setting-up-replication/
6579 log " INFO" " Checking whether replication user exist or not......"
@@ -77,6 +91,7 @@ function create_replication_user() {
7791 else
7892 log " INFO" " Replication user exists. Skipping creating new one......."
7993 fi
94+ alter_user " repl"
8095}
8196
8297function create_maxscale_user() {
@@ -101,9 +116,10 @@ function create_maxscale_user() {
101116 else
102117 log " INFO" " Maxscale user exists. Skipping creating new one......."
103118 fi
119+ alter_user " maxscale"
104120}
105121
106- //TODO:
122+ # //TODO:
107123# function create_maxscale_confsync_user() {
108124# log "INFO" "Checking whether maxscale user exist or not......"
109125# local mysql="$mysql_header --host=$localhost"
@@ -121,6 +137,7 @@ function create_maxscale_user() {
121137# fi
122138# }
123139
140+
124141function create_monitor_user() {
125142 log " INFO" " Checking whether monitor user exist or not......"
126143 local mysql=" $mysql_header --host=$localhost "
@@ -146,6 +163,7 @@ function create_monitor_user() {
146163 else
147164 log " INFO" " Monitor user exists. Skipping creating new one......."
148165 fi
166+ alter_user " monitor_user"
149167}
150168function bootstrap_cluster() {
151169 echo " this is master node"
@@ -154,13 +172,20 @@ function bootstrap_cluster() {
154172}
155173
156174function join_to_master_by_current_pos() {
157- # member try to join into the existing group as old instance
175+ # member try to join into the existing group as fresh install, datadir is clean and no backup is restored
158176 log " INFO" " The replica, ${report_host} is joining to master node ${master} ..."
159177 local mysql=" $mysql_header --host=$localhost "
160178 log " INFO" " Joining to master with gtid current_pos.."
161179 retry 20 ${mysql} -N -e " STOP SLAVE;"
162180 retry 20 ${mysql} -N -e " RESET SLAVE ALL;"
163- retry 20 ${mysql} -N -e " CHANGE MASTER TO MASTER_HOST='$master ',MASTER_USER='repl',MASTER_PASSWORD='$MYSQL_ROOT_PASSWORD ',MASTER_USE_GTID = current_pos;"
181+ local ssl_options=" "
182+ if [[ " ${REQUIRE_SSL:- } " == " TRUE" ]]; then
183+ ssl_options=" , MASTER_SSL=1, MASTER_SSL_CA='/etc/mysql/certs/server/ca.crt'"
184+ log " INFO" " Configuring replication with TLS enabled"
185+ else
186+ log " INFO" " Configuring replication without TLS"
187+ fi
188+ retry 20 ${mysql} -N -e " CHANGE MASTER TO MASTER_HOST='$master ', MASTER_USER='repl', MASTER_PASSWORD='$MYSQL_ROOT_PASSWORD ' $ssl_options , MASTER_USE_GTID=current_pos;"
164189 retry 20 ${mysql} -N -e " START SLAVE;"
165190 joining_for_first_time=0
166191 echo " end join to master node by gtid current_pos"
@@ -176,13 +201,18 @@ function join_to_master_by_slave_pos() {
176201 if [ $joining_for_first_time -eq 1 ]; then
177202 retry 20 ${mysql} -N -e " SET GLOBAL gtid_slave_pos = '$gtid ';"
178203 fi
179- retry 20 ${mysql} -N -e " CHANGE MASTER TO MASTER_HOST='$master ',MASTER_USER='repl',MASTER_PASSWORD='$MYSQL_ROOT_PASSWORD ',MASTER_USE_GTID = slave_pos;"
204+ if [[ " ${REQUIRE_SSL:- } " == " TRUE" ]]; then
205+ ssl_options=" , MASTER_SSL=1, MASTER_SSL_CA='/etc/mysql/certs/server/ca.crt'"
206+ log " INFO" " Configuring replication with TLS enabled"
207+ else
208+ log " INFO" " Configuring replication without TLS"
209+ fi
210+ retry 20 ${mysql} -N -e " CHANGE MASTER TO MASTER_HOST='$master ', MASTER_USER='repl', MASTER_PASSWORD='$MYSQL_ROOT_PASSWORD ' $ssl_options , MASTER_USE_GTID=slave_pos;"
180211 retry 20 ${mysql} -N -e " START SLAVE;"
181212 joining_for_first_time=0
182213 echo " end join to master node by gtid slave_pos"
183214}
184215
185-
186216export pid
187217function start_mysqld_in_background() {
188218 log " INFO" " Starting MySQL server with docker-entrypoint.sh mysqld $args ..."
221251
222252start_mysqld_in_background
223253
224- export mysql_header=" mariadb -u ${USER} --port=3306"
254+ if [[ " ${REQUIRE_SSL:- } " == " TRUE" ]]; then
255+ export mysql_header=" mariadb -u ${USER} --port=3306 --ssl-ca=/etc/mysql/certs/server/ca.crt --ssl-cert=/etc/mysql/certs/server/tls.crt --ssl-key=/etc/mysql/certs/server/tls.key"
256+ else
257+ export mysql_header=" mariadb -u ${USER} --port=3306"
258+ fi
259+
225260export MYSQL_PWD=${PASSWORD}
226261
227262# wait for mysqld to be ready
@@ -288,16 +323,3 @@ while true; do
288323 wait $pid
289324done
290325
291-
292-
293-
294-
295-
296-
297-
298-
299-
300-
301-
302-
303-
0 commit comments