Erlang Central

Distributing a Mnesia schema

Revision as of 16:34, 18 July 2006 by 213.171.204.166 (Talk)

Overview

This HOWTO describes how you make your tables replicate over several nodes.

Start several nodes

Start nodes and give them a name. This is how you start node foo@example.com:

erl -name foo@example.com

By this example, start a foo and a bar node on your domain (i.e. not example.com).

If you run the shells on different nodes you will have to make sure both nodes have the same cookie so they allow being connected. We suggest you avoid this to begin with.

Creating a schema

Right now, you should have two shells running. Our example node foo@example.com have a default disc storage paths set to the directory Mnesia.foo@example.com in the current directory.

The directory can be overriden by using -mnesia dir '"/path/of/your/preference"' ' when starting the node.

Read [5.2 Distribution and Fault Tolerance] in the mnesia user's guide. We learn that the following code creates a table already set to be replicated on several nodes.

 mnesia:create_table(foo,
                          [{ram_copies, [N1, N2]},
                           {attributes, record_info(fields, foo)}]).

In our situation N1 and N2 is 'foo@example.com' and 'bar@example.com'. This will create a table called foo which has all the fields of a record called foo. (What you see calledrecord_info(fields, Name) is a macro that expands to the names of each field of a record Name. Record Name must be bound at compile time as erlang records are 100% compile time.)

The function mneisia:add_table_copy can be used to add replicas of tables when you're already running.