Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant setup Mnesia

Tags:

erlang

mnesia

setup_mnesia(Name) ->
    ?VALUE(application:start(mnesia)),
    ?VALUE(mnesia:create_schema([node()|[Name]])),
    ?VALUE(mnesia:create_table(muppet, [
                        {attributes, record_info(fields, muppet)},
                        {disc_copies, [foo@kos13]}])),
    ?VALUE(mnesia:wait_for_tables([muppet], infinity)),
    ok.

result is

"application : start ( mnesia )" = ok

"mnesia : create_schema ( [ node ( ) | [ Name ] ] )" = {error, {foo@kos13, {already_exists, foo@kos13}}}

"mnesia : create_table ( muppet , [ { attributes , record_info ( fields , muppet ) } , { disc_copies , [ foo@kos13 ] } ] )" = {aborted, {bad_type, muppet, disc_copies, foo@kos13}}

EDITED, ADDED if rewrite both processes to call application:start after mnesia:create_schema it spits out "Cannot install fallback". In current directory appear two files - FALLBACK.BUP and foo@kos13131851070846165246847780.

like image 507
Yola Avatar asked Dec 07 '22 18:12

Yola


2 Answers

I do think that since you are trying to make a disc_copies table you need to set the location of the mnesia directory when starting the erlang node.

erl -mnesia dir db_dir

Edit: Ok I created my own example and I have a little more info for you, and I will be posting the commands I ran

Run you erlang node:

erl -sname mnesiatest -mnesia dir db

Start mnesia and check for the info

(mnesiatest@host)1> mnesia:start().
 ok
(mnesiatest@host)2> mnesia:info().
...
opt_disc. Directory "/home/user/test/db" is NOT used.
use fallback at restart = false
running db nodes   = [mnesiatest@host]
stopped db nodes   = []
master node tables = []
remote             = []
ram_copies         = [schema]
disc_copies        = []
disc_only_copies   = []
[{mnesiatest@host,ram_copies}] = [schema]

Check the third line and you see that "/home/user/test/db" is NOT used."

The solution:

Before starting mnesia you must create the schema, not the other way around. I know that doesn't seems logical, but is like config first and start second.

So you do:

 (mnesiatest@host)3> mnesia:stop().
  ...
 (mnesiatest@host)4> mnesia:create_schema([node()]).
 ok
 (mnesiatest@host)5> mnesia:start().
 ok
 (mnesiatest@host)6> mnesia:info().
  ...
  opt_disc. Directory "/home/user/test/db" is used. 
  ...

Now if you run the mnesia:create_table(...) you should have success. Again if you restart the node with the same dir, you will have the table created and with persistent data.

Hope this help.

like image 125
Nuno Freitas Avatar answered Dec 26 '22 16:12

Nuno Freitas


If your schema is a ram_copies type, mnesia cannot allow any other table to reside on disc, all tables you create in a ram_copies schema will also be in RAM.

Another thing, specify a mnesia dir like this:

erl -name MY_NODE_NAME -mnesia dir '"./Databases/MY_NODE_NAME_DATABASE"'

Where, ./Databases/MY_NODE_NAME_DATABASE must be an existing folder. After this, then you can create your schema and tables as documented.

Another thing you can do if your schema is in RAM is the function: mnesia:change_table_copy_type(Table_name, On_which_Node, To_new_type). After changing the schema type to disc_copies or disc_only_copies, then you can change the type of your tables as well to disc. If the schema is a disc_copies type as it normally will be, you can have tables of any nature you want, whether RAM, Disc or Disc_only_copies.

Its possible to get rid of unwanted schemas using mnesia:delete_schema/1, but be very careful with this method.

Any thing beyond this , please do refer to the mnesia users guide.

like image 24
Muzaaya Joshua Avatar answered Dec 26 '22 16:12

Muzaaya Joshua