Preliminary finishing of mysql cluster

If the mySql database adopts the replication (Replication) cluster, there are several ways to separate read and write in the program:

The automatic master-slave management scheme includes: MHA official introduction: https://code.google.com/p/ mysql-master-ha/

1. Using the ReplicationDriver driver provided by MySql, the simple process is as follows:
  
   Disadvantages: Cannot be integrated into Spring for read-write separation operation.

public static void main(String[] args) throws Exception {
    ReplicationDriver driver = new ReplicationDriver();
 
    Properties props = new Properties();
 
    // We want this for failover on the replicas
    props.put("autoReconnect", "true" );
 
    // We want to load balance between the slaves
    props.put("roundRobinLoadBalance", "true");
 
    props.put("user", "foo");
    props.put("password", "bar") ;
 


    // comma-separated list of hosts, the first
    // being the 'master', the rest being any number
    // of slaves that the driver will load balance against
    //
 
    Connection conn =
        driver.connect("jdbc:mysql:/ /master,slave1,slave2,slave3/test",
            props);
 
    //
    // Perform read/write work on the master
    // by setting the read-only flag to "false"
    //
 
    //This node should be through spring Set up the transaction management of the conn object, and this conn object should not be a real connection,
    // but a proxy class. By setting readonly, the proxy class will use different connections.
    //Then the problem is the connection used by the proxy class. Where did you get it, or does it open a new connection every time? , you need to see the source code
        
   conn.setReadOnly(false);
 
    conn.setAutoCommit(false);
    conn.createStatement().executeUpdate("UPDATE some_table ....");
    conn.commit();
 
    //
    // Now, do a query from a slave, the driver automatically picks one
    // from the list
    //
 
    conn .setReadOnly(true);
 
    ResultSet rs =
      conn.createStatement().executeQuery("SELECT a,b FROM alt_table");
 
     .......
  }


2. Use multiple data source configuration, such as: configure write data source , read the data source, and choose to use it in the program.

#Read data source
DBDriver=com.mysql.jdbc.Driver 
url=jdbc\:mysql\:loadbalance\://slave1,slave2,slave3\:3306/test?roundRobinLoadBalance\=true&characterEncoding\=UTF-8 
name=test 
pass =test 
characterEncoding=utf8 #Write

data source
DBDriver=com.mysql.jdbc.Driver 
url=jdbc\:mysql\:loadbalance\://master\:3306/test?roundRobinLoadBalance\=true&characterEncoding\=UTF-8 
name=test 
pass=test
characterEncoding=utf8

3. Mysql Router
 
  MySQL Proxy is not GPL, so choose MySql Router,
  MySQL Router is a lightweight middleware that provides transparent routing between applications and backend databases. It is used by mysql to achieve load balancing and high availability functions.
  At the same time, the router also provides a way to use fabric high availability.

  In configuration:
  When mode = read-only,
   client requests will be distributed to the configuration list cyclically. When the server in the configuration list is unavailable, the server will be skipped,
   and the next available machine in the list will process the request, if the list is not available server, routing will fail.

  When mode = read-write,
     all client requests will be processed by the first available server in the list. When this server is down,
     the next available machine in the list will process client requests. If there is no available server in the list, the route will be fail.
     The information of the first server that is successfully connected will be stored in memory and will be invalid after restarting the router.

  This allows access to mysql through the Router, which doesn't seem to have any obvious benefits (except for load balancing).

4. The following database middleware can access the mysql database cluster with read-write separation. The excerpts are as follows for viewing.

  A. Amoeba
     Baidu knows the address: http://baike.baidu.com/link?url=N2uPirw5znkFAvFUh0PDLlRLy7flqfUNdJmsP6oNCm-ziGrFSxzX-H1_fnjJ1FmjGjyZ1daDPc_euxVwcg-OBq
     From the official website, the last update is: Last Update: 2013-07-05

  B. Mycat
    http: //www.mycat.org.cn/
    The most active and best-performing open source database middleware in China!
   
    This is very active, people of the country, please strongly support it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326395366&siteId=291194637