如何防止RethinkDB创建测试数据库

How to prevent RethinkDB from creating test database

当您启动 rethinkdb 实例时,它会自动创建一个名为 'test' 的数据库。当您 运行 多个实例并使用 rethinkdb proxy 对它们进行集群时,这会导致问题:

Database name conflict: test is the name of more than one database

如果您尝试删除数据库,即使用

r.dbDrop('test').run(conn, function(result) {
   console.log(result) // Will result in error
});

这将产生以下错误:

ReqlOpFailedError: Database 'test' is ambiguous; there are multiple databases with that name in r.dbDrop("test")

那么如何防止RethinkDB自动创建'test'数据库呢?或者,如果 运行 发生名称冲突,如何删除数据库?

经过一段时间的挖掘,我没有找到任何好的选项来阻止 RethinkDB 在启动时尝试创建默认的 test 数据库。只有当集群节点使用不同的数据目录时,才会出现上述问题。否则他们不会尝试创建额外的 test 数据库,因为其他节点会简单地识别它已经存在(由启动的第一个节点创建)。

我最终在后端软件中解决了这个问题,方法是在启动期间从 rethinkdb 数据库中的 db_config table 枚举所有名为 test 的数据库。

示例:

// Read from the database 'rethinkdb' that holds information about other databases
r.db("rethinkdb")

// Table db_config contains database id, name information
 .table("db_config")

// Filter by name 'test'
 .filter({name: "test"})
 .run(conn, function(err, cursor) {

     // Get results from cursor by id and rename
     cursor.each(function(err, result){
         r.db("rethinkdb")
          .get(result.id)
          .update({name: "other_name"})
          .run(conn, function(err, result) {
              console.log(result.replaced);
           });
     });
});

如果使用 rethinkdb create 创建数据目录,将不会创建 test 数据库。

例如,如果 rethinkdb_data 不存在,这将在没有 test 数据库的情况下创建它:

rethinkdb create
rethinkdb

这将做同样的事情,但使用 -d 指定数据目录的位置:

rethinkdb create -d /var/lib/rethinkdb/data
rethinkdb -d /var/lib/rethinkdb/data