一、方案选择
当数据库表中的记录量达到百万级别以上,那么就必须考虑分库分表或者使用分布式数据库。分库分表是比较常见的方案,一种是在客户端直接实现,比较出名的有sharding-jdbc,另一种是使用代理方式,常用的有mycat。分布式数据库有最新的tidb,也有阿里的oceandb。
1.1 对比
- sharding-jdbc:最新版的叫做sharding-sphere,持续在开发和维护,由来自京东的技术大牛主导,已经有广泛的应用。分片配置灵活,都在客户端代码直接实现,性能损失少,但是维护会比较麻烦,需要逐个项目的修改配置。不能进行跨库连接,代码需要进行改造。在扩展数据库服务器时需要考虑一致性哈希问题,或者采用分片键局部取模方式,也难免要进行部分的数据迁移。
- mycat:由来自阿里的技术大牛主导,也有较多的实际应用。分片不要在客户端实现,直接由代理服务器实现,客户端也有进行代码的改造。代理服务器可以管理数据,dba维护比较方便。但是代理服务器有性能瓶颈问题,同样也存在扩展麻烦的问题。
- tidb:直接可以将mysql原始数据库导入tidb,除了函数和触发器等一些特殊的对象。支持集群和扩展,解决了上面两种方案的真正痛点。但是该项目教新,还没有经过大量的生产环境检验,可能会存在一定的风险。
1.2 小结
所以3种方案各有优缺点,要根据实际情况选择,1和2相对比较成熟,如果技术团队人员够多,用成熟的方案没有风险,但是对于创业型的公司人才比较缺乏,选择tidb升级系统相对比较轻松,灵活性强。
二、tidb安装体验
2.1 ubuntu14.04安装tidb
虚拟机上安装的话,建议内存2G以上,推荐4G,硬盘20G以上。
- 安装docker和git
sudo apt-get update
wget -qO- https://get.docker.com/ | sh
sudo usermod -aG docker $(whoami)
sudo apt-get -y install python-pip
sudo pip install docker-compose
sudo apt-get install git - 安装tidb docker版本
git clone https://github.com/pingcap/tidb-docker-compose.git
cd tidb-docker-compose && docker-compose pull
Pulling dashboard-installer ... done
Pulling pd2 ... done
Pulling pd1 ... done
Pulling pd0 ... done
Pulling tikv0 ... done
Pulling tikv1 ... done
Pulling tikv2 ... done
Pulling prometheus ... done
Pulling tispark-master ... done 这个和下面的那个镜像下载比较慢需要耐心等待
Pulling tispark-slave0 ... done
Pulling tidb ... done
Pulling tidb-vision ... done
Pulling grafana ... done
Pulling pushgateway ... done
docker-compose up -d
Creating tidb-docker-compose_prometheus_1 ... done
Creating tidb-docker-compose_dashboard-installer_1 ... done
Creating tidb-docker-compose_pd2_1 ... done
Creating tidb-docker-compose_tidb-vision_1 ... done
Creating tidb-docker-compose_pushgateway_1 ... done
Creating tidb-docker-compose_grafana_1 ... done
Creating tidb-docker-compose_pd1_1 ... done
Creating tidb-docker-compose_pd0_1 ... done
Creating tidb-docker-compose_tikv0_1 ... done
Creating tidb-docker-compose_tikv1_1 ... done
Creating tidb-docker-compose_tikv2_1 ... done
Creating tidb-docker-compose_tidb_1 ... done
Creating tidb-docker-compose_tispark-master_1 ... done
Creating tidb-docker-compose_tispark-slave0_1 ... done
连接测试(默认密码为空):
mysql -h 127.0.0.1 -P 4000 -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.7.10-TiDB-v2.1.0-rc.2-49-geb617c5 MySQL Community Server (Apache License 2.0)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| INFORMATION_SCHEMA |
| PERFORMANCE_SCHEMA |
| mysql |
| test |
+--------------------+
4 rows in set (0.03 sec)
访问web控制台:
http://192.168.128.128:3000/
用户名和密码都是admin,界面相当惊艳!
2.2 数据导入
将原系统的数据导入到tidb中,可以使用navicat lite客户端工具,注意破解版的navicat在导入时采用了批量插入方式,好像超过了tidb的批量限制,也可以试试官方的导入工具。
除了函数和触发器一般的表结构和记录都能导入,导入速度会比较慢需要耐心等待,直到导入完毕!
导入完毕后跟普通的mysql使用没有什么两样,非常方便!
2.3 项目运行测试
如果没有用到函数几乎不用修改代码,只需要修改数据库配置文件即可。eclispe控制台的输出一摸一样,连接数据库成功。
2018-10-01 13:37:29,640 [http-bio-8083-exec-4] [com.alibaba.druid.pool.DruidDataSource]-[INFO] {dataSource-1} inited
2018-10-01 13:37:30,085 [http-bio-8083-exec-5] [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Acquired Connection [com.mysql.jdbc.JDBC4Connection@64ae575b] for JDBC transaction
2018-10-01 13:37:30,094 [http-bio-8083-exec-5] [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Switching JDBC Connection [com.mysql.jdbc.JDBC4Connection@64ae575b] to manual commit
2018-10-01 13:37:30,102 [http-bio-8083-exec-2] [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Acquired Connection [com.mysql.jdbc.JDBC4Connection@16082c75] for JDBC transaction
2018-10-01 13:37:30,102 [http-bio-8083-exec-2] [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Switching JDBC Connection [com.mysql.jdbc.JDBC4Connection@16082c75] to manual commit
2018-10-01 13:37:30,132 [http-bio-8083-exec-5] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Creating a new SqlSession
2018-10-01 13:37:30,137 [http-bio-8083-exec-3] [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Acquired Connection [com.mysql.jdbc.JDBC4Connection@58614fb7] for JDBC transaction
2018-10-01 13:37:30,137 [http-bio-8083-exec-3] [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Switching JDBC Connection [com.mysql.jdbc.JDBC4Connection@58614fb7] to manual commit
2018-10-01 13:37:30,150 [http-bio-8083-exec-3] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Creating a new SqlSession
2018-10-01 13:37:30,157 [http-bio-8083-exec-2] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Creating a new SqlSession
2018-10-01 13:37:30,168 [http-bio-8083-exec-4] [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Acquired Connection [com.mysql.jdbc.JDBC4Connection@356fcce9] for JDBC transaction
2018-10-01 13:37:30,169 [http-bio-8083-exec-4] [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Switching JDBC Connection [com.mysql.jdbc.JDBC4Connection@356fcce9] to manual commit
2018-10-01 13:37:30,176 [http-bio-8083-exec-3] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Registering transaction synchronization for SqlSession
分页,添加、修改、删除都没有问题。
2.4 分布式事务测试
事务是分库分表比较难解决的问题,在tidb中也进行了事务的简单测试。
public Integer updateSelective(T record) {
// record.setUpdateTime(new Date());
System.out.println("updateSelective-------------------------a");
Integer i=this.mapper.updateByPrimaryKeySelective(record);
System.out.println("updateSelective-------------------------b"+i);
i=1/0;//插入异常代码
System.out.println("updateSelective-------------------------c");
return i;
}
控制台输出,事务回滚正常,数据库中的记录并未修改:
2018-10-01 13:37:37,346 [http-bio-8083-exec-10] [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Switching JDBC Connection [com.mysql.jdbc.JDBC4Connection@356fcce9] to manual commit
updateSelective-------------------------a
2018-10-01 13:37:37,350 [http-bio-8083-exec-10] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Creating a new SqlSession
2018-10-01 13:37:37,350 [http-bio-8083-exec-10] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7c7ea421]
2018-10-01 13:37:37,410 [http-bio-8083-exec-10] [org.mybatis.spring.transaction.SpringManagedTransaction]-[DEBUG] JDBC Connection [com.mysql.jdbc.JDBC4Connection@356fcce9] will be managed by Spring
2018-10-01 13:37:37,411 [http-bio-8083-exec-10] [com.charge.mapper.TCodeitemMapper.updateByPrimaryKeySelective]-[DEBUG] ==> Preparing: UPDATE dbcharge.t_codeitem SET CODEITEM_REMARK = ?, CODEITEM_NAME = ?, CODEITEM_CONTENT = ?, CODEITEM_UPDATETIME = ?, CODEITEM_ENABLE = ?, CODEITEM_ADDTIME = ?, CODEITEM_CODE_ID = ?, CODEITEM_LABEL = ? WHERE CODEITEM_ID = ?
2018-10-01 13:37:37,412 [http-bio-8083-exec-10] [com.charge.mapper.TCodeitemMapper.updateByPrimaryKeySelective]-[DEBUG] ==> Parameters: 2277788(String), 首检(String), (String), 2018-10-01 13:37:37.346(Timestamp), 1(Integer), 2017-12-25 15:12:42.0(Timestamp), 1c1637d9bf32481b8f0789f41cf6b3f6(String), (String), 5dabb8c3ef4243fdbc4c15864a0706a3(String)
2018-10-01 13:37:37,421 [http-bio-8083-exec-10] [com.charge.mapper.TCodeitemMapper.updateByPrimaryKeySelective]-[DEBUG] <== Updates: 1
2018-10-01 13:37:37,421 [http-bio-8083-exec-10] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7c7ea421]
updateSelective-------------------------b1
2018-10-01 13:37:37,421 [http-bio-8083-exec-10] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7c7ea421]
2018-10-01 13:37:37,421 [http-bio-8083-exec-10] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7c7ea421]
2018-10-01 13:37:37,422 [http-bio-8083-exec-10] [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Initiating transaction rollback
2018-10-01 13:37:37,422 [http-bio-8083-exec-10] [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Rolling back JDBC transaction on Connection [com.mysql.jdbc.JDBC4Connection@356fcce9]
2018-10-01 13:37:37,426 [http-bio-8083-exec-10] [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Releasing JDBC Connection [com.mysql.jdbc.JDBC4Connection@356fcce9] after transaction