mnesia:start()启动数据库
本帖最后由 茶叶feng 于 2018-8-7 20:00 编辑mnesia:start()启动数据库的时候,必定会创建一个名为schema的表在erlang OTP并发编程实战中有个例子,代码如下:
%% 前面调用了mnesia:start().
add_extra_nodes() ->
case mnesia:change_config(extra_db_nodes, ) of
{ok, } ->
{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版本问题还是我代码存在问题,希望有大神指点
补充一下:
我把红色的那句注释了之后,代码就能正常运行了
Rs= mnesia:add_table_copy(schema, node(), ram_copies),
把 Rs 打印出来 看是什么嘛
dreamxyp 发表于 2018-8-31 20:47
把 Rs 打印出来 看是什么嘛
(test@localhost)1> mnesia:start().
ok
(test@localhost)2> mnesia:change_config(extra_db_nodes, ['contact1@localhost']).
{ok,}
(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 =
stopped db nodes = []
master node tables = []
remote = []
ram_copies =
disc_copies = []
disc_only_copies = []
[{contact1@localhost,ram_copies},
{contact2@localhost,ram_copies},
{test@localhost,ram_copies}] =
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: []
它会提示我已经创建了结构,的确他也有这个结构存在,不知道是不是默认会自动创建的 那就对了嘛
{atomic, ok} = mnesia:add_table_copy(schema, node(), ram_copies),%% 这里报错了 你这样写要是返回 不是 {atomic, ok} 不就 报错了 dreamxyp 发表于 2018-9-5 22:23
那就对了嘛
你这样写要是返回 不是 {atomic, ok} 不就 报错了
理论上这步应该不会出问题的吧?我就是想确保这步能成功执行,为啥跟书上讲的结果不同。。。 太旧了,书上的也有bug可能第一次 不会出错
页:
[1]