I am looking into mnesia for the first time and I try to set it up in a cluster managed by libcluster.
I saw that you have to pass a list of cluster nodes to mnesia:create_schema before starting mnesia with mnesia:start. start needs to be called on every node while mnesia:create_schema and mnesia:create_table should only be called on one node. In all tutorials this is done by hand in the shell, but how do I do this automatically on startup in the right way? Currently my nodes are all running the exact same code, so all would create a schema and a table.
You don’t have to have the list of cluster nodes from the start. You only need one node of the cluster to start. mnesia can increase the cluster at runtime with minimal impact on transactions.
There are typically two ways of doing this:
create dbase upon installation.
create dbase upon startup
I do have a preference for (1). Upon installation we would run a escript or some other script to launch a quasi BEAM node to create the dbase. Elixir has a mix task which does something similar. You are creating a release aren’t you ?
The script would take a node name and detects if it is the first node running and creates the dbase if it is. If it is the second node running it modifies the configuration using mnesia:add_table_copy/3.
(2) basically starts your application and does the same as the script.
Sorry if I don’t get this straight away, I am still new to elixir and erlang. Do you suggest something like this? So the first node creates a schema with just itself and starts mnesia (if Node.list is empty). All subsequent nodes (if Node.list is non-empty) just start mnesia and ask Node 0 to copy over the tables and change the config.
I guess that SyncM library would fall into category (2) while you would do that in some kind of a mix install task?
Hi, there was an article recently on how to set a mnesia up (in Elixir), perhaps it could be of use as a reference. Just scroll down towards the middle for the relevant parts.