Setting Up Binary Log File Position Based Replication:
SOURCE SERVER IP:172.31.36.56
REPLICA SERVER IP:172.31.46.145
MYSQL VERSION :8.4
Installation document link:
https://www.linkedin.com/posts/naveen-gomangi-aa85bb184_steps-for-an-installation-of-
mysql-84-activity-7270113700815003649-
z0OG?utm_source=share&utm_medium=member_desktop
Step1) Install MySQL 8.4 On Source server:
wget https://dev.mysql.com/get/mysql84-community-release-el9-1.noarch.rpm
sudo yum localinstall mysql84-community-release-el9-1.noarch.rpm
yum repolist enabled | grep mysql.*-community
sudo yum install mysql-community-server
systemctl start mysqld
systemctl enable mysqld
systemctl status mysqld
sudo grep 'temporary password' /var/log/mysqld.log
mysql_secure_installation
Step2) Install MySQL 8.4 On Replica server:
wget https://dev.mysql.com/get/mysql84-community-release-el9-1.noarch.rpm
sudo yum localinstall mysql84-community-release-el9-1.noarch.rpm
yum repolist enabled | grep mysql.*-community
sudo yum install mysql-community-server
systemctl start mysqld
systemctl enable mysqld
systemctl status mysqld
sudo grep 'temporary password' /var/log/mysqld.log
mysql_secure_installation
Step3) Setting the Replication Source server Configuration:
Each server within a replication topology must be configured with a unique server ID, which you
can specify using the server_id system variable. This server ID is used to identify individual
servers within the replication topology, and must be a positive integer between 1 and (232)−1.
The default server_id value is 1
Binary logging is required on the source because the binary log is the basis for replicating
changes from the source to its replicas. Binary logging is enabled by default (the log_bin system
variable is set to ON). The --log-bin option tells the server what base name to use for binary log
files. It is recommended that you specify this option to give the binary log files a non-default
base name, so that if the host name changes, you can easily continue to use the same binary
log file names
For the greatest possible durability and consistency in a replication setup using InnoDB with
transactions, you should use innodb_flush_log_at_trx_commit=1 and sync_binlog=1 in the
source's my.cnf file.
#Edit mysql configuration file(/etc/my.cnf):
server-id=1
log-bin=/var/lib/mysql/mysql-bin.log
innodb_flush_log_at_trx_commit=1
sync_binlog=1
save&exit
Step4) Restart the MySQL service on the source server:
systemctl restart mysqld
systemctl status mysqld
Step5) Setting the Replica server Configuration:
Each replica must have a unique server ID, as specified by the server_id system variable. If you
are setting up multiple replicas, each one must have a unique server_id value that differs from
that of the source and from any of the other replicas. If the replica's server ID is not already set,
or the current value conflicts with the value that you have chosen for the source or another
replica, you must change it.
The relay log contains a record of events that affect the data or structure of a database.
#Edit mysql configuration file(/etc/my.cnf):
server-id=2
relay-log = /var/lib/mysql/mysql-relay-bin.log
log_bin = /var/lib/mysql/mysql-bin.log
save&exit
Step6) Restart the MySQL service on the replica server:
systemctl restart mysqld
systemctl status mysqld
Step7) Creating a User for Replication on the source server:
CREATE USER 'rep_user'@'172.31.46.145' IDENTIFIED BY 'Rep_user@2025';
GRANT REPLICATION SLAVE ON *.* TO 'rep_user'@'172.31.46.145';
Step8) Creating a admin User for Backup on the source server:
CREATE USER 'admin_user'@'172.31.46.145' IDENTIFIED BY 'Admin_user@2025';
GRANT ALL PRIVILEGES ON *.* TO 'admin_user'@'172.31.46.145';
Step9) Create sample database on the source and load some data on the source server:
-- Create a database
CREATE DATABASE SampleDB;
-- Use the newly created database
USE SampleDB;
-- Create tables
CREATE TABLE Employees (EmployeeID INT AUTO_INCREMENT PRIMARY KEY,FirstName
VARCHAR(50),LastName VARCHAR(50),Department VARCHAR(50),Salary DECIMAL(10, 2));
CREATE TABLE Departments (DepartmentID INT AUTO_INCREMENT PRIMARY
KEY,DepartmentName VARCHAR(50),ManagerID INT);
-- Insert sample data into Employees table
INSERT INTO Employees (FirstName, LastName, Department, Salary) VALUES('John', 'Doe', 'HR',
60000.00),('Jane', 'Smith', 'IT', 75000.00),('Michael', 'Brown', 'Finance', 50000.00),('Emily', 'Davis',
'HR', 58000.00),('Robert', 'Johnson', 'IT', 80000.00);
-- Insert sample data into Departments table
INSERT INTO Departments (DepartmentName, ManagerID) VALUES('HR', 1),('IT', 2),('Finance', 3);
-- Query the tables to verify data
SELECT * FROM Employees;
SELECT * FROM Departments;
Step10) Obtaining the Replication Source Binary Log Coordinates on the source server:
Flush all tables and block write statements by executing the FLUSH TABLES WITH READ
LOCK statement:
FLUSH TABLES WITH READ LOCK;
Use the SHOW BINARY LOG STATUS statement to determine the current binary log file
name and position:
SHOW BINARY LOG STATUS\G;
The File column shows the name of the log file and the Position column shows the position
within the file. In this example, the binary log file is mysql-bin.000004 and the position is 2867.
Record these values. You need them later when you are setting up the replica. They represent
the replication coordinates at which the replica should begin processing new updates from the
source.
Step11) Creating a Source Server Data Snapshot Using mysqldump on the Replica Server:
mysqldump -u admin_user -p -h 172.31.36.56 --all-databases > source_data_snapshot.sql
Step12) Release the lock on source server:
UNLOCK TABLES;
Step13) Restoring the Source Server Data Snapshot Using mysql on the Replica Server:
mysql -u root -p < source_data_snapshot.sql
Step14) Setting the Source Configuration on the Replica:
Execute a CHANGE REPLICATION SOURCE TO statement on the replica to set the source
configuration.
CHANGE REPLICATION SOURCE TO
SOURCE_HOST='source_host_name',SOURCE_USER='replication_user_name',SOURCE_PASS
WORD='replication_password',SOURCE_LOG_FILE='recorded_log_file_name',SOURCE_LOG_P
OS=recorded_log_position,SOURCE_SSL = {0|1};
CHANGE REPLICATION SOURCE TO
SOURCE_HOST='172.31.36.56',SOURCE_USER='rep_user',SOURCE_PASSWORD='Rep_user@2
025',SOURCE_LOG_FILE='mysql-bin.000004', SOURCE_LOG_POS=2867,SOURCE_SSL=1;
View the replica status by issuing SHOW REPLICA STATUS statement:
SHOW REPLICA STATUS\G;
Start the replication threads by issuing a START REPLICA statement:
START REPLICA;
Test the Replication:
ON THE SOURCE SERVER:
USE SampleDB;
CREATE TABLE Persons (PersonID int,LastName varchar(255),FirstName varchar(255),Address
varchar(255),City varchar(255));
INSERT INTO Persons (PersonID, LastName, FirstName, Address, City) VALUES(1, 'Smith', 'John',
'123 Elm Street', 'New York'),(2, 'Johnson', 'Mary', '456 Oak Avenue', 'Los Angeles'),(3, 'Brown',
'James', '789 Pine Road', 'Chicago'),(4, 'Taylor', 'Patricia', '101 Maple Lane', 'Houston'),(5,
'Anderson', 'Robert', '202 Birch Blvd', 'Phoenix');
SELECT * FROM Persons;
ON THE REPLICATION SERVER:
USE SampleDB;
SHOW TABLES;
SELECT * FROM Persons;
Enable below parameters to prevent the write operations on the replica server:
If the read_only system variable is enabled, the server permits no client updates except from
users who have the CONNECTION_ADMIN privilege (or the deprecated SUPER privilege). This
variable is disabled by default.
If super_read_only is enabled, the server prohibits client updates, even from users who have
the CONNECTION_ADMIN or SUPER privilege.
SET @@GLOBAL.read_only = ON;
SET @@GLOBAL.super_read_only=ON;