Database Replication

3,477 views

Published on

A short introduction to database replication, theory and practice (MySQL, PostgreSQL, CouchDB)

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,477
On SlideShare
0
From Embeds
0
Number of Embeds
43
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Database Replication

  1. 1. Database replication Vatroslav Mileusnić, univ.bacc.inf. mileusnic.vatroslav@gmail.com
  2. 2. What is replication?Database replication - creating and maintaining multiple copies of the same databaseTypes of replication: synchronous and asynchronous master-slave and master-master (multimaster) ...
  3. 3. Synchronous Asynchronouspropagates every transaction propagates multiple transactionscreating connections for every transaction causes uses less network bandwidth bigger overhead because and offers better performancestops replication if one server is unavailable high availability of replication group possibility of dana collision possibility of lost transactions
  4. 4. Master-slave Multimaster1 master, other slaves all servers are masterswrite on master write on any server,propagate to slaves propagate to othersdivide reading between divide reading between all slaves serversconfigure slave to take powerful and flexible over as master if the solution, but also very original master falls out complex
  5. 5. Multimaster replication problemspossibility of conflict in asynchronous replication update conflict unique conflict delete conflictpossible solutions: configure application to write in only one database configure application to write in another database if the first one falls out
  6. 6. Multimaster replication problemsvery short intervals of empting queries uses a huge number of system resources starting and stopping the process requires a lot processor cyclessolution: dont exaggerate with application requrements intervals of 1 or more minutes are fine
  7. 7. Multimaster replication problemstool for monitoring replication system if using synchronous replication, all servers have to be active and ready automated tool that periodicaly checks the status of all servers if the tool decides a server is unavailable, it should try to restart it, and if it fails, remove it from replication, so replication can go on
  8. 8. Primjer replikacijeAsynchronous master-slave replication in MySQLAsynchronous master-slave replication in PostgreSQLAsynchronous multimaster replication in CouchDBDisplayed solutions are not configured for production servers, but are good enough for learning and understanding
  9. 9. MySQLOn servers create a database (replicate_me)On master server change /etc/mysql/my.cnf #skip-networking #bind-address = 127.0.0.1 log-bin = /var/log/mysql/mysql-bin.log binlog-do-db = replicate_me server-id=1Save file and restart MySQL
  10. 10. MySQLEnter MySQL and grant rights to the slave user GRANT REPLICATION SLAVE ON *.* TO slave_user@192.168.1.101 IDENTIFIED BY password; GRANT RELOAD SLAVE ON *.* TO slave_user@192.168.1.101 IDENTIFIED BY password; GRANT SUPER SLAVE ON *.* TO slave_user@192.168.1.101 IDENTIFIED BY password; FLUSH PRIVILEGES;
  11. 11. MySQLPerform commands to display masters status USE replicate_me; SHOW MASTER STATUS; take note of File and Position columns UNLOCK TABLES;
  12. 12. MySQLOn slave server add in /etc/mysql/my.cnf server-id = 2 master-host = 192.168.1.100 master-user = slave_user master-password = password master-connect-retry = 60 replicate-do-db = replicate_me
  13. 13. MySQLOn slave server, in MySQL, do STOP SLAVE; CHANGE MASTER TO MASTER_HOST = 192.168.1.100 MASTER_USER=slave_user, MASTER_PASSWORD=password, MASTER_LOG_FILE=mysql-bin.000001, MASTER_LOG_POS=106; START SLAVE;
  14. 14. PostgreSQLAs postgres user initialize cluster on both master and slave servers initdb /var/lib/pgsql/dataAllow communication of postgres user SSH or another way
  15. 15. PostgreSQLOn master in /var/lib/pgsql/data/postgresql.conf wal_level = hot_standby archive_mode = on archive_command = scp %p postgres@192.168.1.101:/var/lib/pgsql/archive/%fStart cluster pg_ctl -D /var/lib/pgsql/data start
  16. 16. PostgreSQLPerform backup psql -c "SELECT pg_start_backup(initial backup)" template1 tar cvf pg_base_backup.tar /var/lib/pgsql/data psql -c "SELECT pg_stop_backup()" template1Copy the result file to slave and unpack mv /var/lib/pgsql/pg_base_backup.tar / tar xvf pg_base_backup.tarOn slave, delete /data/postmaster.pid
  17. 17. PostgreSQLMaster, /var/lib/pgsql/data/postgresql.conf listen_addresses = * max_wal_senders = 5 wal_keep_segments = 32Master, /var/lib/pgsql/data/pg_hba.conf host replication postgres 192.168.1.101/24 trust
  18. 18. PostgreSQLOn slave server /var/lib/pgsql/data/postgresql.conf hot_standby = onAdd recovery.conf standby_mode = on primary_conninfo = host=192.168.1.100 port=5432 user=postgres should contain additional settingsStart servers pg_ctl -D /var/lib/pgsql/data start
  19. 19. CouchDBAccess administration console Futon <computers IP address>:5984/_utilsIn Configuration set it so it listens to all IP addresses bind_address = 0.0.0.0Create databasesIn Replicator set <another computers IP address>/database_on_other_computer turn on Continuous, click Replicate
  20. 20. CouchDBTo do multimaster replication, repeat steps on other computer <another computers IP address>/database_on_first_computer turn on Continuous click Replicate
  21. 21. ZaključakImpressions usual MySQL vs PostgreSQL discussion realisticaly: we wont choose a system solely based on whose replication is better for small projects and home usage, CouchDBhr.linkedin.com/in/vatroslavmileusnic
  22. 22. Questions?

×