Database Replication


Published on

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

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Database Replication

  1. 1. Database replication Vatroslav Mileusnić, univ.bacc.inf.
  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 = 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@ IDENTIFIED BY password; GRANT RELOAD SLAVE ON *.* TO slave_user@ IDENTIFIED BY password; GRANT SUPER SLAVE ON *.* TO slave_user@ 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 = 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 = 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@ 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/
  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 trust
  18. 18. PostgreSQLOn slave server /var/lib/pgsql/data/postgresql.conf hot_standby = onAdd recovery.conf standby_mode = on primary_conninfo = host= 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 = 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,
  22. 22. Questions?