To understand mariadb replication I created the next docker containers:
docker run -e TZ=America/Denver --name mariadb1 --network=camino_nw \
-e MARIADB_SERVER_ID=1 -e MARIADB_LOG_BIN=mysql-bin -e MARIADB_LOG_BASENAME=mariadb1 \
-e MARIADB_BINLOG_FORMAT=mixed -p 127.0.0.1:53301:3306 \
-v /home/jcz/Documents/dockerMariadbData1:/var/lib/mysql:z \
-e MARIADB_ROOT_PASSWORD=S3cretPw -d mariadb:latest
docker run -e TZ=America/Denver --name mariadb2 --network=camino_nw \
-e MARIADB_SERVER_ID=2 -e MARIADB_LOG_BIN=mysql-bin -e MARIADB_LOG_BASENAME=mariadb2 \
-e MARIADB_BINLOG_FORMAT=mixed -p 127.0.0.1:53302:3306 \
-v /home/jcz/Documents/dockerMariadbData2:/var/lib/mysql:z \
-e MARIADB_ROOT_PASSWORD=S3cretPw -d mariadb:latest
docker run -e TZ=America/Denver --name mariadb3 --network=camino_nw \
-e MARIADB_SERVER_ID=3 -e MARIADB_LOG_BIN=mysql-bin -e MARIADB_LOG_BASENAME=mariadb3 \
-e MARIADB_BINLOG_FORMAT=mixed -p 127.0.0.1:53303:3306 \
-v /home/jcz/Documents/dockerMariadbData3:/var/lib/mysql:z \
-e MARIADB_ROOT_PASSWORD=S3cretPw -d mariadb:latest
The master is mariadb1 and for this one I executed:
CREATE USER 'replication_user'@'%' IDENTIFIED BY 'S3cretPw';
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';
For each of the slaves mariadb2 and mariadb3 I run:
CHANGE MASTER TO MASTER_HOST='mariadb1', MASTER_USER='replication_user', MASTER_PASSWORD='S3cretPw', MASTER_PORT=3306, MASTER_CONNECT_RETRY=10;
CHANGE MASTER TO MASTER_USE_GTID = slave_pos;
I also started the slaves with START SLAVE;
After running the
SHOW SLAVE STATUS \G
I got the next error:
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MariaDB server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).
How can this happen if I used
MARIADB_SERVER_ID=1
MARIADB_SERVER_ID=2
MARIADB_SERVER_ID=3
for each of my containers?
I am trying to follow:
Why do you think that setting the
MARIADB_SERVER_ID
environment variable will set the mariadb server id? I cannot find that mentioned in either the mariadb documentation or in the documentation for the container image (or in the list of environment variables). The same is true for most of your otherMARIADB_*
variables as well.You can get things working if you:
That might look something like:
This will automatically create the replication user for you (because we set
MARIADB_REPLICATION_USER
andMARIADB_REPLICATION_PASSWORD
) and it will automatically configure the replicas to replicate frommariadb1
(because we also setMARIADB_MASTER_HOST
for the replicas).This is, incidentally, a fantastic opportunity for using
docker compose
, because it makes these longdocker run
command lines much easier to manage. I put all of the above into acompose.yaml
that looks like this:I have a
.env
file that looks like this:This allows me to simply
docker compose up
anddocker compose down
the whole environment. You can find the files referenced in this answer in this repository.