mnesia 数据转移
2016-06-15 23:54
309 查看
修改远程数据库的方法,先连接到远程数据库,再对里边的数据时行操作就可以了操作如下:
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上。实际上不是移动那个数据库目录再改个名这么简单的。
感激大神帮忙
。现在总结一下。
假设有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
好,开波!
以下命令在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: []
完事!
方法二:
由于刚才在方法一时把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",[]).
完事!
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上。实际上不是移动那个数据库目录再改个名这么简单的。
感激大神帮忙
。现在总结一下。
假设有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
好,开波!
以下命令在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: []
完事!
方法二:
由于刚才在方法一时把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",[]).
完事!
相关文章推荐
- Erlang项目内存泄漏分析方法
- Erlang实现的一个Web服务器代码实例
- Erlang并发编程介绍
- Erlang的一些编程技巧分享
- Erlang程序设计(第2版)读书笔记:Erlang安装和基础语法
- Erlang中的模块与模式匹配介绍
- Erlang中的函数与流程控制介绍
- Erlang语法学习笔记:变量、原子、元组、列表、字符串
- Erlang中的Record详解
- Erlang初学:Erlang的一些特点和个人理解总结
- Erlang中的OTP简介
- Erlang中遍历取出某个位置的最大值代码
- Erlang中3种生成随机数的方法
- Erlang中的并发程序简介
- Erlang分布式节点中的注册进程使用实例
- Erlang中的注册进程使用实例
- Erlang中的映射组Map详细介绍
- CentOS 6.5源码安装Erlang教程
- Erlang的运算符(比较运算符,数值运算符,移位运算符,逻辑运算符)
- Erlang实现的百度云推送Android服务端实例