MySQL master-slave replication principle

Source: Internet
Author: User

A simple and complete Mysql master-slave copy, read and write separated.


1. First build Mysql master-slave architecture to automatically copy Mater data to Slave

MySQL replication works very simply, one server as a host, and one or more servers as slave computers. The host will log changes to the database. Once these changes are recorded to the log, they are sent to the slave immediately (or at a set interval).


Using MySQL replication provides the ability to scale large Web sites where the databases are primarily read operations (selects). Slave-to-host consumed are rare (typically 1% overhead per slave), and deploying 30 slave machines per host in a large web site is also common.

Asynchronous replication vs. synchronous replication

Asynchronous replication: MySQL natively supports one-way, asynchronous replication. Asynchronous replication means there is a delay in copying data from one machine to another-most importantly this means that the data cannot be copied/applied to the slave at the same time when the application's transaction commit has been confirmed. Typically, this delay is determined by network bandwidth, resource availability, and system load. However, with the right components and tuning, replication can be done almost instantaneously.

Synchronous replication: Synchronous replication can be defined as data being submitted to one or more machines at the same time, usually through a well-known "two-phase commit". While this does give you consistency in multiple systems, it also degrades performance due to increased additional message exchange.

MySQL, which uses the MyISAM or InnoDB storage engine, does not natively support synchronous replication, but some technologies, such as the Distributed Replication Block device (DRBD), can provide synchronous replication on the underlying file system. Allow the second MySQL server to take over if the primary server is lost (using a replica of the second server). To learn more, see: http://www.drbd.org/





Asynchronous replication scenarios:

1. Mysql Database Installation

The installation process is omitted: See more: http://pengranxiang.iteye.com/admin/blogs/1138059

Server master:192.168.14.131

Mysql installation directory:/home/mysql/mysql (use source installation, standalone directory)

Server slave:192.168.14.132

Mysql installation directory:/home/mysql/mysql

2. Modify the configuration

In order not to affect the original configuration file:/etc/my.cnf

Create a new configuration file,

Cp/etc/my.cnf/home/mysql/mysql/conf/master.cnf

Cp/etc/my.cnf/home/mysql/mysql/conf/slave.cnf

Modify the MASTER.CNF, add the following settings,

(Official note: In order to use transactional InnoDB for maximum persistence and consistency in replication, you should specify the innodb_flush_log_at_trx_commit=1,sync_binlog=1 option.) )

CNF Code
    1. Log-bin=mysql-bin #slave会基于此log-bin to do replication.
    2. server-id=1 #master的标示
    3. innodb_flush_log_at_trx_commit=1
    4. sync_binlog=1

Modify SLAVE.CNF

CNF Code
    1. [Mysqld]
    2. server-id=2 #slave的标示

3. Start the service

CNF Code
    1. # Master
    2. # If Mysql is started, turn it off first.
    3. /home/mysql/mysql/bin/mysqladmin-u root-p shutdown
    4. # start MySQL with the modified MASTER.CNF
    5. /home/mysql/mysql/bin/mysqld_safe--DEFAULTS-FILE=/HOME/MYSQL/MYSQL/CONF/MASTER.CNF &

Java code
    1. # Slave
    2. # If Mysql is started, turn it off first.
    3. /home/mysql/mysql/bin/mysqladmin-u root-p shutdown
    4. # start MySQL with the modified SLAVE.CNF
    5. /home/mysql/mysql/bin/mysqld_safe--DEFAULTS-FILE=/HOME/MYSQL/MYSQL/CONF/SLAVE.CNF &

4. Create an account on Master that is dedicated to replication Repl_user


5. Start the master-slave copy function

Need to see the master status in master

Mysql> Show master status;

And then Slave, start the replication


The above window is the connection Master, the following window connects Slave

6. Test replication

Insert a piece of data in Master and then query in Slave. can be verified.


2 Simple read-write separation implementation

The read-write separation can be implemented either directly on the client or through a proxy server.

Proxy servers generally have the option of:

Official: MySQL proxy address: http://dev.mysql.com/downloads/mysql-proxy/#downloads

Domestic Open source project: Amoeba

Amoeba Developer Blog: http://amoeba.meidusa.com

Amoeba Open Source project address: Http://www.sourceforge.net/projects/amoeba
Amoeba Chinese Document: Http://amoeba.meidusa.com/amoeba.pdf

This demonstrates only the simplest scenario: JDBC directly implements read-write separation.

Java code
  1. Package Prx.dao;
  2. Import java.sql.Connection;
  3. Import Java.sql.ResultSet;
  4. Import java.util.Properties;
  5. Import Com.mysql.jdbc.ReplicationDriver;
  6. Public class Test {
  7. public static void Main (string[] args) throws Exception {
  8. Replicationdriver Driver = new Replicationdriver ();
  9. Properties props = new properties ();
  10. //We want this for failover on the slaves
  11. Props.put ("AutoReConnect", "true");
  12. //We want to load balance between the slaves
  13. Props.put ("roundrobinloadbalance", "true");
  14. Props.put ("user", "foo");
  15. Props.put ("password", "bar");
  16. //    
  17. //Looks like a normal MySQL JDBC URL with a
  18. //comma-separated list of hosts, the first
  19. //Being the ' master ', the rest being any number
  20. //Of slaves that the driver would load balance against
  21. //    
  22. Connection conn = Driver.connect (
  23. "Jdbc:mysql://master,slave1,slave2,slave3/test", props);
  24. //    
  25. //Perform read/write work on the master
  26. //By setting the READ-ONLY flag to "false"
  27. //  
  28. //Whether the Conn ReadOnly is true to determine if the database to be connection connected is the primary database or the database
  29. //False connection to primary database
  30. //True for connections from the database
  31. Conn.setreadonly (false);
  32. Conn.setautocommit (false);
  33. Conn.createstatement (). Executeupdate ("UPDATE some_table ....");
  34. Conn.commit ();
  35. //    
  36. //Now, does a query from a slave, the driver automatically picks one
  37. // from the list
  38. //    
  39. Conn.setreadonly (true);
  40. ResultSet rs = conn.createstatement (). ExecuteQuery (
  41. "Select a, b from alt_table");
  42. }
  43. }

MySQL master-slave replication principle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.