Skip to content
This repository has been archived by the owner on Feb 22, 2022. It is now read-only.

[incubator/mysqlha] fix the creation of the replication user #6354

Merged
merged 1 commit into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion incubator/mysqlha/Chart.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
11 changes: 7 additions & 4 deletions incubator/mysqlha/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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;"
100 changes: 49 additions & 51 deletions incubator/mysqlha/templates/statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down