{"id":22513418,"url":"https://github.com/emahtab/mysql-master-slave-replication","last_synced_at":"2025-03-28T01:23:24.301Z","repository":{"id":266595055,"uuid":"898797668","full_name":"eMahtab/mysql-master-slave-replication","owner":"eMahtab","description":"A demo showing MySQL data replication from Master to Slave","archived":false,"fork":false,"pushed_at":"2024-12-05T12:49:16.000Z","size":793,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-02T03:19:09.555Z","etag":null,"topics":["master-slave-replication","mysql","replication"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-05T03:50:27.000Z","updated_at":"2024-12-05T12:49:19.000Z","dependencies_parsed_at":"2024-12-05T04:26:54.263Z","dependency_job_id":"d6ae3c67-24d8-4ff9-a01f-09c666c3d6f7","html_url":"https://github.com/eMahtab/mysql-master-slave-replication","commit_stats":null,"previous_names":["emahtab/mysql-master-slave-replication"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmysql-master-slave-replication","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmysql-master-slave-replication/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmysql-master-slave-replication/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmysql-master-slave-replication/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/mysql-master-slave-replication/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245950635,"owners_count":20699102,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["master-slave-replication","mysql","replication"],"created_at":"2024-12-07T03:12:17.801Z","updated_at":"2025-03-28T01:23:24.279Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# MySQL Master Slave Replication\n\n**A demo showing MySQL data replication from Master to Slave**, in this example we setup a single master and a single slave or replica.\n\n### !!! Prerequisite : You would need Docker installed on your system to follow along this demo.\n\n# Replication :\nReplication enables data from one database server (known as a source or master) to be copied to one or more database servers (known as replicas or slaves).\nReplication is usually asynchronous by default; replicas do not need to be connected permanently to receive updates from a source.\n\n## Note : There can be many Replication topologies possible, in this one we setup a single MySQL Master and a single MySQL Slave(Replica). Data is replicated from the master to the slave/replica but not the other way. In Master-Slave topology data is replicated in only one way that is from master to slave/replica. \n\n## Step 1 : Create the Docker compose file and execute docker compose up\nBelow docker-compose.yml declares two services, named as mysql_master and mysql_slave_1 (in docker compose file actual containers are named as mysql-master and mysql-slave-1). We are using **mysql:8.0** as the docker image, and declare root user password as `toor` and create a test database.\n**Make sure docker engine is running on your host machine before running the `docker compose up` command.**\n\n```yml\n---\nversion: \"2\"\nservices:\n  mysql_master:\n    image: mysql:8.0\n    container_name: mysql-master\n    volumes:\n      - mysql-master-volume:/tmp\n    command:\n      [\n        \"mysqld\",\n        \"--datadir=/tmp/master/data\",\n        \"--log-bin=bin.log\",\n        \"--server-id=1\"\n      ]\n    environment:\n      \u0026mysql-default-environment\n      MYSQL_ROOT_PASSWORD: toor\n      MYSQL_DATABASE: test\n      MYSQL_USER: test_user\n      MYSQL_PASSWORD: insecure\n    ports:\n      - \"3308:3306\"\n\n  mysql_slave_1:\n    image: mysql:8.0\n    container_name: mysql-slave-1\n    volumes:\n      - mysql-replica1-volume:/tmp\n    command:\n      [\n        \"mysqld\",\n        \"--datadir=/tmp/slave/data1\",\n        \"--log-bin=bin.log\",\n        \"--server-id=2\"\n      ]\n    environment: *mysql-default-environment\n    ports:\n      - \"3309:3306\"\n\nvolumes:\n  mysql-master-volume:\n  mysql-replica1-volume:\n```\n\n![\"Running MySQL Master and Slave as Docker Containers\"](docker-compose-up.png?raw=true)\n\n![\"MySQL Master and Slave containers\"](docker-containers.png?raw=true)\n\n## Step 2 : Create Replication user on Master with REPLICATION SLAVE privilege\nNext we need to create a replication user on Master and grant that user `REPLICATION SLAVE` privilege.\nTo do this, we execute bash against master **`docker exec -it mysql-master bash`** and connect to mysql **`mysql -uroot -ptoor`** running on master, then execute below mysql commands.\n```sql\nCREATE USER 'replicator'@'%' IDENTIFIED BY 'rotacilper';\nGRANT REPLICATION SLAVE ON *.* TO 'replicator'@'%';\nFLUSH PRIVILEGES;\n```\nHere we create a replication user called `replicator` with password `rotacilper` and grant this user **`REPLICATION SLAVE`** privilege, and finally flush privileges.\n![\"Create Replication user on Master\"](create-replication-user.png?raw=true)\n\n## Step 3 : Execute SHOW MASTER STATUS on MySQL Master \nGet the master status, execute the command **`SHOW MASTER STATUS;`** on Mysql Master to find the Binlog file and position.\n\n![\"Get Master status\"](show-master-status.png?raw=true)\n\n## Step 4 : Connect to MySQL slave and execute CHANGE MASTER TO\nNext we need to execute **`CHANGE MASTER TO`** command on MySQL slave. Connect to MySQL slave and execute below command, update MASTER_LOG_FILE and MASTER_LOG_POS values which you get from executing SHOW MASTER STATUS command on MySQL master.\n```sql\nCHANGE MASTER TO\n  MASTER_HOST='mysql_master',\n  MASTER_PORT=3306,\n  MASTER_USER='replicator',\n  MASTER_PASSWORD='rotacilper',\n  MASTER_LOG_FILE='bin.000004',\n  MASTER_LOG_POS=871,\n  GET_MASTER_PUBLIC_KEY=1;\n```\n![\"Execute Change Master to command on MySQL Slave\"](change-master-to.png?raw=true)\n\n## Step 5 : Execute START SLAVE on MySQL slave\nExecute the command **`START SLAVE;`** on MySQL slave to start the replication, after executing **`START SLAVE;`** you can optionally run **`SHOW REPLICA STATUS;`** to get the status of replica.\nOne of the most important parameter is **`Seconds_Behind_Source`** which tells how much behind, replica is from master, ideally **`Seconds_Behind_Source`** should always be 0, which means replica is up to date with master.\n\n![\"Start slave for replication\"](start-slave.png?raw=true)\n\n## Step 6 : See Replication in action\nBy performing the step 1 to 5, we have set the replication on Slave from Master, now whatever changes are done on master mysql will be replicated to slave automatically.\n\n![\"Update Master database\"](update-database.png?raw=true)\n\nWe create `users` table under test database on the Master and insert records into the users table.\n\n![\"Replication on Slave\"](replication-on-slave.png?raw=true)\n\n## Step 6a : See Replication in action : update on master database are replicated to slave\nWe delete 4 users having id as either 3, 7, 9 or 10 on the mysql master.\n\n![\"Deleting some of the user records\"](delete-records-on-master.png?raw=true)\n\nThe changes in mysql master are replicated to mysql replica.\n\n![\"Replication on slave\"](slave-after-delete-on-master.png?raw=true)\n\n## !Important :  Data doesn't replicate from replica to master\n\n**Creating a table on replica and inserting a record**\n\n![\"In master-slave replication data is not replicated from slave to master\"](write-on-replica.png?raw=true)\n\n**Data is not replicated to master in Master-Slave replication**\n\n![\"Unidirectional Replication\"](unidirectional-replication.png?raw=true)\n\n# References :\n1. https://victoronsoftware.com/posts/mysql-master-slave-replication/\n2. https://www.youtube.com/watch?v=nMbb1199HQU\n3. https://dev.mysql.com/doc/refman/8.4/en/replication.html\n4. https://dev.mysql.com/doc/refman/8.4/en/faqs-replication.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fmysql-master-slave-replication","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fmysql-master-slave-replication","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fmysql-master-slave-replication/lists"}