Is there ways to specify path to schema in cowboy app? Maybe it's possible to set in my_app.app.src or any config file?
2 Answers
The path to the mnesia directory has to be provided to erlang VM before mnesia application is started through application configuration parameters. In Mnesia tutorial, this is done with the -Application par val
VM arguments syntax.
What you call a cowboy application is probably an Erlang OTP release (built by relx as per cowboy tutorial). The solutions, quickly described in Cowboy issue #595, are as follows.
The choice between solutions really depends on style as well as some constraints. Any sufficiently complex release would use a configuration file, so it would be a good choice. vm.args
seems easier to deal with. Eventually, you might need to alter the start script (for example to run several nodes from a single deployment), and include some logic to define the mnesia directory.
Provide relx with a configuration file (sys_config option)
To do so, add the following term to relx.config
as documented.
{sys_config, "./config/sys.config"}.
sys.config
actually is a standard Erlang configuration file, also documented. Specifying mnesia dir is done by adding a section for mnesia application. If no other configuration is required, the file would be:
[{mnesia, [{dir, "/path/to/dir"}]}].
Get relx to pass arguments to the vm (vm_args option)
The vm.args
file is actually passed to the VM through -args_file
option. This is a simple text file with arguments.
You would add the following term to relx.config
as documented.
{vm_args, "./config/vm.args"}.
And put the following content in the vm.args
file:
-mnesia dir foo
Write your own start script
relx
actually creates a start script, which passes -config sys.config
and args_file vm.args
to the VM as required. You could modify this script or roll your own to actually pass the -mnesia dir
argument to the VM.

- 6,257
- 1
- 20
- 31
-
The answer was edited to detail all three solutions evoked on Github. – Paul Guyot May 13 '14 at 20:11
-
Thanks a lot for expanded answer! – Don Lino May 13 '14 at 20:43
-
In the first approach I had got error, but it fixed by changing `[{mnesia, {dir, "/path/to/dir"}}]` to `[{mnesia, [{dir, "/path/to/dir"}]}]` – Don Lino May 14 '14 at 11:21
-
Indeed, I fixed the example accordingly. – Paul Guyot May 14 '14 at 12:17
in case of vm.args, planit text -mnesia dir foo is invalid, please use format: -mnesia {dir,foo}

- 41
- 3
-
Your suggested format does not seems to work, at least under erlang OTP 23. The format is still `-mnesia dir foo`, more precisely: -mnesia dir `'"/path/to/your/data"'` – Ken Chen May 23 '20 at 10:12