mnesia 数据转移



修改远程数据库的方法,先连接到远程数据库,再对里边的数据时行操作就可以了操作如下:
1.erl -setcookie 123 -name a@127.0.0.1 (远程也要和这个参数一样才可以)

2.mnesia:start().
3.mnesia:change_config(extra_db_nodes,['a@127.0.0.1', 'abc@someip']).             

4.(a@127.0.0.1)4> db:select(isp).
[{isp,"123","1","3"}]

creat mnesia 并带路径的方法是: erl -mnesia dir '"some dir"' some dir 一定存在,并要有操作权限才可以,否则不能用。

测试用:net_adm:ping(someip).
pong  是通的意思。
mnesia:create_schema([node()|nodes{}].  这个创建,如果远程已存在了就会失败,所在只适用于不存在的节点的情况。


以下是数据库的迁移工作:

我打算把一个项目里的mnesia数据库完封不动移动到另一个node上。实际上不是移动那个数据库目录再改个名这么简单的。

感激大神帮忙 Mnesia 数据库节点转移 -  ̄Lil Dash_ - 〓Meteora〓 2.1。现在总结一下。

假设有node:  aa@localhost 和 bb@localhost,   在aa@localhost上的mnesia有个user表,  我们要把aa@localhost上面的mnesia移动到bb@localhost。
怎样把mnesia数据库从一个node移动到另外一个node? 有两种方法。


方法一:

这个方法前提是bb@localhost上的数据库的schema是空的。
like this :

Erlang R15B (erts-5.9) [source] [64-bit] [smp:4:4] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.9  (abort with ^G)
(bb@localhost)1> mnesia:start().
ok
(bb@localhost)2> mnesia:info().
---> Processes holding locks <--- 
---> Processes waiting for locks <--- 
---> Participant transactions <--- 
---> Coordinator transactions <---
---> Uncertain transactions <--- 
---> Active tables <--- 
schema         : with 1        records occupying 415      words of mem
===> System info in version "4.6", debug level = none <===
opt_disc. Directory "/home/fzd_dash/mnesia/Mnesia.bb@localhost" is NOT used.
use fallback at restart = false
running db nodes   = [bb@localhost]
stopped db nodes   = [] 
master node tables = []
remote             = []
ram_copies         = [schema]
disc_copies        = []
disc_only_copies   = []
[{bb@localhost,ram_copies}] = [schema]
2 transactions committed, 0 aborted, 0 restarted, 0 logged to disc
0 held locks, 0 in queue; 0 local transactions, 0 remote
0 transactions waits for other nodes: []
ok

好,开波! Mnesia 数据库节点转移 -  ̄Lil Dash_ - 〓Meteora〓 2.1


以下命令在bb@localhost节点上操作。

让mnesia连接这两个node;
mnesia:change_config(extra_db_nodes,['aa@localhost', 'bb@localhost']).
之后mnesia:info().就会看到running_db_nodes 那里会有这两node了。同时显示remote有个user表。
like this:
running db nodes   = [aa@localhost,bb@localhost]
stopped db nodes   = [] 
master node tables = []
remote             = [user]
ram_copies         = [schema]
disc_copies        = []
disc_only_copies   = []
[{aa@localhost,disc_copies}] = [user]
[{aa@localhost,disc_copies},{bb@localhost,ram_copies}] = [schema]

然后就将bb@localhost的schema由原来的 ram_copies变成disc_copies类型。
mnesia:change_table_copy_type(schema,node(),disc_copies).

添加user表到bb@localhost,并且是disc_copies类型。
mnesia:add_table_copy(user,node(),disc_copies).

像这样将每个表添加过来。到此为止aa@localhost上的mnesia数据表都copy到bb@localhost了。如果你想保留aa@localhost那边的东西,这样就可以了。否则,继续:

停了aa@localhost那边的mnesia。
在bb@localhost 输入以下命令删除aa@localhost的schema
mnesia:del_table_copy(schema,'aa@localhost').

q().退出。进入aa@localhost的mnesia数据库文件夹 Mnesia.aa@localhost,将里面的schema.DAT 删掉。
然后再次去bb@localhost, 启动mnesia,可以看到running db nodes上没有了aa@localhost这个节点。
opt_disc. Directory "/home/fzd_dash/mnesia/Mnesia.bb@localhost" is used.
use fallback at restart = false
running db nodes   = [bb@localhost]
stopped db nodes   = [] 
master node tables = []
remote             = []
ram_copies         = []
disc_copies        = [schema,user]
disc_only_copies   = []
[{bb@localhost,disc_copies}] = [schema,user]
2 transactions committed, 0 aborted, 0 restarted, 0 logged to disc
0 held locks, 0 in queue; 0 local transactions, 0 remote
0 transactions waits for other nodes: []
 完事! Mnesia 数据库节点转移 -  ̄Lil Dash_ - 〓Meteora〓 2.1



方法二:

由于刚才在方法一时把aa@localhost的东西清了,现在反过来,假设 有node:  aa@localhost 和 bb@localhost,   在bb@localhost上的mnesia有个user表,  我们要把bb@localhost上面的mnesia移动到aa@localhost。

在bb@localhost上backup一个出来。新建一个backup目录。do:
mnesia:backup("backup/mnesia.bb.backup").

可以看到backup目录下多了个mnesia.bb.backup文件。

去aa@localhost操作。接下来就是读取backup文件了。但要注意,aa@localhost上面的表结构必须要与backup出来的那个数据库一模一样。
就是说,aa@localhost也要有个user表。

先create个user表。
rd(user,{id,name}).
mnesia:change_table_copy_type(schema,node(),disc_copies).
mnesia:create_table(user,[{disc_copies,[node()]}]).     

restore那个文件
mnesia:restore("backup/mnesia.bb.backup",[]).

完事! Mnesia 数据库节点转移 -  ̄Lil Dash_ - 〓Meteora〓 2.1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值