如有侵权请联系作者。
转载请注明出处。
帮助他人,记录自己。
标签
ArangoDB 2.7.5,ArangoDB,2.7.5,数据库,主从关系,master-slave,master,slave,主备。
修订历史
- 2020-04-21:首次编辑,编写“标签”、“修订历史”、“目录”、“说明”、“注意事项”、“资源下载地址”、“常用命令”、“操作步骤”、“帮助信息”等内容。
说明
最近工作中遇到一个需求,让配置一下 ArangoDB 数据库的主备关系,从 master 数据库自动同步数据到 slave 数据库中。如果是普通的配置也就罢了,关键公司数据库是“老古董”——ArangoDB 2.7.5。现在配置 3.x 版本很容易,所以记录一下。
如果想直接开始请跳转至操作步骤
- ArangoDB 版本:2.7.5。
- 启动方式:Docker 容器(或者 Windows 下载安装也可以)。
注意事项
概念:database 数据库,与关系型数据库中类似。collection 对应关系型数据库的 table。document 对应关系型数据库的一条数据。
ArangoDB 对大小写敏感,对双引号和单引号不敏感。
先在 slave 数据库执行一次完全同步(require("org/arangodb/replication").sync(相应参数)),然后再启动同步应用。
ArangoDB 2.7.5 版本中没有 require("org/arangodb/replication").setupReplication() 函数,2.8.9 的文档中有,说明这是在 2.7.5 到 2.8.9 之间增加的新功能。
配置 replication 的 applier 的属性时,如果在配置 endpoint 之前配置其他项,会报错。所以 endpoint 项要第一时间配置。
关于同步,master 上有一条数据 master-person 的数据状态为 1,slave 同步后 slave-person 的数据状态也为 1。如果 slave-person 做了修改(ArangoDB 2.7.5 版本无法设置 slave 数据库为只读模式),状态变为 2,master-person 的状态依然为 1,不会改变。当 master-person 修改后,数据状态变为 3,则 slave-person 数据状态也会同步为 3,slave-person 的状态 2 会丢失。如果 slave-person 再次修改,数据状态为 4,此时操作手动同步动作(require("org/arangodb/replication").sync(相应参数);),则 slave-person 数据状态再次变为 3,slave-person 的状态 4 会丢失。
关于数据状态,数据库上的一条记录 person,增删改其任意字段和删除该条记录都会修改它的数据状态。
启动自动同步应用时(require("org/arangodb/replication").applier.start("<lastLogTick>");),首次启动必须传入 lastLogTick。之后再启动,如果需要从特定位置开始则传入该位置的 Tick,否则无需传入(即 require("org/arangodb/replication").applier.start();)。
ArangoDB 2.8.9 官方文档摘录内容(可以跳过):
-
First Steps
- The ArangoDB Server
- The ArangoDB database server has two modes of operation: As a server, where it will answer to client requests and as an emergency console, in which you can access the database directly. The latter - as the name suggests - should only be used in case of an emergency, for example, a corrupted collection. Using the emergency console allows you to issue all commands normally available in actions and transactions. When starting the server in emergency console mode, the server cannot handle any client requests.
- The ArangoDB Server
-
Replication
- Introduction to Replication
- How the replication works
- Replication lag
- Replication configuration
- Components
- Replication Logger
- Purpose
- Checking the state
Configuration
- Replication Applier
- Purpose
- All-in-one setup
- Starting and Stopping
- Configuration
- Replication Logger
- Example Setup
- All-in-one setup
- Initial synchronization
- Initial synchronization from the ArangoShell
- Continuous synchronization
- Syncing Collections
- Replication Limitations
- at the moment it is assumed that only the replication applier executes write operations on a slave. ArangoDB currently does not prevent users from carrying out their own write operations on slaves, though this might lead to undefined behavior and the replication applier stopping.
- replication is only supported between the two ArangoDB servers running the same ArangoDB version. It is currently not possible to replicate between different ArangoDB versions.
- a replication applier cannot apply data from itself.
- Replication Overhead
- Introduction to Replication
-
Sever Configuration
- Authentication
- Authentication and Authorization
- ArangoDB only provides a very simple authentication interface and no authorization. We plan to add authorization features in later releases, which will allow the administrator to restrict access to collections and queries to certain users, given them either read or write access.
- Currently, you can only secure the access to ArangoDB in an all-or-nothing fashion.
- Authentication and Authorization
- Authentication
资源下载地址
所有ArangoDB数据库安装包:
https://download.arangodb.com/index.htmlWindowsArangoDB数据库安装包:
https://download.arangodb.com/Windows7/x86_64/index.html2.8.9文档下载地址:
https://download.arangodb.com/arangodb2/doc/ArangoDB_Manual_2.8.9.pdf
常用命令
- Docker 拉取镜像:
sudo docker pull arangodb:2.7.5
- Docker 启动容器:
sudo docker run -d -p 8529:8529 --name my_arangodb arangodb:2.7.5
- Docker 列出容器:
sudo docker container ls
- Docker 进入容器:
sudo docker exec -it containerId /bin/bash
- 连接指定 IP 和 port 数据库服务器上的 arango-test 数据库:
arangosh --server.endpoint tcp://masterIp:8529 --server.database arango-test --server.username name --server.password userPassword
- 退出数据库:
Ctrl + c / Ctrl + d / quit
- 列出所有数据库:
db._listDatabases();
- 使用指定数据库:
db._useDatabases("databaseName");
- 创建数据库:
db._createDatabases("databaseName");
- 删除数据库:
db._dropDatabases("databaseName");
- 获取一张表的数据条数:
db.collectionName.count();
- 获取一张表的所有数据:
db.collectionName.toArray();
- 启动数据库时允许 replication(2.2 版本后无需指定):
arangod --replication.active-failover=true
- 查看数据库 replication logger 状态:
require("org/arangodb/replication").logger.state();
- 查看数据库 replication applier 状态:
require("org/arangodb/replication").applier.state();
- 使用数据库 _system 并配置它的 replication 策略(2.7.5 版本不支持,2.8.9 版本支持):
db._useDatabase("_system");
require("org/arangodb/replication").setupReplication({endpoint: "tcp://master.domain.org:8529", username: "myuser", password: "mypasswd", verbose: false, includeSystem: false, incremental: true, autoResync: true });
- 手动开始/停止 replication 的 applier:
require("org/arangodb/replication").applier.start(<tick>);
require("org/arangodb/replication").applier.stop();
- 配置 replication 的 applier 的属性(如果不传参数则为获取属性)。配置时需先停掉 applier,运行时无法使新配置生效(下次开始 replication 时才会生效)。配置了 endpoint 后,applier.state() 和 applier.properties() 中都会同时添加 endpoint 信息。:
require("org/arangodb/replication").applier.properties({endpoint: "tcp://master.domain.org:8529", username: "root", password: "secret", verbose: false, includeSystem: true, autoStart: true, autoResync: true});
- 获取 replication 的 applier 的属性信息:
require("org/arangodb/replication").applier.properties();
- 执行一次完全同步(同步的方式),把 master 数据库同步到当前数据库中。注意:1. 这将删除当前数据库中所有表和数据(2.8.9 版本官方文档说会删除,2.7.5 实测没有删除)。2. 如果同步时只指定了 master 的地址,并没有指定 database,同步时则默认同步 master 中与当前使用的数据库同名的数据库进行同步。如果指定了 database,则按照指定的进行同步。
require("org/arangodb/replication").sync({endpoint: "tcp://master.domain.org:8529", database: "adam", username: "root", password: "secret, includeSystem: true });
- 异步的方式执行一次完全同步:
var replication = require("org/arangodb/replication");
var id = replication.sync({endpoint: "tcp://master.domain.org:8529", database: "adam", async: true});
print(replication.getSyncResult(id));
操作步骤
启动 ArangoDB 2.7.5 的 Docker 容器并进入。
连接容器内数据库 server。
arangosh
- 创建数据库并使用:
db._createDatabase("Construct");
db._useDatabase("Construct");
- 手动进行一次数据同步拉取全数据,并获取 lastLogTick(数据量过大建议使用异步方式)。
同步方式:
require("org/arangodb/replication").sync({endpoint: "tcp://master.domain.org:8529", database: "Construct"});
数据量大的话,使用同步方式容易卡死,可以使用异步方式拉取数据:
var replication = require("org/arangodb/replication");
var id = replication.sync({endpoint: "tcp://master.domain.org:8529", database: "Construct", async: true});
print(replication.getSyncResult(id));
拉取数据未完成返回 false,已完成则返回结果信息(关键是获取 lastLogTick),如下:
{
"collections" : [
{
"id" : "28243094",
"name" : "_users"
},
{
"id" : "29947030",
"name" : "_graphs"
},
...
{
"id" : "41612438",
"name" : "_system_users_users"
},
{
"id" : "57406614",
"name" : "WorkflowRelation"
},
{
"id" : "57799830",
"name" : "TemplateRef"
}
],
"lastLogTick" : "170563815548423"
}
- 查看数据库 applier 的 state 和 properties 信息:
require("org/arangodb/replication").applier.properties();
require("org/arangodb/replication").applier.state();
- 配置 applier。如果没有指定 database 名称,则默认使用当前使用的 database 名称:
require("org/arangodb/replication").applier.stop();
require("org/arangodb/replication").applier.properties({endpoint: "tcp://master.domain.org:8529", database: "Construct", verbose: false, includeSystem: true, autoStart: true, autoResync: true, autoResyncRetries: 10, adaptivePolling: true});
- 启动自动同步(参数是第四步中获取到的 lastLogTick 值):
require("org/arangodb/replication").applier.start("<lastLogTick>");