erlangelixirmnesia

Mnesia in 2 separate IEx sessions on the same node


I'm learning Erlang's mnesia DB and trying to use it in Elixir app. According to documentation while creating a schema by default mnesia creates a directory with the name "Mnesia.#{node}". Here is the code for mnesia preparation:

:mnesia.create_schema([node])
:mnesia.start
:mnesia.create_table(User,
  [
    attributes: [:name, :email],
    disc_copies: [node]
  ]
)

When I'm starting 2 separate IEx sessions and make some transactional queries it seems they are only happened in one of the session. I infer this from :mnesia.info output.

How can I synchronise them, or is it an expected behaviour?


Solution

  • When one starts a shell without an explicitly specified node name, it’s started as nonode@nohost:

    $ iex
    Erlang/OTP 19 [erts-8.1] [source-e7be63d] ...
    Interactive Elixir (1.5.0-dev) ...
    
    iex(1)> node()
    :nonode@nohost
    

    Mnesia itself is node-aware. So, you need to explicitly remote-connect to the node, that is aware of running mnesia. Let’s start a named shell:

    $ iex --sname mnesia # the node name is arbitrary
    iex(mnesia@localhost)1>
    

    Now, let’s start the second shell and connect to the firstly started shell remotely. To do so, just start a shell, then press CtrlG to enter User switch command mode and type:

    User switch command
    --> r 'mnesia@localhost' 'Elixir.IEx'
    --> c
    

    Now you have two shells, connected to the same node, that handles a mnesia instance.

    More info in the IEx docs on remote shells.