Access distributed mnesia database from different nodes

社会主义新天地 提交于 2019-12-07 05:34:47

问题


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.


回答1:


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.

来源:https://stackoverflow.com/questions/9423832/access-distributed-mnesia-database-from-different-nodes

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