Trying MySQL Replication on Docker
→ 日本語版を読むUsing a Docker Ubuntu image, I will try setting up MySQL 8 replication.
First, prepare containers to host MySQL.
Since the MySQL master and slave communicate with each other for replication, run master and slave containers on a Docker Bridge network.
> docker network create mysql-replica
> docker network ls
> docker network inspect mysql-replica
> docker run --name mysql-master --network mysql-replica -it ubuntu /bin/bash
> docker run --name mysql-slave --network mysql-replica -it ubuntu /bin/bash
Install the packages needed to verify connectivity between master and slave. Also install vim for file editing.
# apt update -y
# apt install dnsutils -y
# apt install iputils-ping -y
# apt install vim -y
Confirm that name resolution works and that ping responds.
# nslookup mysql-master
# nslookup mysql-slave
# ping mysql-master
# ping mysql-slave
Install the MySQL server and start it with service ... start. Installing mysql-server also installs the mysql client.
# apt install mysql-server -y
# service mysql start
MySQL server preparation is now complete. Next, configure replication.
Perform the following configuration on both master and slave.
Master-side configuration
- Set server_id
- Set GTID and enforce_gtid_consistency (not required if not using GTID)
- Configure remote connection settings
- Create an account for slave connection
- Enable binary logging (not required as it is enabled by default)
Slave-side configuration
- Set server_id
- Set GTID and enforce_gtid_consistency (not required if not using GTID)
- Configure replication settings
- Start replication
Start with the master-side configuration.
Set server_id and GTID. Append the following to /etc/mysql/my.cnf.
# vi /etc/mysql/my.cnf
Make sure server_id does not overlap with the slave.
[mysqld]
server_id = 201
gtid_mode=ON
enforce_gtid_consistency
Restart to apply the configuration changes.
# service mysql restart
To allow remote connections, comment out the bound address.
In /etc/mysql/mysql.conf.d/mysqld.cnf, comment out the following line.
# bind-address = 127.0.0.1
The slave acts as the mysql client, and the master acts as the mysql server for MySQL replication.
That is, the slave accesses the master using a user account to retrieve the binlog.
Create the user account to be used by the slave.
The slave's hostname will be mysql-slave.mysql-replica.
# mysql
mysql> CREATE USE repl@'mysql-slave.mysql-replica' IDENTIFIED BY 'secret';
Since we want the slave to access using password authentication, set the authentication method to mysql_native_password.
ALTER USER repl@'mysql-slave.mysql-replica' IDENTIFIED WITH 'mysql_native_password' BY 'secret';
Master-side configuration is now complete.
Configure the slave side.
Set server_id and GTID. Append the following to /etc/mysql/my.cnf.
[mysqld]
server_id = 211
gtid_mode=ON
enforce_gtid_consistency
Restart to apply the configuration changes.
# service mysql restart
Configure replication and start it.
# mysql
mysql> change master to MASTER_HOST='mysql-master' , MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_HEARTBEAT_PERIOD=60;
mysql> start replica user='repl' password='secret';
The replication status can be checked with the following command. Actually verify that databases and tables created on the master side are also replicated on the slave side.
mysql> show replica status\G;
*************************** 1. row ***************************
Replica_IO_State: Waiting for source to send event
Source_Host: mysql-master
Source_User: repl
Source_Port: 3306
Connect_Retry: 60
Source_Log_File: binlog.000008
Read_Source_Log_Pos: 157
Relay_Log_File: 7db1994e1293-relay-bin.000002
Relay_Log_Pos: 367
Relay_Source_Log_File: binlog.000008
Replica_IO_Running: Yes
Replica_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Source_Log_Pos: 157
Relay_Log_Space: 584
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Source_SSL_Allowed: No
Source_SSL_CA_File:
Source_SSL_CA_Path:
Source_SSL_Cert:
Source_SSL_Cipher:
Source_SSL_Key:
Seconds_Behind_Source: 0
Source_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Source_Server_Id: 201
Source_UUID: 0700aae6-15ad-11ef-82bc-0242ac140002
Source_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Replica_SQL_Running_State: Replica has read all relay log; waiting for more updates
Source_Retry_Count: 86400
Source_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Source_SSL_Crl:
Source_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Source_TLS_Version:
Source_public_key_path:
Get_Source_public_key: 0
Network_Namespace:
1 row in set (0.01 sec)
That's it.