Search code examples
databaseerlangdistributedmnesia

Access distributed mnesia database from different nodes


I have a mnesia database containning different tables.

I want to be able to access the tables from different Linux terminals.

I have a function called add_record, which takes a few parameters, say name and id. I want to be able to call add_record on node1 and add record on node2 but I want to be updating the same table from different locations.

I read a few sources and the only thing i found out was that i should use net_adm:ping (node2). but somehow I cant access the data from the table.


Solution

  • i assume that you probably meant replicated table. Suppose you have your mnesia table on node: nodea@127.0.0.1 with -setcookie mycookie, whether its replicated on another node or not, if i want to access the records from another terminal, then i have to use erlang in this other terminal as well by creating a node, connecting this node to our node with the table (you ensure that they all have the same cookie), then you call a method on the remote node.

    Lets say you want to use a method add_record in module mydatabase.erl on the node nodea@127.0.0.1 which is having the mnesia table, the i open a linux terminal and i enter the following:

    $ erl -name remote@127.0.0.1 -setcookie mycookie
    Eshell V5.8.4  (abort with ^G)
    1> N = 'nodea@127.0.0.1'.
    'nodea@127.0.0.1'
    2> net_adm:ping(N).
    pong
    3> rpc:call(N,mydatabase,add_record,[RECORD]).
    {atomic,ok}
    4> 
    

    with this module (rpc), you can call any method on a remote node, if the two nodes are connected using the same cookie. start by calling this method on the remote node:

    rpc:call('nodea@127.0.0.1',mnesia,info,[]).
    
    It should display everything in your remote terminal. I suggest that probably, you first go through this lecture: Distributed Erlang Programming and then you will be able to see how replicated mnesia tables are managed. Go through that entire tutorial on that domain.