How to rename the Node running a mnesia Database

蹲街弑〆低调 提交于 2019-11-28 20:45:55
archaelus

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

Workaround is migration. Just start mnesia cluster and migrate all your tables and schema to other node. Than remove from original and forgot.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!