当启动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自动创建“测试”数据库呢?或者,如果遇到名称冲突,如何删除数据库?
发布于 2016-11-01 12:19:23
如果使用rethinkdb create
创建数据目录,则不会创建test
数据库。
例如,如果rethinkdb_data
不存在,它将在没有test
数据库的情况下创建它:
rethinkdb create
rethinkdb
这也会这样做,但是使用-d
来指定数据目录的位置:
rethinkdb create -d /var/lib/rethinkdb/data
rethinkdb -d /var/lib/rethinkdb/data
发布于 2016-10-28 03:41:12
经过一段时间的研究,我没有找到任何好的选项来阻止RethinkDB试图在启动时创建默认的test
数据库。只有当群集节点使用不同的数据目录时,才会出现上述问题。否则,他们将不会尝试创建额外的test
数据库,因为其他节点将简单地识别它已经存在(由启动的第一个节点创建)。
最后,我在后端软件中通过从test
数据库中的db_config
表枚举启动期间名为rethinkdb
的所有数据库来解决这个问题。
示例:
// 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);
});
});
});
https://stackoverflow.com/questions/40301430
复制