国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

MySQL 讀寫分離實(shí)例詳解

2024-07-24 12:53:18
字體:
供稿:網(wǎng)友

MySQL 讀寫分離

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

在用過Amoeba 和 Cobar,還有dbware 等讀寫分離組件后,今天我的一個(gè)好朋友跟我講,MySQL自身的也是可以讀寫分離的,因?yàn)樗麄兲峁┝艘粋€(gè)新的驅(qū)動(dòng),叫 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"); ....... }}

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

您可能感興趣的文章:

php實(shí)現(xiàn)帶讀寫分離功能的MySQL類完整實(shí)例MySQL5.6 Replication主從復(fù)制(讀寫分離) 配置完整版MySQL的使用中實(shí)現(xiàn)讀寫分離的教程Yii實(shí)現(xiàn)MySQL多數(shù)據(jù)庫和讀寫分離實(shí)例分析Thinkphp實(shí)現(xiàn)MySQL讀寫分離操作示例通過mysql-proxy完成mysql讀寫分離使用PHP實(shí)現(xiàn)Mysql讀寫分離Ubuntu10下如何搭建MySQL Proxy讀寫分離探討MySQL主從同步、讀寫分離配置步驟mysql 讀寫分離(實(shí)戰(zhàn)篇)mysql 讀寫分離(基礎(chǔ)篇)
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 凤凰县| 广汉市| 东丰县| 溧水县| 兴山县| 涿鹿县| 凭祥市| 青浦区| 苍南县| 固始县| 拜城县| 虎林市| 岳西县| 云龙县| 砚山县| 金乡县| 绍兴县| 永清县| 贺兰县| 濮阳市| 务川| 西峡县| 梁河县| 新营市| 龙游县| 获嘉县| 砚山县| 曲麻莱县| 晋中市| 赣榆县| 梅河口市| 临邑县| 项城市| 南汇区| 苗栗县| 华容县| 当阳市| 独山县| 鄂温| 桑日县| 小金县|