Q: Adding nodes to an Mnesia db - the best way?

Niclas Eklund <>
Fri Jul 13 15:27:06 CEST 2001


Hello!

You do not mention that you start the new node (the one you intend to add)
using extra_db_nodes:

shell> erl -mnesia extra_db_nodes NodeList -name/-sname XX

Might this be your problem?!

/Nick

Example (you can run this from any node in the mnesia-domain):

-define(MyTableList, [person, company, ..]).

add_node(Node, StorageType) when atom(Node), atom(StorageType)  ->
    case rpc:call(Node, mnesia, system_info, [is_running]) of
	{badrpc, Reason} ->
            %% To avoid users doing something they shouldn't ;-)
	    exit("Target node not available");
	yes ->
            copy_tables(?MyTableList, Node, StorageType);
	no ->
	    exit("Mnesia not started on target node")
    end.

copy_tables([], _, _) ->
    ok;
copy_tables([T|Trest], Node, StorageType) ->
    case mnesia:add_table_copy(T, Node, StorageType) of
	{atomic, ok} ->
	    copy_tables(Trest, Node, StorageType);
	_ ->
	    exit("Unable to copy table(s).")
    end.




On Fri, 13 Jul 2001, Bruce Fitzsimons wrote:

> Hi,
> 
> I have been playing with adding and deleting nodes on an Mnesia db. Every
> single time I manage to stuff it up in some way and have Mnesia dump on me.
> 
> All I want to be able to do is to extend the schema onto a remote db, and
> then add the tables to the remote db.
> 
> There appears to be some omissions from the current docs about the schema -
> the R7B-3 code explicitly limits mnesia:add_table_copy to ram_copies for the
> schema table. I'm not sure why. Any explanation would be welcome, I am
> presuming that there is some dependency issue that caused this design.
> 
> Anyway, the code:
> 
> rpc:call(Node, mnesia, stop, []),
> mnesia:add_table_copy(schema, Node, ram_copies),
> io:format("Created Schema (ram copy)"),
>     rpc:call(Node, mnesia, start, []),
> %% Wait for Mnesia to start up
> receive
>      _ ->
>          true
> after 3000 ->
>      ok
> end,
> io:format("Changing schema to disc_copies~n~n"),
> mnesia:change_table_copy_type(schema, Node, disc_copies),
> io:format("schema changed - adding tables~n"),
> mnesia:add_table_copy(worker, Node, disc_copies).
> 
> I have a local node with everything and a remote node with nothing. There is
> another node, but that was turned off.
> 
> I have many weird errors with this code (eg "table schema does not exist"
> from the change_table_copy_type), but the final situation I got myself into
> was the local node believing that the remote node was sharing the schema,
> but the remote node thought that it just had a ram_copies schema of its own.
> Very weird. I could not delete the table copy because the remote node
> "wasn't active", and I couldn't get the remote node to realise its place in
> the world and get the schema copy. A third node that was previously sharing
> the schema crashed when I started it because the schemas could not be
> merged.
> 
> I ended up removing all copies of the database and starting afresh
> (create_schema(all_three_nodes)). Its not too big a deal at this stage, but
> it would be nice to know the definitive way of doing this - I am envisaging
> building a nice screen or two that will let me add/delete nodes at will.
> 
> Any hints or suggestions about the correct way to add and remove new nodes
> would be appreciated.
> 
> Cheers,
> Bruce
> 




More information about the erlang-questions mailing list