Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
FROM mariadb:10.3

COPY mariadb/init.sh /docker-entrypoint-initdb.d/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @db260179,

the mariadb image already runs the scripts inserted in /docker-entrypoint-initdb.d/ once, see: https://github.com/docker-library/mariadb/blob/44cfe68144976ce0ec135593ffe15c4bcca0dc6f/10.3/docker-entrypoint.sh#L80.

So you do not need to manually check in the entrypoint.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct, so no need for that change.
I've added a commit to allow the master to confirm to the slave its ready to start. Although you have a 30 second count down it isn't that reliable and relying on the depends_on is not a good method especially on docker swarm

COPY mariadb/init.sh /usr/local/bin/
COPY mariadb/entrypoint.sh /usr/local/bin/master-slave-entrypoint.sh

RUN mkdir -p /status

ENTRYPOINT ["master-slave-entrypoint.sh"]

CMD ["mysqld"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,4 @@ This project is maintained by using the [Semantic Versioning Specification (SemV

## Copyright and license

Copyright 2017 [Caffeina](http://caffeina.com) srl under the [MIT license](LICENSE.md).
Copyright 2017 [Caffeina](http://caffeina.com) srl under the [MIT license](LICENSE.md).
10 changes: 0 additions & 10 deletions docker-compose.dev.yml

This file was deleted.

9 changes: 8 additions & 1 deletion mariadb/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,11 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
fi
fi

docker-entrypoint.sh $@
# Check if init.sh has been added and add status folder
if [ ! -e "/docker-entrypoint-initdb.d/init.sh" ]; then
cp -f /usr/local/bin/init.sh /docker-entrypoint-initdb.d
chown mysql:mysql /status
chown mysql:mysql /docker-entrypoint-initdb.d/*
fi

docker-entrypoint.sh $@
28 changes: 22 additions & 6 deletions mariadb/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ if [ ! -z "$MYSQL_MASTER_HOST" ]; then
master_mysqldump+=( "$MYSQL_DATABASE" )
fi

until [ -e /status/.done ]; do
echo 'Waiting for master to finish tasks..'
sleep 5
done

for i in {30..0}; do
if echo 'SELECT 1' | "${master_mysql[@]}" &> /dev/null; then
echo 'MySQL master init process is complete...'
Expand All @@ -62,7 +67,7 @@ if [ ! -z "$MYSQL_MASTER_HOST" ]; then
sleep 1
done

if [ ! -z "$MYSQL_GRANT_SLAVE_USER" -a ! -z "$MYSQL_GRANT_SLAVE_PASSWORD" ]; then
if [ -z "$MYSQL_GRANT_SLAVE_USER" ] || [ -z "$MYSQL_GRANT_SLAVE_PASSWORD" ]; then
export MYSQL_GRANT_SLAVE_USER="$(pwgen -1 16)"
echo "GENERATED SLAVE USER: $MYSQL_GRANT_SLAVE_USER"

Expand All @@ -73,20 +78,29 @@ if [ ! -z "$MYSQL_MASTER_HOST" ]; then
"${master_mysql[@]}" <<-EOSQL
GRANT REPLICATION SLAVE ON *.* TO '$MYSQL_GRANT_SLAVE_USER'@'%' IDENTIFIED BY '$MYSQL_GRANT_SLAVE_PASSWORD';
FLUSH PRIVILEGES;
EOSQL
EOSQL

fi

# If the user sets these then just run the SQL grant for replicaton user
if [ ! -z "$MYSQL_GRANT_SLAVE_USER" ] || [ ! -z "$MYSQL_GRANT_SLAVE_PASSWORD" ]; then

"${master_mysql[@]}" <<-EOSQL
GRANT REPLICATION SLAVE ON *.* TO '$MYSQL_GRANT_SLAVE_USER'@'%' IDENTIFIED BY '$MYSQL_GRANT_SLAVE_PASSWORD';
FLUSH PRIVILEGES;
EOSQL
fi

"${master_mysql[@]}" <<-EOSQL > $MYSQL_STATUS 2>&1
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
EOSQL
EOSQL

"${master_mysqldump[@]}" > $MYSQL_EXPORT 2>&1

"${master_mysql[@]}" <<-EOSQL
UNLOCK TABLES;
EOSQL
EOSQL

## SLAVE

Expand Down Expand Up @@ -120,7 +134,7 @@ if [ ! -z "$MYSQL_MASTER_HOST" ]; then
MASTER_CONNECT_RETRY=$MYSQL_MASTER_CONNECT_RETRY;
START SLAVE;
SHOW SLAVE STATUS\G
EOSQL
EOSQL

if [ -e "$MYSQL_STATUS" ]; then
rm -f $MYSQL_STATUS
Expand Down Expand Up @@ -151,9 +165,11 @@ else

"${master_mysql[@]}" <<-EOSQL
SHOW MASTER STATUS\G
EOSQL
EOSQL

echo
echo 'MySQL master process done. Ready for replication.'
echo
echo 'Setting /status/.done for slave to start..'
touch /status/.done 2>&1
fi