简明介绍
Dolt是一个开源的SQL数据库(版本控制数据库,version controlled database),可以对数据进行派生(fork)、克隆(clone)、分支(branch)、合并(merge)、推送(push)和拉取(pull)等操作,就像Git仓库一样具有版本控制功能。Dolt结合了版本控制系统(Git)与数据库(MySQL)的功能特性,所有Git命令在Dolt上都保持一致性,只不过Git针对文件,而Git针对表。
Dolt is Git for Data!Git versions files. Dolt versions tables. It's like Git and MySQL had a baby.
- Hosted Dolt:官方云服务
- DoltHub:官方数据库托管平台。DoltHub之于Dolt,GitHub之于Git
- DoltLab:自建数据库托管平台
Dolt有3种使用模式
- Git模式(Git for Data):通过命令行工具操作,类似git命令
- 数据库模式(Version Controlled Database):作为MySQL替代,通过MySQL客户端连接和操作
- 数据库副本模式(Versioned MySQL Replica):将Dolt配置为MySQL副本
环境安装
Dolt的安装相对简单,这里仅列举两种安装方式,其他安装方式参考官方文档。
二进制安装
Windows上下载MSI安装包双击运行或下载ZIP压缩包解压。
$ dolt version
dolt version 1.41.4
源码安装
Dolt使用Golang语言开发,可以在多种操作系统上克隆源码或下载源码包进行源码编译安装。为了方便测试兼容性,安装历史版本1.15.0。
# 模块代理
$ export GOPROXY="https://goproxy.cn,direct"
# 克隆源码
$ git clone https://github.com/dolthub/dolt.git
$ cd dolt
$ git checkout v1.15.0
$ cd go && go install ./cmd/dolt
$ $GOPATH/bin/dolt version
dolt version 1.15.0
快速使用
配置信息
$ dolt config --global --add user.name "leitiannet"
$ dolt config --global --add user.email "347341200@qq.com"
$ dolt config --list
user.name = leitiannet
user.email = 347341200@qq.com
$ cat ~/.dolt/config_global.json
{"user.email":"347341200@qq.com","user.name":"leitiannet"}
创建数据库
$ mkdir -p ~/dolt/database
$ cd ~/dolt/database
# 初始化数据库,自动创建.dolt隐藏目录
$ dolt init
Successfully initialized dolt data repository.
$ dolt log
创建数据表
# 通过导入CSV文件来创建表
$ vim employees.csv
id,first_name,last_name
0,Tim,Sehn
1,Brian,Hendriks
2,Aaron,Son
# 表名employees,主键id
$ dolt table import --create-table --pk id employees employees.csv
$ dolt status
On branch main
Untracked tables:
(use "dolt add <table>" to include in what will be committed)
new table: employees
# 通过SQL语句查看表
$ dolt sql -q "show tables"
+--------------------+
| Tables_in_database |
+--------------------+
| employees |
+--------------------+
$ dolt sql -q "describe employees"
+------------+----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| first_name | varchar(16383) | YES | | NULL | |
| last_name | varchar(16383) | YES | | NULL | |
+------------+----------------+------+-----+---------+-------+
$ dolt sql -q "select * from employees"
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
| 0 | Tim | Sehn |
| 1 | Brian | Hendriks |
| 2 | Aaron | Son |
+----+------------+-----------+
# 提交表
$ dolt add employees
$ dolt status
On branch main
Changes to be committed:
(use "dolt reset <table>..." to unstage)
new table: employees
$ dolt commit -m "Added new employees table containing the founders of DoltHub"
数据操作
# 修改数据
$ dolt sql -q "insert into employees values (3, 'Daylon', 'Wilkins')"
$ dolt sql -q "update employees set first_name='Timothy' where last_name like 'S%'"
$ dolt diff
diff --dolt a/employees b/employees
--- a/employees
+++ b/employees
+---+----+------------+-----------+
| | id | first_name | last_name |
+---+----+------------+-----------+
| < | 0 | Tim | Sehn |
| > | 0 | Timothy | Sehn |
| < | 2 | Aaron | Son |
| > | 2 | Timothy | Son |
| + | 3 | Daylon | Wilkins |
+---+----+------------+-----------+
# 回滚数据,即丢弃修改数据
$ dolt checkout employees
$ dolt diff
$ dolt sql -q "select * from employees"
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
| 0 | Tim | Sehn |
| 1 | Brian | Hendriks |
| 2 | Aaron | Son |
+----+------------+-----------+
# 修改数据
$ dolt sql -q "insert into employees values (3, 'Daylon', 'Wilkins')"
$ dolt sql -q "update employees set first_name='Timothy' where first_name='Tim'"
# 提交数据
$ dolt commit -am "Added Daylon. Make Tim Timothy."
$ dolt sql -q "select * from employees"
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
| 0 | Timothy | Sehn |
| 1 | Brian | Hendriks |
| 2 | Aaron | Son |
| 3 | Daylon | Wilkins |
+----+------------+-----------+
分支开发
# 查看分支
$ dolt branch
* main
# 创建分支并检出分支
$ dolt checkout -b modifications
$ dolt sql -q "insert into employees values (5,'Taylor', 'Bantle')"
$ dolt commit -am "Modifications on a branch"
$ dolt branch
main
* modifications
$ dolt sql -q "select * from employees"
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
| 0 | Timothy | Sehn |
| 1 | Brian | Hendriks |
| 2 | Aaron | Son |
| 3 | Daylon | Wilkins |
| 5 | Taylor | Bantle |
+----+------------+-----------+
合并分支
$ dolt checkout main
$ dolt diff modifications
diff --dolt a/employees b/employees
--- a/employees
+++ b/employees
+---+----+------------+-----------+
| | id | first_name | last_name |
+---+----+------------+-----------+
| - | 5 | Taylor | Bantle |
+---+----+------------+-----------+
# 合并分支
$ dolt merge modifications
# 删除分支
$ dolt branch -d modifications
远程共享
在DoltHub上注册账号,并创建一个远程数据库,建议远程数据库名称和本地数据库名称保持一致。
# 添加远程数据库
$ dolt remote add origin leitiannet/database
# 推送远程数据库
$ dolt push origin main
# 克隆远程数据库
# dolt clone leitiannet/database database-clone
# 拉取远程数据库
# dolt pull origin main
图形界面
(1)启动数据库服务
$ dolt sql-server
Starting server with Config HP="localhost:3306"|T="28800000"|R="false"|L="info"
(2)连接数据库
使用Navicat、TablePlus等MySQL客户端工具连接到数据库,可以进行创建数据库、创建表、增删改查等操作。
参考资料
dolthub/dolt: Dolt – Git for Data