mongodb 复制集 2


一,节点类型:

a) 主节点:支持读写

b) 从节点:支持读(需设置)

二, 节点环境

主节点:192.168.129.47
mongodb安装目录:/home/en/tool/mongodb-linux-x86_64-2.6.9/
mongodb数据目录:/home/en/njqgd_db
mongodb日志目录:/home/en/njqgd_log
mongodb密匙目录:/home/en/njqgd_key

node.js 程序目录:/home/en/njqgd_node

java  程序目录:/home/en/njqgd_tomcat/apache-tomcat-7.0.61

从节点:192.168.129.48
mongodb安装目录:/home/en/tool/mongodb-linux-x86_64-2.6.9/
mongodb数据目录:/njqgd_db/db
mongodb日志目录:/njqgd_db/log
mongodb密匙目录:/njqgd_db/key

清空从节点数据目录:rm -rf /njqgd_db/db/* (请确保从节点目录为空,若不为空执行此命令)

1.主节点操作:

    1) cd /home/en/tool/mongodb-linux-x86_64-2.6.9/bin

    2) sudo echo "njqgd db 1" > /home/en/njqgd_key/key

    3) sudo chmod 600 /home/en/njqgd_key/key

    4) sudo ./mongod --dbpath=/home/en/njqgd_db/ --logpath=/home/en/njqgd_log/mongodb.log --port 27017  --replSet njqgd --keyFile /home/en/njqgd_key/key --maxConns=2000 --fork --logappend (此操作需等待约半分钟)

    

    出现如下内容,说明第4步操作成功:

    about to fork child process, waiting until server is ready for connections.

    forked process: XXXX

    child process started successfully, parent exiting



    5) 现在开始进行从节点操作,从节点操作完毕,再进行下一步.

    6) cd /home/en/tool/mongodb-linux-x86_64-2.6.9/bin

    7) sudo ./mongo

    8) use admin

    9) db.runCommand({"replSetInitiate" : {"_id" : "njqgd","members" : [{"_id" : 0,"host" : "192.168.129.47:27017"},{"_id" : 1,"host" : "192.168.129.48 :27017"}]}});

    出现如下内容,说明第9步操作成功:

    {

        "info" : "Config now saved locally.  Should come online in about a minute.",

        "ok" : 1

    }

    10) 所有操作完毕.

2.从节点操作:

    1) cd /home/en/tool/mongodb-linux-x86_64-2.6.9/bin

    2) sudo echo "njqgd db 1" > /njqgd_db/key/key (此处 echo 命令后面 双引号内的内容请和主节点保持一致)

    3) sudo chmod 600 /njqgd_db/key/key

    4) sudo ./mongod --dbpath=/njqgd_db/db/ --logpath=/njqgd_db/log/mongodb.log --port 27017  --replSet njqgd/192.168.129.47:27017 --keyFile /njqgd_db/key/key --maxConns=2000 --fork --logappend



    出现如下内容,说明第4步操作成功:

    about to fork child process, waiting until server is ready for connections.

    forked process: XXXX

    child process started successfully, parent exiting



    5) 从节点操作完成,请继续主节点操作第6步.

3. 添加数据库操作用户

    1) cd /home/en/tool/mongodb-linux-x86_64-2.6.9/bin

    2) sudo ./mongo

    3) use admin

    4) db.addUser('njqgd','njqgd');

    5) use wqdwxt_njqgd

    6) db.addUser('njqgd','njqgd');

    7) db.test.insert({'name':'test'})

 4. 节点主从状态切换

cfg = rs.conf()

cfg.members[0].priority = 2

cfg.members[1].priority = 1

cfg.members[2].priority = 0.5

rs.reconfig(cfg)

节点的 priority 值越大,切换时的优先级越高.设置好从节点的 priority 值后,调用一次 rs.reconfig 操作会导致当前主库中断,因为

需要重新执行主节点竞选,大概几十秒后,新的主节点产生#查看复制集状态.

rs.status()
rs.isMaster()
rs.conf()

 

#查看从库状态
db.printSlaveReplicationInfo()

 

#设置从库可查询
db.getMongo().setSlaveOk()
rs.setSlaveOk()

三. 开启各项服务.

1.开启mongodb

1) sudo -s (输入密码)
2) cd /home/en/tool/mongodb-linux-x86_64-2.6.9/bin
3) ./mongod --dbpath=/home/en/njqgd_db/ --logpath=/home/en/njqgd_log/mongodb.log --port 27017 --replSet njqgd --keyFile /home/en/njqgd_key/key --maxConns=2000 --fork --logappend
出现如下类似内容说明成功 about to fork child process, waiting until server is ready for connections. forked process: XXXX child process started successfully, parent exiting

 

1.1 mongodb手动切换主从

1) sudo -s (输入密码)

2) cd /home/en/tool/mongodb-linux-x86_64-2.6.9/bin

3) ./mongo

4) rs.isMaster();

如果出现如下内容

{

        "setName" : "njqgd",

        "setVersion" : 2,

        "ismaster" : true,

        "secondary" : false,

        "hosts" : [

                "192.168.129.47:27017",

                "192.168.129.48 :27017"

        ],

        "primary" : "192.168.129.47:27017",

        "me" : "192.168.129.48:27017",

        "maxBsonObjectSize" : 16777216,

        "maxMessageSizeBytes" : 48000000,

        "maxWriteBatchSize" : 1000,

        "localTime" : ISODate("2015-05-09T02:20:08.633Z"),

        "maxWireVersion" : 2,

        "minWireVersion" : 0,

        "ok" : 1

}

 ismaster为true 当前节点即为主节点(就不用进行下面的操作)



5) use admin

6) db.auth('njqgd','njqgd');

7) rs.status();

{

        "set" : "njqgd",

        "date" : ISODate("2015-05-09T02:28:35Z"),

        "myState" : 1,

        "members" : [

                {

                        "_id" : 0,

                        "name" : "192.168.129.47:27017",

                        "health" : 1,

                        "state" : 1,

                        "stateStr" : "PRIMARY",

                        "uptime" : 54073,

                        "optime" : Timestamp(1431138513, 2),

                        "optimeDate" : ISODate("2015-05-09T02:28:33Z"),

                        "electionTime" : Timestamp(1431084460, 1),

                        "electionDate" : ISODate("2015-05-08T11:27:40Z"),

                        "self" : true

                },

                {

                        "_id" : 1,

                        "name" : "192.168.129.48:27017",

                        "health" : 1,

                        "state" : 2,

                        "stateStr" : "SECONDARY",

                        "uptime" : 54059,

                        "optime" : Timestamp(1431138512, 3),

                        "optimeDate" : ISODate("2015-05-09T02:28:32Z"),

                        "lastHeartbeat" : ISODate("2015-05-09T02:28:33Z"),

                        "lastHeartbeatRecv" : ISODate("2015-05-09T02:28:33Z"),

                        "pingMs" : 1,

                        "syncingTo" : "222.186.43.150:27017"

                }

        ],

        "ok" : 1

}

要记住 节点顺序 以便下一步操作

6) cfg = rs.conf() <回车>

 cfg.members[0].priority = 2 <回车> (此处中括号中是节点顺序.从0开始)

cfg.members[1].priority = 1 <回车>

rs.reconfig(cfg) <回车>

节点的 priority 值越大,切换时的优先级越高.设置好从节点的 priority 值后,调用一次 rs.reconfig 操作会导致当前主库中断,因为



需要重新执行主节点竞选,大概几十秒后,新的主节点产生#查看复制集状态.

 

 

2.开启node.js

1) sudo -s (输入密码)

2) forever stopall(开发人员,在执行此命令之前 请使用forever list 记录日志位置)

3) forever start -c "npm start" /home/en/njqgd_node

 

3.开启tomcat

1) sudo -s(输入密码)

2) source /root/.profile

3) cd /home/en/njqgd_tomcat/apache-tomcat-7.0.61/bin

4) sh shutdown.sh

5) sh startup.sh

 

注:

mongodb 2.4 以后使用了新的权限系统

创建 最高权限用户(创建完毕后 需要重新登录):

use admin

db.createUser(
{
user:"njqgd",
pwd:"njqgd",
roles:["root"]
}
)

登陆:

mongo -u njqgd -p njqgd

创建普通用户:

use wqdwxt_njqgd

db.createUser(
{
user:"njqgd",
pwd:"njqgd",
roles:[
{role:"readWrite",db:"wqdwxt_njqgd"}
]
}
)

 

参考:

http://blog.csdn.net/irelandken/article/details/8003315
http://lovelace.blog.51cto.com/1028430/1441043
http://www.cnblogs.com/unqiang/p/3723643.html
http://blog.51yip.com/nosql/1582.html
http://www.royalwzy.com/?p=317

  

 

你可能感兴趣的:(mongodb)