问题描述
我在machine1上创建了一个Mnesia数据库/ Schema。该节点被命名为mypl @ machine1。然后我将所有文件移动到machine2,因为machine1中断了。只要代码运行的名称为mypl @ machine1,一切正常运行。显然这是有点confugsing,因为它现在运行在machine2。如果我启动Erlang的节点名称mypl @ machine2,Mnesia数据库显示为空。 / p>
如何将Mnesia数据库中的节点从machine1重命名为machine2?
我不认为这可以在单个节点(任何人)上在线完成,但除了运行两个节点和添加表副本之外,还可以通过备份/恢复进行。在部分6.9.1中,您将找到一些使用的代码mnesia:traverse_backup来更改mnesia备份文件中的模式表(如下所示)中的节点名称。您应该可以使用的模块名称是 mnesia_backup
。
使用此代码,您需要:
%% On mypl @ machine1
mnesia:backup(/ path / to / mnesia.backup)。
change_node_name(mnesia_backup,mypl @ machine1,mypl @ machine2,
/path/to/mnesia.backup,/path/to/new.mnesia.backup)。
%% On mypl @ machine2
mnesia:restore(/ path / to / new.mnesia.backup,[])。
我不知道您是否需要先在 mypl上创建架构@ machine2
。
用户指南中的更改节点名称代码:
change_node_name(Mod,From,To,Source,Target) - >
Switch =
fun(Node)当Node == From - >至;
(Node)当Node == To - > throw({error,already_exists});
(Node) - > Node
end,
Convert =
fun({schema,db_nodes,Nodes},Acc) - >
{[{schema,db_nodes,lists:map(Switch,Nodes)}],Acc};
({schema,version,Version},Acc) - >
{[{schema,version,Version}],Acc};
({schema,cookie,Cookie},Acc) - >
{[{schema,cookie,Cookie}],Acc};
({schema,Tab,CreateList},Acc) - >
Keys = [ram_copies,disc_copies,disc_only_copies],
OptSwitch =
fun({Key,Val}) - >
案例列表:
的成员(键,键)true - > {Key,lists:map(Switch,Val)};
false-> {Key,Val}
end
end,
{[{schema,Tab,lists:map(OptSwitch,CreateList)}],Acc};
(Other,Acc) - >
{[Other],Acc}
end,
mnesia:traverse_backup(Source,Mod,Target,Mod,Convert,switched)。
I created a Mnesia database / Schema on machine1. The node was named mypl@machine1. I then moved all files to machine2, because machine1 broke down. Everything runs fine as long as the code is running with the name "mypl@machine1". Obviously this is somewhat confugsing, because it is now running on machine2.
If I start Erlang with the node name "mypl@machine2" the Mnesia Database appears being empty.
How do I rename the node in a Mnesia Database from machine1 to machine2?
I don't think this can be done online on a single node(anyone?), but it is possible to do via a backup/restore in addition to running two nodes and adding table copies. In the Mnesia User's guide section 6.9.1 you'll find some code that uses mnesia:traverse_backup to alter the node names in the schema table (Shown below) in an mnesia backup file. The module name you should probably use is mnesia_backup
.
With this code you'll need to:
%% On mypl@machine1
mnesia:backup("/path/to/mnesia.backup").
change_node_name(mnesia_backup, mypl@machine1, mypl@machine2,
"/path/to/mnesia.backup", "/path/to/new.mnesia.backup").
%% On mypl@machine2
mnesia:restore("/path/to/new.mnesia.backup", []).
I'm not sure if you need to create the schema first on mypl@machine2
.
The change node name code from the user's guide:
change_node_name(Mod, From, To, Source, Target) ->
Switch =
fun(Node) when Node == From -> To;
(Node) when Node == To -> throw({error, already_exists});
(Node) -> Node
end,
Convert =
fun({schema, db_nodes, Nodes}, Acc) ->
{[{schema, db_nodes, lists:map(Switch,Nodes)}], Acc};
({schema, version, Version}, Acc) ->
{[{schema, version, Version}], Acc};
({schema, cookie, Cookie}, Acc) ->
{[{schema, cookie, Cookie}], Acc};
({schema, Tab, CreateList}, Acc) ->
Keys = [ram_copies, disc_copies, disc_only_copies],
OptSwitch =
fun({Key, Val}) ->
case lists:member(Key, Keys) of
true -> {Key, lists:map(Switch, Val)};
false-> {Key, Val}
end
end,
{[{schema, Tab, lists:map(OptSwitch, CreateList)}], Acc};
(Other, Acc) ->
{[Other], Acc}
end,
mnesia:traverse_backup(Source, Mod, Target, Mod, Convert, switched).
这篇关于如何重命名运行mnesia数据库的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!