Search code examples
erlangmnesia

Erlang - Standard location of mnesia database


Is there a standard place to put the mnesia database within erlang? At the moment I am putting it in the priv directory.


Solution

  • By default, Mnesia will create the schema in a subdirectory called Mnesia.<node name> of the current directory of the emulator process. If that's not what you want, all you need to do is set Mnesia's dir application variable with something like

    application:set_env(mnesia, dir, "/path/to/db").
    

    As for where to place the database: that depends on your deployment scenario. Since application variables can also be set using release config files or command line arguments, you can delay that decision until you ship (or install on your own servers). For use in production, a standard directory like /var/lib/<your application>/mnesia-<node name>(on unix) should do.

    For playing around, i'd recommend using a dedicated directory under the code root (NOT the priv directory) and setting that location within your application's startup section. In my pet projects, i often use code such as

    Root = filename:absname_join(filename:dirname(?FILE), ".."),
    application:set_env(mnesia, dir, filename:join(Root, "db")).
    

    for exactly that purpose.