在用过Amoeba 和 Cobar,还有dbware 等读写分离组件后,今天我的一个好朋友跟我讲,MySQL自身的也是可以读写分离的,因为他们提供了一个新的驱动,叫 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");
.......
}
}
原文出自:http://www.cnblogs.com/taven/archive/2013/04/24/3040489.htmlimport 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");
.......
}
}
相关推荐
MySQL读写分离又一好办法 使用 com.mysql.jdbc.ReplicationDriver 在用过Amoeba 和 Cobar,还有dbware 等读写分离组件后,今天我的一个好朋友跟我讲,MySQL自身的也是可以读写分离的,因为他们提供了一个新的驱动,...
springboot 实现mysql读写分离 通过aop 并且根据方法名进行分类 实现读写分离
springboot集成shardingJDBC实现读写分离demo工程,亲测可用 使用方式见博客:https://maoqizhi.blog.csdn.net/article/details/123149268?spm=1001.2014.3001.5502
(双主多从、故障转移因mysql环境为一主一从,所以代码未实现,不难实现,需要实现的代码位置已标记) (3)、非注解方式,业务代码侵入基本为零 (4)、懒事务处理方式,service层使用@Transactional注解的方法...
Amoeba搞定mysql主从读写分离
Sharding-JDBC教程:Spring Boot整合Sharding-JDBC实现读写分离
Sharding-JDBC教程:Spring Boot整合Sharding-JDBC实现分库分表+读写分离
6. 掌握MySQL读写分离的原理 1. mysql-proxy (GA) 2. mysql router(待完善) 3. Mycat 7. 要理解为什么分库分表 8. 要理解如何分库分表 9. 什么时候选择垂直分库、什么时候选择水平分表 10. 要明白分库分表中会...
datax读写MySQL8的插件,修改源码重新编译而来
详细介绍Mysql、MariaDB主从复制、多主多从架构、负载平衡和集群的设置。读写分离和数据库垂直、水平切分建议使用Sharding JDBC
针对这一情况,提出在现有硬件的基础上利用JDBC规范与MySQL Replication实现数据库集群从而解决数据访问瓶颈。其主要方法是在进行JDBC连接之前实现负载均衡,所有SQL请求由负载均衡器进行统一调度。在数据库端利用...
读写分离 HA架构 监控审计 压力测试 性能优化 自动化运维 ==数据的存储方式 1. 人工管理阶段 2. 文件系统阶段 3. 数据库系统管理阶段 ==数据库技术构成 1. 数据库系统 DBS A.数据库管理系统(DataBase ...
sharding-jdbc多数据源,实现读写分离
在局域网环境中android 访问Windows共享的MySQL数据库,并可以进行读写操作,使用的jdbc的jar包
Mysql主从复制,MySQL读写分离(Mycat和ShardingJdbc两种方式),MySQL索引(数据结构和失效原因),MySqL性能优化(慢查询),MySQL分库分表(Mycay和ShardingJdbc),MySqL集群问题
MySQL.jdbc.driver=com.mysql.jdbc.Driver MySQL.jdbc.url=jdbc:mysql://127.0.0.1:3306/svnadmin?characterEncoding=utf-8 MySQL.jdbc.username=root MySQL.jdbc.password=root 9.把phpMyAdmin解压到\JspStudy\...
采⽤Sharding-JDBC实现订单表的 分库分表 和 读写分离,搭建 两套一主二从 主从集群
支持读写分离,支持MySQL双主多从,以及一主多从的模式; 支持全局表,数据自动分片到多个节点,用于高效表关联查询; 支持独有的基于E-R 关系的分片策略,实现了高效的表关联查询; 多平台支持,部署和实施简单。
springboot 使用jdb直接读取数据库,并且将.sql文件中的备份数据恢复到mysql。涉及到jdbc操作数据库,和java对大文件io读写操作。
通过ShardingSphere的Sharding-jdbc实现MySQL数据库的读写分离简单例子,代码里面有测试数据库的建库sql,主从复制需要用户自己配置