How to Deploy MySQL InnoDB Replica Set in Production?
- What is MySQL InnoDB Replica Set?
- What is prerequisite and limitation of using MySQL Replica Set?
- In what kind of scenarios MySQL Replica Set is not recommended.
- How to configure and deploy MySQL Replica Set- (step by step guide )
- How to use InnoDB Replica Set?
- What if Primary goes down? Does select query re-routed to another server?
- What if Secondary goes down while executing select queries?
Application------->Router------->List of MySQL Servers.
|
What is prerequisite and limitation of using MySQL Replica Set?
In what kind of scenarios MySQL Replica Set is Recommended ?
How to configure and deploy MySQL Replica Set
##In Machine 01
mysqlsh
shell.connect("root@10.0.10.33:3306");
Creating a session to 'root@10.0.10.33:3306'
Please provide the password for 'root@10.0.10.33:3306': ********
Save password for 'root@10.0.10.33:3306'? [Y]es/[N]o/Ne[v]er (default No): Y
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 13
Server version: 8.0.19-commercial MySQL Enterprise Server - Commercial
No default schema selected; type \use <schema> to set one.
<ClassicSession:root@10.0.10.33:3306>
MySQL 10.0.10.33:3306 ssl JS >
dba.configureReplicaSetInstance("root@10.0.10.33:3306",{clusterAdmin: "'rsadmin'@'10.0.10.33%'"});
dba.configureReplicaSetInstance("root@10.0.10.33:3306",{clusterAdmin: "'rsadmin'@'10.0.10.33%'"});
Configuring local MySQL instance listening at port 3306 for use in an InnoDB ReplicaSet...
This instance reports its own address as Workshop-33:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.
Password for new account: ********
Confirm password: ********
NOTE: Some configuration options need to be fixed:
+--------------------------+---------------+----------------+--------------------------------------------------+
| Variable | Current Value | Required Value | Note |
+--------------------------+---------------+----------------+--------------------------------------------------+
| enforce_gtid_consistency | OFF | ON | Update read-only variable and restart the server |
| gtid_mode | OFF | ON | Update read-only variable and restart the server |
| server_id | 1 | <unique ID> | Update read-only variable and restart the server |
+--------------------------+---------------+----------------+--------------------------------------------------+
Some variables need to be changed, but cannot be done dynamically on the server.
Do you want to perform the required configuration changes? [y/n]: y
Do you want to restart the instance after configuring it? [y/n]: y
Cluster admin user 'rsadmin'@'10.0.10.33%' created.
Configuring instance...
The instance 'Workshop-33:3306' was configured to be used in an InnoDB ReplicaSet.
Restarting MySQL...
NOTE: MySQL server at Workshop-33:3306 was restarted.
##In Machine 2
mysqlsh
shell.connect("root@10.0.10.38:3306");
Creating a session to 'root@10.0.10.38:3306'
Please provide the password for 'root@10.0.10.38:3306': ********
Save password for 'root@10.0.10.38:3306'? [Y]es/[N]o/Ne[v]er (default No): Y
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 10
Server version: 8.0.19-commercial MySQL Enterprise Server - Commercial
No default schema selected; type \use <schema> to set one.
<ClassicSession:root@10.0.10.38:3306>
dba.configureReplicaSetInstance("root@10.0.10.38:3306",{clusterAdmin: "'rsadmin'@'10.0.10.38%'"});
Configuring local MySQL instance listening at port 3306 for use in an InnoDB ReplicaSet...
This instance reports its own address as Workshop-38:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.
Password for new account: ********
Confirm password: ********
NOTE: Some configuration options need to be fixed:
+--------------------------+---------------+----------------+--------------------------------------------------+
| Variable | Current Value | Required Value | Note |
+--------------------------+---------------+----------------+--------------------------------------------------+
| enforce_gtid_consistency | OFF | ON | Update read-only variable and restart the server |
| gtid_mode | OFF | ON | Update read-only variable and restart the server |
| server_id | 1 | <unique ID> | Update read-only variable and restart the server |
+--------------------------+---------------+----------------+--------------------------------------------------+
Some variables need to be changed, but cannot be done dynamically on the server.
Do you want to perform the required configuration changes? [y/n]: y
Do you want to restart the instance after configuring it? [y/n]: y
Cluster admin user 'rsadmin'@'10.0.10.38%' created.
Configuring instance...
The instance 'Workshop-38:3306' was configured to be used in an InnoDB ReplicaSet.
Restarting MySQL...
NOTE: MySQL server at Workshop-38:3306 was restarted.
MySQL 10.0.10.38:3306 ssl JS >
|
##Connect to Machine 01 :-
mysqlsh
shell.connect("root@10.0.10.33:3306");
var rs = dba.createReplicaSet("MyReplicatSet")
A new replicaset with instance 'Workshop-33:3306' will be created.
* Checking MySQL instance at Workshop-33:3306
This instance reports its own address as Workshop-33:3306
Workshop-33:3306: Instance configuration is suitable.
* Updating metadata...
ReplicaSet object successfully created for Workshop-33:3306.
Use rs.addInstance() to add more asynchronously replicated instances to this replicaset and rs.status() to check its status.
MySQL 10.0.10.33:3306 ssl JS > rs.addInstance("10.0.10.38:3306");
Adding instance to the replicaset...
* Performing validation checks
This instance reports its own address as Workshop-38:3306
Workshop-38:3306: Instance configuration is suitable.
* Checking async replication topology...
* Checking transaction state of the instance...
The safest and most convenient way to provision a new instance is through automatic clone provisioning, which will completely overwrite the state of 'Workshop-38:3306' with a physical snapshot from an existing replicaset member. To use this method by default, set the 'recoveryMethod' option to 'clone'.
WARNING: It should be safe to rely on replication to incrementally recover the state of the new instance if you are sure all updates ever executed in the replicaset were done with GTIDs enabled, there are no purged transactions and the new instance contains the same GTID set as the replicaset or a subset of it. To use this method by default, set the 'recoveryMethod' option to 'incremental'.
Incremental state recovery was selected because it seems to be safely usable.
* Updating topology
** Configuring Workshop-38:3306 to replicate from Workshop-33:3306
** Waiting for new instance to synchronize with PRIMARY...
The instance 'Workshop-38:3306' was added to the replicaset and is replicating from Workshop-33:3306.
MySQL 10.0.10.33:3306 ssl JS >
rs.status();
{
"replicaSet": {
"name": "ReplicatSet",
"primary": "Workshop-38:3306",
"status": "AVAILABLE",
"statusText": "All instances available.",
"topology": {
"10.0.10.39:3306": {
"address": "10.0.10.39:3306",
"instanceRole": "SECONDARY",
"mode": "R/O",
"replication": {
"applierStatus": "APPLIED_ALL",
"applierThreadState": "Slave has read all relay log; waiting for more updates",
"receiverStatus": "ON",
"receiverThreadState": "Waiting for master to send event",
"replicationLag": null
},
"status": "ONLINE"
},
"Workshop-38:3306": {
"address": "Workshop-38:3306",
"instanceRole": "PRIMARY",
"mode": "R/W",
"status": "ONLINE"
}
},
"type": "ASYNC"
}
}
|
mysqlrouter --force --user=root --bootstrap root@10.0.10.38:3306 --directory myrouter
#In Case Router from Remote Machine:-cluster in 10.0.10.14
mysqlrouter --bootstrap root@10.0.10.14:3310 --directory myrouter
|
myrouter/start.sh |
Step 5: Using Replica Set
mysqlsh
MySQL JS>
shell.connect("root@127.0.0.1:6446");
\sql
SQL>SELECT * FROM performance_schema.replication_group_members;
|
CREATE DATABASE sales;USE sales;
CREATE TABLE if not exists sales.employee(empid int primary key auto_increment,empname varchar(100),salary int,deptid int);
|
INSERT sales.employee(empname,salary,deptid) values('Ram',1000,10); |
INSERT sales.employee(empname,salary,deptid) values('Raja',2000,10); |
INSERT sales.employee(empname,salary,deptid) values('Sita',3000,20);
SELECT * FROM sales.employee;
|
Connect Router to another machine to verify changes.
mysqlsh
JS>shell.connect("root@127.0.0.1:6447");
\sql
SQL>SELECT * FROM sales.employee;
INSERT sales.employee values(100,'Ram',1000,10);
<Error> because this machine is not allowed to execute DML,DDL statements.>
|
##Create Disaster
#service mysqld stop
|
RS1= dba.getReplicaSet()
RS1.status();
MySQL 10.0.10.38:3306 ssl JS > RS1.status()
ReplicaSet.status: Failed to execute query on Metadata server 10.0.10.38:3306: Lost connection to MySQL server during query (MySQL Error 2013)
MySQL 10.0.10.38:3306 ssl JS > RS1.status()
ReplicaSet.status: The Metadata is inaccessible (MetadataError)
MySQL 10.0.10.38:3306 ssl JS > RS1.status()
ReplicaSet.status: The Metadata is inaccessible (MetadataError)
MySQL 10.0.10.38:3306 ssl JS >
|
MySQL-JS>shell.connect("root@localhost:6446");
Creating a session to 'root@10.0.10.38:6446'
Please provide the password for 'root@10.0.10.38:6446': ********
Shell.connect: Can't connect to remote MySQL server for client connected to '0.0.0.0:6446' (MySQL Error 2003)
|
#service mysqld start
MySQL 10.0.10.38:3306 ssl JS > RS1.status()
ReplicaSet.status: The Metadata is inaccessible (MetadataError)
MySQL 10.0.10.38:3306 ssl JS > RS1=dba.getReplicaSet()
You are connected to a member of replicaset 'ReplicatSet'.
<ReplicaSet:ReplicatSet>
RS1=dba.getReplicaSet()
RS1.status()
|
##Again Connect to Router to send the traffic
mysqlsh
shell.connect("root@localhost:6447");
\sql
SQL>SELECT * FROM sales.employee;
|
Fix :-
RS1= dba.getReplicaSet()
RS1.status();
|
Can you Observe one important observation? why port 6447 is executing R/W query?