Accessing a Mnesia node from another Erlang shell while it is running
Asked Answered
B

1

6

What is the best practice to accessing a single running mnesia node from another Erlang shell to only view data in the tables?

I tried opening two shells and pointing them to the same mnesia directory location which I realized was a very bad idea after finding this in the documentation.

-mnesia dir Directory. The name of the directory where all Mnesia data is stored. The name of the directory must be unique for the current node. Two nodes may, under no circumstances, share the same Mnesia directory. The results are totally unpredictable.

Botulism answered 26/6, 2010 at 5:52 Comment(0)
B
1

I think that easiest way is joining to remote shell. Just start erl with -remsh Node parameter

$ erl -sname foo
Erlang R13B04 (erts-5.7.5) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.5  (abort with ^G)
(foo@hynek-notebook)1> 

Another terminal:

$ erl -sname bar -remsh 'foo@hynek-notebook'
Erlang R13B04 (erts-5.7.5) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.5  (abort with ^G)
(foo@hynek-notebook)1> 

Another option is use powerful job control capability of erl (Press ^G)

$ erl -sname bar
Erlang R13B04 (erts-5.7.5) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.5  (abort with ^G)
(bar@hynek-notebook)1> 
User switch command
 --> h
  c [nn]            - connect to job
  i [nn]            - interrupt job
  k [nn]            - kill job
  j                 - list all jobs
  s [shell]         - start local shell
  r [node [shell]]  - start remote shell
  q        - quit erlang
  ? | h             - this message
 --> r 'foo@hynek-notebook'
 --> j
   1  {shell,start,[init]}
   2* {'foo@hynek-notebook',shell,start,[]}
 --> c 
Eshell V5.7.5  (abort with ^G)
(foo@hynek-notebook)1> 
User switch command
 --> j
   1  {shell,start,[init]}
   2* {'foo@hynek-notebook',shell,start,[]}
 --> c 1

(bar@hynek-notebook)1>

Note that you have to press Enter to show shell prompt if you are switching back to existing one.

Bonitabonito answered 26/6, 2010 at 7:9 Comment(3)
What would be the best practice to access a running Mnesia database from two Erlang applications? Add a Node to the Mnesia cluster and access it that way, seems a little overkill?Botulism
@Peter: Then use rpc module if it is in same Erlang cluster or use lib_chan when you want communicate through the socket.Bonitabonito
I recommend using the -hidden flag when using a remote shell especially if you are dealing with distributed networks. E.g. erl -sname bar -remsh 'foo@hynek-notebook' -hiddenEmotional

© 2022 - 2024 — McMap. All rights reserved.