閱讀738 返回首頁    go 英雄聯盟


RDS MySQL 實現讀寫分離__MYSQL使用_技術運維問題_雲數據庫 RDS 版-阿裏雲

 

基礎配置流程與要求:

1. 主實例必須為 RDS MySQL 5.6 版本。

2. 在主實例的【實例基本信息】頁麵購買隻讀實例。

rd_01.png

3. 在應用側配置判斷和分配讀寫請求。

下麵是基於MySQL Connector/J 中 com.mysql.jdbc.ReplicationDriver 驅動的應用側配置讀寫分離樣例:

具體請參考:https://dev.mysql.com/doc/connector-j/en/connector-j-master-slave-replication-connection.html 

注:其他應用側實現讀寫分離的方式和配置(比如通過MySQL Proxy、Amoeba、Cobar或DBWare)請參考對應實現的技術文檔。

 

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Properties;

import com.mysql.jdbc.ReplicationDriver;

public class ReplicationDriverDemo {

  public static void main(String[] args) throws Exception {
    ReplicationDriver driver = new ReplicationDriver();

    Properties props = new Properties();

    // We want this for failover on the slaves
    props.put("autoReconnect", "true");

    // We want to load balance between the slaves
    props.put("roundRobinLoadBalance", "true");

    props.put("user", "foo");
    props.put("password", "bar");

    //
    // Looks like a normal MySQL JDBC url, with a
    // 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:replication://master,slave1,slave2,slave3/test",
            props);

    //
    // Perform read/write work on the master
    // by setting the read-only flag to "false"
    //

    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");

     .......
  }
}

 

 

如問題還未解決,請聯係售後技術支持

 

最後更新:2016-07-12 15:28:46

  上一篇:go 雲服務器如何通過內網訪問RDS?__MYSQL使用_技術運維問題_雲數據庫 RDS 版-阿裏雲
  下一篇:go RDS MySQL權限問題(錯誤代碼:1227,1725)__MYSQL使用_技術運維問題_雲數據庫 RDS 版-阿裏雲