ArangoDB 数据库配置主从关系

如有侵权请联系作者。
转载请注明出处。
帮助他人,记录自己。

标签

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.
  • 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
    • 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
  • 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.

资源下载地址

常用命令

  • 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));

操作步骤

  1. 启动 ArangoDB 2.7.5 的 Docker 容器并进入。

  2. 连接容器内数据库 server。

arangosh
  1. 创建数据库并使用:
db._createDatabase("Construct");
db._useDatabase("Construct");
  1. 手动进行一次数据同步拉取全数据,并获取 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"
}
  1. 查看数据库 applier 的 state 和 properties 信息:
require("org/arangodb/replication").applier.properties();
require("org/arangodb/replication").applier.state();
  1. 配置 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});
  1. 启动自动同步(参数是第四步中获取到的 lastLogTick 值):
require("org/arangodb/replication").applier.start("<lastLogTick>");
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,012评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,589评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,819评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,652评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,954评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,381评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,687评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,404评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,082评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,355评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,880评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,249评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,864评论 3 232
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,007评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,760评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,394评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,281评论 2 259

推荐阅读更多精彩内容