Organizations rely on databases to store data and allow efficient retrieval of it. Robust replication helps organizations meet availability goals and ensure the data is where users need it when they need it. There are many database options, but this article focuses on MySQL. Specifically, it explains using Global Transaction Identifiers (GTIDs) in MySQL replication. It also provides instructions for configuring source and replica servers with GTID enabled.
What is replication?
Replication is a process where multiple databases maintain copies of the same data. When one database instance updates its data, it sends those changes, as quickly as possible, to all the other databases.
MySQL uses the source-replica replication architecture. One of the databases is chosen as a source (it provides the replication), and the others are replicas (read-only databases that receive a copy).
What is a Global Transaction Identifier?
GTIDs bring transaction-based replication to the MySQL databases. With GTID replication, every transaction can be identified and tracked as it is committed on the originating source server and applied by replicas. You don't have to refer to any log files when starting the replica servers. GTID replication uses auto-positioning replication, which reduces the work spent on configuration.
It is very easy to determine if the source and replica are consistent. If all transitions committed on the source are also committed on the replicas, consistency is guaranteed. GTIDs use unique identifiers, which can be viewed by examining the binary log and are used for debugging purposes. The figure below depicts the status of a source MySQL server with GTID enabled.
To read more about creating and managing GTIDs, visit the official MySQL documentation. And to learn about using MySQL in Red Hat Enterprise Linux (RHEL), consult RHEL's Using MySQL documentation.
Implement GTID replication on MySQL servers
The following steps cover the configuration and connection of replication between two MySQL servers. One is the designated source and the other is the replica.
[ Learn Linux basics in 30 days with Red Hat Enterprise Linux Technical Overview course. ]
Ensure both machines have the MySQL server and client installed, and then follow the steps below.
1. Configure a MySQL source server
First, configure the source server by editing the MySQL configuration file. Pay close attention to ensuring the server-id value is unique.
Include the following options in the /etc/my.cnf.d/mysql-server.cnf file under the [mysqld] section:
bind-address=<source_ip_address>
log_bin=<path_to_source_server_log>
For example, specify the log file is found along this path: log_bin=/var/log/mysql/mysql-bin.log.
server-id=<id>
The server-id must be unique; otherwise, replication won't work.
gtid_mode=ON
enforce-gtid-consistency=ON
Because you made changes to the configuration file, you must restart the mysqld service:
$ sudo systemctl restart mysqld.service
2. Configure a MySQL replica server
Now move to the replica server and edit its configuration file, again ensuring the server-id is unique.
Include the following options in the /etc/my.cnf.d/mysql-server.cnf file under the [mysqld] section (if you want to read more about the options, refer to the MySQL documentation).
log_bin=<path_to_source_server_log>
For example, the source server's log file might be here: log_bin=/var/log/mysql/mysql-bin.log.
relay-log=path_to_replica_server_log
The replication server's log might be along this path: relay-log=/var/log/mysql/mysql-relay-bin.log.
server-id=<id>
The server-id must be unique; otherwise, replication won't work.
gtid_mode=ON
enforce-gtid-consistency=ON
log-replica-updates=ON
skip-replica-start=ON
You changed the configuration file on the replica server, so don't forget to restart the mysqld service:
$ sudo systemctl restart mysqld.service
3. Create a replication user on the MySQL source server
Now that the replication configuration is in place on both servers, the next step is to configure the necessary user account on the source server. Run the following commands from the MySQL command prompt.
Create a replication user:
mysql> CREATE USER 'replication_user'@'replica_server_ip' IDENTIFIED WITH mysql_native_password BY 'password';
Grant the user replication permissions:
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'replica_server_ip';
Reload the grant tables in the MySQL database:
mysql> FLUSH PRIVILEGES;
Set the source server to the read_only state:
mysql> SET @@GLOBAL.read_only = ON;
4. Connect the replica server to the source server
The final piece is to connect the replica server with the source server. These steps are also conducted at the MySQL prompt on the replica server.
Set the replica server to the read_only state:
mysql> SET @@GLOBAL.read_only = ON;
Configure the replication source:
mysql> CHANGE REPLICATION SOURCE TO
-> SOURCE_HOST='source_ip_address',
-> SOURCE_USER='replication_user',
-> SOURCE_PASSWORD='password',
-> SOURCE_AUTO_POSITION=1;
Start the replica thread in the MySQL replica server:
mysql> START REPLICA;
Now unset the read_only state on both servers, and you can make changes to the source server. These changes are sent to the replica:
mysql> SET @@GLOBAL.read_only = OFF;
[ The OpenShift for Developers eBook will get you started with containers quickly. ]
Use GTID
There is no reason not to use the GTID configuration for MySQL server replication. It makes rollouts, debugging, and configuration much easier for administrators. There may be some minor limitations to GTID-based replication, but the pros outweigh the cons. It is even easier to configure than configuring replication without GTID, so go for it.
저자 소개
Lukáš Javorský is a software engineer at Red Hat. He's a member of a team maintaining MariaDB, MySQL, and archive products (such as tar, zlib, and libarchive) and works with sclorg in container images on Fedora/RHEL/CentOS. He is a volleyball enthusiast and enjoys communicating and connecting with other people.
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
가상화
온프레미스와 클라우드 환경에서 워크로드를 유연하게 운영하기 위한 엔터프라이즈 가상화의 미래