Erlang中文论坛
标题: mnesia:start()启动数据库 [打印本页]
作者: 茶叶feng    时间: 2018-8-7 19:42
标题: mnesia:start()启动数据库
 本帖最后由 茶叶feng 于 2018-8-7 20:00 编辑 
mnesia:start()启动数据库的时候,必定会创建一个名为schema的表在erlang OTP并发编程实战中有个例子,代码如下:
%% 前面调用了mnesia:start().
add_extra_nodes([Node | Tail]) ->
    case mnesia:change_config(extra_db_nodes, [Node]) of
        {ok, [Node]} ->
            {atomic, ok} = mnesia:add_table_copy(schema, node(), ram_copies),%% 这里报错了
            {atomic, ok} = mnesia:add_table_copy(key_to_pid, node(), ram_copies),
            Tables = mnesia:system_info(tables),
            mnesia:wait_for_tables(Tables, ?WAIT_FOR_TABLES);
        _ ->
            add_extra_nodes(Tail)
    end.
这样做的话,新加入分布式mnesia的节点本地数据库就回重复创建schema而报错,具体原因不知道是不是otp版本问题还是我代码存在问题,希望有大神指点
补充一下:
我把红色的那句注释了之后,代码就能正常运行了
作者: dreamxyp    时间: 2018-8-31 20:47
- Rs  = mnesia:add_table_copy(schema, node(), ram_copies),
 
 复制代码
把 Rs 打印出来 看是什么嘛
作者: 茶叶feng    时间: 2018-9-3 14:58
- (test@localhost)1> mnesia:start().
 - ok
 - (test@localhost)2> mnesia:change_config(extra_db_nodes, ['contact1@localhost']).
 - {ok,[contact1@localhost]}
 - (test@localhost)3> mnesia:add_table_copy(schema, node(), ram_copies).
 - {aborted,{already_exists,schema,test@localhost}}
 - (test@localhost)4> mnesia:system_info().
 - ===> System info in version "4.14.3", debug level = none <===
 - opt_disc. Directory "/data/opensource/my_try/Mnesia.test@localhost" is NOT used.
 - use fallback at restart = false
 - running db nodes   = [contact1@localhost,contact2@localhost,test@localhost]
 - stopped db nodes   = []
 - master node tables = []
 - remote             = []
 - ram_copies         = [schema]
 - disc_copies        = []
 - disc_only_copies   = []
 - [{contact1@localhost,ram_copies},
 -  {contact2@localhost,ram_copies},
 -  {test@localhost,ram_copies}] = [schema]
 - 4 transactions committed, 1 aborted, 0 restarted, 0 logged to disc
 - 0 held locks, 0 in queue; 0 local transactions, 0 remote
 - 0 transactions waits for other nodes: []
 
 复制代码
它会提示我已经创建了结构,的确他也有这个结构存在,不知道是不是默认会自动创建的
作者: dreamxyp    时间: 2018-9-5 22:23
那就对了嘛
   - {atomic, ok} = mnesia:add_table_copy(schema, node(), ram_copies),%% 这里报错了
 
 复制代码     你这样写  要是返回 不是 {atomic, ok}   不就 报错了  
作者: 茶叶feng    时间: 2018-9-6 09:51
理论上这步应该不会出问题的吧?我就是想确保这步能成功执行,为啥跟书上讲的结果不同。。。
作者: dreamxyp    时间: 2018-9-6 18:34
太旧了,书上的也有bug  可能第一次 不会出错
| 欢迎光临 Erlang中文论坛 (https://bbs.erldoc.com/) | 
Powered by Discuz! X3.3 |