TIDB的選擇與體驗

一、方案選擇

當數據庫表中的記錄量達到百萬級別以上,那麼就必須考慮分庫分表或者使用分佈式數據庫。分庫分表是比較常見的方案,一種是在客戶端直接實現,比較出名的有sharding-jdbc,另一種是使用代理方式,常用的有mycat。分佈式數據庫有最新的tidb,也有阿里的oceandb。

1.1 對比

  1. sharding-jdbc:最新版的叫做sharding-sphere,持續在開發和維護,由來自京東的技術大牛主導,已經有廣泛的應用。分片配置靈活,都在客戶端代碼直接實現,性能損失少,但是維護會比較麻煩,需要逐個項目的修改配置。不能進行跨庫連接,代碼需要進行改造。在擴展數據庫服務器時需要考慮一致性哈希問題,或者採用分片鍵局部取模方式,也難免要進行部分的數據遷移。
  2. mycat:由來自阿里的技術大牛主導,也有較多的實際應用。分片不要在客戶端實現,直接由代理服務器實現,客戶端也有進行代碼的改造。代理服務器可以管理數據,dba維護比較方便。但是代理服務器有性能瓶頸問題,同樣也存在擴展麻煩的問題。
  3. tidb:直接可以將mysql原始數據庫導入tidb,除了函數和觸發器等一些特殊的對象。支持集羣和擴展,解決了上面兩種方案的真正痛點。但是該項目教新,還沒有經過大量的生產環境檢驗,可能會存在一定的風險。

1.2 小結

所以3種方案各有優缺點,要根據實際情況選擇,1和2相對比較成熟,如果技術團隊人員夠多,用成熟的方案沒有風險,但是對於創業型的公司人才比較缺乏,選擇tidb升級系統相對比較輕鬆,靈活性強。

二、tidb安裝體驗

2.1 ubuntu14.04安裝tidb

虛擬機上安裝的話,建議內存2G以上,推薦4G,硬盤20G以上。

  1. 安裝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
  2. 安裝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的批量限制,也可以試試官方的導入工具。
navicat lite

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
除了函數和觸發器一般的表結構和記錄都能導入,導入速度會比較慢需要耐心等待,直到導入完畢!
在這裏插入圖片描述
導入完畢後跟普通的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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章