想在MochiWeb中用mnesia,并且让mnesia按集群部署。
Erlang mochiweb 部署
环境:
2台centos系统的机器,在同一网段。
设置host
ip:192.168.2.126
more /etc/sysconfig/network
NETWORKING=yes HOSTNAME=centos-126 GATEWAY=192.168.0.1
more /etc/hosts
127.0.0.1 centos-126 localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.2.127 www.centos127.com
注意:192.168.2.127 www.centos127.com,要保证126这台机器能够通过域名,ping通127机器。
ip:192.168.2.127
more /etc/sysconfig/network
NETWORKING=yes HOSTNAME=centos-127 GATEWAY=192.168.0.1
more /etc/hosts
127.0.0.1 centos-127 localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.2.126 www.centos126.com
注意:192.168.2.126 www.centos126.com,要保证127这台机器能够通过域名,ping通126机器。
设置完host后,保证2台机器能通过,hostname相互ping通。
测试Erlang能否net_adm:ping通
启动Erlang虚拟机:
[root@centos-126 /]# erl -name "a@www.centos-126.com" -setcookie abc [root@centos-127 /]# erl -name "b@www.centos127.com" -setcookie abc
验证net_adm:ping
(b@www.centos127.com)1> net_adm:ping(a@www.centos126.com). pong (a@www.centos126.com)1> net_adm:ping(b@www.centos127.com). pong
说明Erlang能够彼此ping通。
接下来配置MochiWeb:
在Programming Erlang书中,说到
引用
We can also use -sname on two different machines when they are on the same subnet.
所以我使用sname进行配置。
首先还是更改hostname
ip:192.168.2.126
vim /etc/sysconfig/network
NETWORKING=yes HOSTNAME=centos126 GATEWAY=192.168.0.1
vim /etc/hosts
127.0.0.1 centos126 192.168.2.127 centos127
hostname centos126
让新设置的hostname生效
ip:192.168.2.127
vim /etc/sysconfig/network
NETWORKING=yes HOSTNAME=centos127 GATEWAY=192.168.0.1
vim /etc/hosts
127.0.0.1 centos127 192.168.2.126 centos126
hostname centos127
让新设置的hostname生效
互相ping通。
设置MochiWeb的start-dev.sh
126,127上的start-dev.sh 修改如下:
#!/bin/sh # NOTE: mustache templates need \ because they are not awesome. exec erl +P 500000 -pa ebin edit deps/*/ebin -boot start_sasl \ -setcookie abc \ -sname im_dev \ -s im \ -s reloader \ -mnesia dir '"/ide/erlang/im/db/online"'
设置mnesia的初始化方法,设置为两点同步,仅内存模式。
online_init.erl
-record(im_monitor, {pid, user_name } ). %% save user_name, pid, type is bag -record(im_chat, {user_name, pid } ). init() -> mnesia:create_table(im_monitor, [{ram_copies, [im_dev@centos126,im_dev@centos127]}, {type, set}, {attributes, record_info(fields, im_monitor)}]), mnesia:create_table(im_chat, [{ram_copies, [im_dev@centos126,im_dev@centos127]}, {type, bag}, {attributes, record_info(fields, im_chat)}]).
验证:详细请参考http://amornio.iteye.com/blog/1546536
126,127上
1) 分别输入./start-dev.sh,正常启动
2) 分别net_adm:ping()通
3) 第一次启动时候,在其中一台输入 mnesia:create_schema([im_dev@centos126,im_dev@centos127]).
4) 分别输入,mnesia:start().
5) 其中一台,输入online_init:init().返回{atomic, ok}.
6) 分别输入mnesia:info().数据表是否创建成功。
好了,现在完成了。