主頁 > 知識庫 > MySQL 讀寫分離實例詳解

MySQL 讀寫分離實例詳解

熱門標簽:外呼系統(tǒng)怎樣才能不封號 西藏地圖標注改進點 地圖標注什么軟件好用 如何辦理400客服電話 地圖標注專員入駐 地圖標注百度競價 神行者百貨商場地圖標注 安陽手機自動外呼系統(tǒng)原理是什么 外呼系統(tǒng)線路經(jīng)常出問題嗎

MySQL 讀寫分離

MySQL讀寫分離又一好辦法 使用 com.mysql.jdbc.ReplicationDriver

在用過Amoeba 和 Cobar,還有dbware 等讀寫分離組件后,今天我的一個好朋友跟我講,MySQL自身的也是可以讀寫分離的,因為他們提供了一個新的驅(qū)動,叫 com.mysql.jdbc.ReplicationDriver

說明文檔:http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-replication-connection.html

 代碼例子:

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

感謝閱讀,希望能幫助到大家,謝謝大對本站的支持!

您可能感興趣的文章:
  • php實現(xiàn)帶讀寫分離功能的MySQL類完整實例
  • MySQL5.6 Replication主從復制(讀寫分離) 配置完整版
  • MySQL的使用中實現(xiàn)讀寫分離的教程
  • Yii實現(xiàn)MySQL多數(shù)據(jù)庫和讀寫分離實例分析
  • Thinkphp實現(xiàn)MySQL讀寫分離操作示例
  • 通過mysql-proxy完成mysql讀寫分離
  • 使用PHP實現(xiàn)Mysql讀寫分離
  • Ubuntu10下如何搭建MySQL Proxy讀寫分離探討
  • MySQL主從同步、讀寫分離配置步驟
  • mysql 讀寫分離(實戰(zhàn)篇)
  • mysql 讀寫分離(基礎(chǔ)篇)

標簽:阜陽 酒泉 AXB 貴港 衡水 張掖 雞西 萍鄉(xiāng)

巨人網(wǎng)絡(luò)通訊聲明:本文標題《MySQL 讀寫分離實例詳解》,本文關(guān)鍵詞  MySQL,讀寫,分離,實例,詳解,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySQL 讀寫分離實例詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于MySQL 讀寫分離實例詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章