fix the creation of the replication user (#6354)

This commit is contained in:
Carlos Tadeu Panato Junior
2018-09-07 16:02:41 -07:00
committed by k8s-ci-robot
parent a3697be896
commit a87de92336
3 changed files with 57 additions and 56 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
name: mysqlha
version: 0.3.0
version: 0.4.0
appVersion: 5.7.13
description: MySQL cluster with a single master and zero or more slave replicas
keywords:
+7 -4
View File
@@ -12,7 +12,10 @@ data:
server-id.cnf: |
[mysqld]
server-id=@@SERVER_ID@@
create-replication-user.sql: |
CREATE USER IF NOT EXISTS '@@REPLICATION_USER@@' IDENTIFIED BY '@@REPLICATION_PASSWORD@@';
GRANT PROCESS, RELOAD, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO '@@REPLICATION_USER@@';
FLUSH PRIVILEGES;
create-replication-user.sh: |-
#!/bin/bash
set -e
mysql -h 127.0.0.1 --verbose -e "CREATE USER IF NOT EXISTS '${MYSQL_REPLICATION_USER}' IDENTIFIED BY '${MYSQL_REPLICATION_PASSWORD}';"
mysql -h 127.0.0.1 --verbose -e "GRANT PROCESS, RELOAD, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO '${MYSQL_REPLICATION_USER}';"
mysql -h 127.0.0.1 --verbose -e "FLUSH PRIVILEGES;"
+49 -51
View File
@@ -51,8 +51,8 @@ spec:
mountPath: /etc/mysql/conf.d
- name: init-mysql
image: {{ .Values.mysqlImage }}
command:
- bash
command: ["/bin/bash"]
args:
- "-c"
- |
set -ex
@@ -63,13 +63,14 @@ spec:
cat /mnt/config-map/server-id.cnf | sed s/@@SERVER_ID@@/$((100 + $ordinal))/g > /mnt/conf.d/server-id.cnf
# Copy appropriate conf.d files from config-map to config mount.
if [[ $ordinal -eq 0 ]]; then
cp /mnt/config-map/master.cnf /mnt/conf.d/
cp -f /mnt/config-map/master.cnf /mnt/conf.d/
else
cp /mnt/config-map/slave.cnf /mnt/conf.d/
cp -f /mnt/config-map/slave.cnf /mnt/conf.d/
fi
# Copy replication user script
if [[ $ordinal -eq 0 ]]; then
cp /mnt/config-map/create-replication-user.sql /mnt/scripts/create-replication-user.sql
cp -f /mnt/config-map/create-replication-user.sh /mnt/scripts/create-replication-user.sh
chmod 700 /mnt/scripts/create-replication-user.sh
fi
volumeMounts:
- name: conf
@@ -154,58 +155,55 @@ spec:
ports:
- name: xtrabackup
containerPort: 3307
command:
- bash
- "-c"
- |
set -ex
command: ["/bin/bash"]
args:
- "-c"
- |
set -ex
echo "Waiting for mysqld to be ready (accepting connections)"
until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done
echo "Waiting for mysqld to be ready (accepting connections)"
until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 5; done
# Create replication user
cd /mnt/scripts
if [[ -f create-replication-user.sql ]]; then
cp create-replication-user.sql create-replication-user.orig.sql
cat create-replication-user.sql \
| sed s/@@REPLICATION_USER@@/"${MYSQL_REPLICATION_USER}"/g \
| sed s/@@REPLICATION_PASSWORD@@/"${MYSQL_REPLICATION_PASSWORD}"/g \
| tee create-replication-user.sql
mysql -h 127.0.0.1 --verbose < create-replication-user.sql
fi
# Create replication user
cd /mnt/scripts
# file exists and is not empty with -s
if [[ -s create-replication-user.sh ]]; then
ls -la
./create-replication-user.sh
fi
cd /var/lib/mysql
# Determine binlog position of cloned data, if any.
if [[ -f xtrabackup_slave_info ]]; then
# XtraBackup already generated a partial "CHANGE MASTER TO" query
# because we're cloning from an existing slave.
cp xtrabackup_slave_info change_master_to.sql.in
elif [[ -f xtrabackup_binlog_info ]]; then
# We're cloning directly from master. Parse binlog position.
[[ $(cat xtrabackup_binlog_info) =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
fi
cd /var/lib/mysql
# Determine binlog position of cloned data, if any.
if [[ -f xtrabackup_slave_info ]]; then
# XtraBackup already generated a partial "CHANGE MASTER TO" query
# because we're cloning from an existing slave.
cp xtrabackup_slave_info change_master_to.sql.in
elif [[ -f xtrabackup_binlog_info ]]; then
# We're cloning directly from master. Parse binlog position.
[[ $(cat xtrabackup_binlog_info) =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
fi
# Check if we need to complete a clone by starting replication.
if [[ -f change_master_to.sql.in ]]; then
# Check if we need to complete a clone by starting replication.
if [[ -f change_master_to.sql.in ]]; then
# In case of container restart, attempt this at-most-once.
cp change_master_to.sql.in change_master_to.sql.orig
mysql -h 127.0.0.1 --verbose<<EOF
STOP SLAVE IO_THREAD;
$(<change_master_to.sql.orig),
MASTER_HOST='{{ template "fullname" . }}-0.{{ template "fullname" . }}',
MASTER_USER='${MYSQL_REPLICATION_USER}',
MASTER_PASSWORD='${MYSQL_REPLICATION_PASSWORD}',
MASTER_CONNECT_RETRY=10;
START SLAVE;
EOF
fi
# In case of container restart, attempt this at-most-once.
cp change_master_to.sql.in change_master_to.sql.orig
mysql -h 127.0.0.1 --verbose<<EOF
STOP SLAVE IO_THREAD;
$(<change_master_to.sql.orig),
MASTER_HOST='{{ template "fullname" . }}-0.{{ template "fullname" . }}',
MASTER_USER='${MYSQL_REPLICATION_USER}',
MASTER_PASSWORD='${MYSQL_REPLICATION_PASSWORD}',
MASTER_CONNECT_RETRY=10;
START SLAVE;
EOF
fi
# Start a server to send backups when requested by peers.
exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
"xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=${MYSQL_REPLICATION_USER} --password=${MYSQL_REPLICATION_PASSWORD}"
# Start a server to send backups when requested by peers.
exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
"xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=${MYSQL_REPLICATION_USER} --password=${MYSQL_REPLICATION_PASSWORD}"
volumeMounts:
- name: data
mountPath: /var/lib/mysql