# Master Slave Replication
Master Slave Replication in windows, across two different MySQL versions(5.7.31 and 8.0.22)
## Getting Started
## Prerequisites
Download mysql-8.0.22-winx64.zip and mysql-5.7.31-winx64.zip from -
```
https://dev.mysql.com/downloads/mysql/
```
## Configuring ans initilizing the servers
### Configuring options file-
For master as-
```
[mysqld] //for group mysqld
server-id=1 //set server-id 1
basedir=D:\\111111\\Softwares\\sql\\mysql-5.7.31-winx64\\mysql-5.7.31-winx64 \\directory of mysql extracted zip
datadir=D:\\111111\\Softwares\\sql\\mysql-5.7.31-winx64\\mysql-5.7.31-winx64\\data \\directory where all the data of server is to be strore
log-bin=mysql-bin.log //location of log dump and also enables binary loggin
innodb_flush_log_at_trx_commit = 1
sync_binlog = 1 //enables sync of bin log
binlog-format = ROW
```
For slave as-
```
[mysqld]
server-id=2 //different server id than master version
port=3307 //different port than master as both are running on same ip i.e. 127.0.0.1
basedir=D:\\111111\\Softwares\\sql\\mysql-8.0.22-winx64\\mysql-8.0.22-winx64 \\directory of extracted zip
datadir=D:\\111111\\Softwares\\sql\\mysql-8.0.22-winx64\\mysql-8.0.22-winx64\\data \\directory where all the data is to be stored
skip-log-bin \\disables binary logging
```
### Initializing database
```
mysqld.exe --defaults-file="basedir_master\my.ini.txt" --initialize --console
mysqld.exe --defaults-file="basedir_slave\my.ini.txt" --initialize --console
```
### Start servers and logging in
##### Start servers-
```
mysqld.exe --defaults-file="basedir_master\my.ini.txt" --console
mysqld.exe --defaults-file="basedir_slave\my.ini.txt" --console
```
--initialize : initializes database directory
--console : outputs logs in terminal
##### Login
```
mysql.exe --port=3306 -u root -p
mysql.exe --port=3307 -u root -p
```
## Configurin master-slave
Create a new user for replication:
```
mysql> CREATE USER 'replication'@'%' IDENTIFIED BY 'replication';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%';
```
From basedir_master/bin:
```
mysqldump.exe -u root -p --ignore-table=mysql.innodb_index_stats --ignore-table=mysql.innodb_table_stats --all-databases --master-data=2 > replicationdump.sql
```
Check replicationdump.sql for line-
```
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=609;
```
Import the dump you created in the previous step into the slave instance:
```
mysql -uroot -p --host=127.0.0.1 --port=3307 < replicationdump.sql
```
Connect to the slave instance using the MySQL client with the appropriate host and port:
```
mysql -uroot -p --host=127.0.0.1 --port=3307
```
Update the master information in slave server (take the replication coordinates from the dump file replicationdump.sql, as explained above):
```
mysql> CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_USER='replication', MASTER_PASSWORD='replication', MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=609;
```
Start the slave:
```
mysql> START SLAVE;
```
Check status:
```
mysql>SHOW SLAVE STATUS \G
```
If you get the following output, replication was successful:
```
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 1800
Relay_Log_File: DESKTOP-1U6PSDP-relay-bin.000002
Relay_Log_Pos: 1513
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_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_Master_Log_Pos: 1800
Relay_Log_Space: 1732
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 4d488924-1ff9-11eb-968c-9828a610e132
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.01 sec)
```
## Built With
* [MySQL](https://www.mysql.com/) - Database server.
## Authors
* **Abhishek Kumar Yadav** - *Initial work* - [abhk943](https://github.com/abhk943)
## Acknowledgments
* [Toptal-Master-Slave](https://www.toptal.com/mysql/mysql-master-slave-replication-tutorial)