Mnesia directory is not created when running iex -S mix and :mnesia.info() shows: Mnesia.nonode@nohost" is NOT used

Hello,
I need some help concerning using mnesia database with a mix project, I have a new project hello which have just hello.ex module which is simply defined by

.... 
 def start(_type, _args) do
 :mnesia.create_schema([node])
 :mnesia.start()
 {:ok, self()}
 end
.... 

My issue is that the mnesia directory is not created at all when running iex -S mix and :mnesia.info() shows

 ....
 Directory "/elixir/test/Mnesia.nonode@nohost" is NOT used
 ....

I have long experience with mnesia in Erlang but Iam new to Elixir and I don’t know where is the problem, and even trying to customise the path by creating config/config.exs and defining config :mnesia, dir 'path/to/directory' didn’t change anything, so any help please ?
Thank you in advance.
NOTE:
When using :mnesia.create_schema([node()]) in the Elixir shell, the directory created and used properly

Are you calling the function on the iex session though?!
From your description I would assume something like Hello.start(nil, nil)?

1 Like

What is missing in your description is: what is the application being started and is that being done correctly? It might be missing or assumed in your description. If the application configuration is missing then that likely explains the issue. Otherwise, I expect those items to be key to understanding what you’re seeing and would need to see them.

For the start/2 function to be called the Hello module needs to be configured as the application starting point. This is described in: Application — Elixir v1.12.3

The key things here are that the Hello module should use Application and that your mix.exs should configure that module to be a started application… something like:

def application do
  [mod: {Hello, []}]
end

If those elements are missing, then iex -S mix won’t run start/2.

1 Like

Thank you @crova and @sbuttgereit for your replies, I did everything as expected and of course I have set the project to start by calling Hello.start/2 and it had normal start, I think I found the bug :
I remember few days ago, I did a similar project and when using iex -S mix I had a big WARNING said that I should include :mnesia and every Erlang/OTP application which I will use in my project, in extra_applications field in the mix.exs file and this is the BUG, when including it, I can use mnesia normal but the directory will never been created, in fact I don’t know where the data is stored, and typing :mnesia. create_schema([node()]) results in {:error, {:already_exists....}} and I confirmed that the database is not created maybe in another place by running find /-name Mnesia.nonode@nohost in my unix shell which didn’t find it everywhere.
Once I deleted it from extra_applications I got the Warning but the directory is created and everything work perfectly, so I think this is a mix bug which maybe someone can reported.

1 Like

duplicate

create_schema must be called when mnesia is stopped. By adding mnesia to extra applications it is started automatically. When that happens it will create an in-memory schema.

Some alternatives.

  1. create the mnesia schema before you start the application (create schema is a one-time operation so it could be done in a task like normal SQL schema migration.
  2. Check if the schema exist and if not stop mnesia, create the schema and start it as part of some startup logic. or perhaps add it to included_applications instead of extra_applications and start it manually after schema is created.
  3. Check if schema exists and if not change the in memory schema to :disc_copies using :mnesia.change_table_copy_type/3

#1 would be the most robust I would say.

2 Likes

Thanks @cmkarlsson, but even ignoring the Warning will does the perfect behaviour

I would also recommend you read the user guide Erlang -- Mnesia User's Guide. It explains in great detail how mnesia works