Erlang Central

Difference between revisions of "Distributing a Mnesia schema"

From ErlangCentral Wiki

Line 20:Line 20:
  
 
The directory can be overriden by using '''-mnesia dir '"/path/of/your/preference"' '''' when starting the node.
 
The directory can be overriden by using '''-mnesia dir '"/path/of/your/preference"' '''' when starting the node.
 +
 +
Are you using discless nodes? They involve a bit different solutions [[Distributing_a_Mnesia_schema/Discless_nodes]]
  
 
Read [[http://www.erlang.org/doc/doc-5.5/lib/mnesia-4.3.1/doc/html/Mnesia_chap5.html#5.2 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.
 
Read [[http://www.erlang.org/doc/doc-5.5/lib/mnesia-4.3.1/doc/html/Mnesia_chap5.html#5.2 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.

Revision as of 17:40, 18 July 2006

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.

Are you using discless nodes? They involve a bit different solutions Distributing_a_Mnesia_schema/Discless_nodes

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.