某电商公司MongoDB装机文档

MongoDB · ruyi · 于 2年前发布 · 1403 次阅读

以下安装流程中安装目录和MySQL配置参数请根据自己的服务器和业务特性调整:

== 安装MongoDB ==

创建目录

$ mkdir /data

创建分区

$ parted /dev/sdb -s mklabel gpt

$ parted /dev/sdb -s mkpart primary 0% 100%

格式化

$ mkfs.ext4 /dev/sdb1

挂载

$ mount -o nobarrier,noatime,nodiratime /dev/sdb1 /data

$ echo ‘/dev/sdb1 /data ext4 defaults,nobarrier,noatime,nodiratime 0 0′ >> /etc/fstab

设置系统参数

$ echo ” >> /etc/sysctl.conf

$ echo ‘fs.file-max = 655360′ >>/etc/sysctl.conf

$ echo ‘vm.swappiness = 0′ >>/etc/sysctl.conf

$ sysctl -p

$ echo ” >> /etc/security/limits.conf

$ echo ‘* – nproc 655360 ‘ >>/etc/security/limits.conf

$ echo ‘* – nofile 655360 ‘ >>/etc/security/limits.conf

设置NTP

$ echo ” >> /etc/crontab

$ echo ’01 00 * root /usr/sbin/ntpdate 192.168.254.250 >/dev/null 2>&1′ >>/etc/crontab

创建mongodb用户

$ groupadd mongodb

$ useradd -d /data/mongodb -s /bin/bash -g mongodb mongodb

安装MongoDB二进制包

$ sudo su – mongodb

$ tar xzf mongodb-linux-x86_64-2.4.5.tgz

$ mv mongodb-linux-x86_64-2.4.5/bin .

$ rm -rf mongodb-linux-x86_64-2.4.5

$ mkdir config data log

创建配置文件

$ vi ./config/mongod.cnf

fork = 1 maxConns = 1000 port = 27017 pidfilepath = /data/mongodb/data/mongod.pid unixSocketPrefix = /data/mongodb/data logpath = /data/mongodb/log/mongod_data.log $ logappend = 1 quiet = 1 profile = 1 slowms = 500 journal = 1 dbpath = /data/mongodb/data directoryperdb = 1 syncdelay = 30 nohttpinterface = 1 rest = 0 jsonp = 0 auth = 1 repairpath = /data/mongodb/data $ repair = 1 $ upgrade = 1

启动MongoDB

$ numactl –interleave=all /data/mongodb/bin/mongod -f /data/mongodb/config/mongod.cnf

创建管理员用户,类似于MySQL中的root,MongoDB没有这样的预设用户

$ mongo admin

db.addUser(‘dbadmin’,’123456′);

exit

创建只读用户,类似于MySQL中的query,可以用于海葵

$ mongo -udbadmin -p123456 admin

db.addUser(‘query’,’query’,true);

db.system.users.find();

== 配置ReplSet ==

目前三台机器:

IP Port Role Shift Replset

10.0.2.56 27017 DataNode Yes rs1

10.0.2.57 27017 DataNode Yes

10.0.2.58 27017 DataNode No

10.0.2.58 27027 Arbiter -

根据上面步骤,完成10.0.2.56:27017、10.0.2.57:27017和10.0.2.58:27017的DataNode安装配置

根据以下步骤,完成10.0.2.58:27027的Arbiter安装配置

创建Arbiter配置文件

$ vi ./config/arbiter.cnf

fork = 1 maxConns = 1000 port = 27027 pidfilepath = /data/mongodb/arbiter/mongod.pid unixSocketPrefix = /data/mongodb/arbiter logpath = /data/mongodb/log/mongod_arbiter.log $ logappend = 1 quiet = 1 profile = 1 slowms = 500 journal = 1 dbpath = /data/mongodb/arbiter directoryperdb = 1 syncdelay = 10 nohttpinterface = 1 rest = 0 jsonp = 0 auth = 1 repairpath = /data/mongodb/arbiter $ repair = 1 $ upgrade = 1

创建目录

$ mkdir arbiter

启动Arbiter

$ numactl –interleave=all /data/mongodb/bin/mongod -f /data/mongodb/config/arbiter.cnf

创建Arbiter管理员用户

$ mongo –port 27027 admin

db.addUser(‘dbadmin’,’123456′);

exit

在四个实例上创建复制用户

$ mongo -udbadmin –p123456 –port 27017 admin

use local

db.addUser(‘repl’,’repl’);

exit

$ mongo -udbadmin -p123456 –port 27027 admin

use local

db.addUser(‘repl’,’repl’);

exit

关闭所有DataNode和Arbiter

$ mongo -udbadmin -p123456 –port 27017 admin

db. shutdownServer();

exit

$ mongo -udbadmin -pyihaodian –port 27027 admin

db. shutdownServer();

exit

调整三个DataNode实例的配置文件,增加以下内容

$ vi ./config/mongod.cnf

oplogSize = 10000 replSet = rs1 keyFile = /data/mongodb/data/rs1.keyfile fastsync = 1

调整Arbiter实例的配置文件,增加以下内容

$ vi ./config/arbiter.cnf

oplogSize = 10000 replSet = rs1 keyFile = /data/mongodb/arbiter/rs1.keyfile fastsync = 1

在四个实例上生成对应的keyFile文件

$ echo ‘wireless information push’ >./data/rs1.keyfile

$ chmod 600 ./data/rs1.keyfile

$ echo ‘wireless information push’ >./arbiter/rs1.keyfile

$ chmod 600 ./arbiter/rs1.keyfile

在各机上启动相应的DataNode和Arbiter

$ numactl –interleave=all /data/mongodb/bin/mongod -f /data/mongodb/config/mongod.cnf

$ numactl –interleave=all /data/mongodb/bin/mongod -f /data/mongodb/config/arbiter.cnf

初始化Replset,在10.0.2.56:27017上操作

$ mongo -udbadmin -p123456 –port 27017 admin

config={_id:’rs1′,members:[{_id:0,host:'10.0.2.56:27017',priority:10}]};

rs.initiate(config);

过一小会儿,命令提示符会有变化

rs1:PRIMARY>

加入各结点

rs1:PRIMARY> rs.add({_id:2,host:’10.0.2.57:27017′,priority:10});

rs1:PRIMARY> rs.add({_id:4,host:’10.0.2.58:27017′,priority:0});

rs1:PRIMARY> rs.addArb(“10.0.2.58:27027″);

检查Replset状态

rs1:PRIMARY> rs.conf();

{

“_id” : “rs1″,

“version” : 9,

“members” : [

{

"_id" : 0,

"host" : "10.0.2.56:27017",

"priority" : 10

},

{

"_id" : 2,

"host" : "10.0.2.57:27017",

"priority" : 10

},

{

"_id" : 4,

"host" : "10.0.2.58:27017",

"priority" : 0

},

{

"_id" : 5,

"host" : "10.0.2.58:27027",

"arbiterOnly" : true

}

]

}

rs1:PRIMARY> db.isMaster();

{

“setName” : “rs1″,

“ismaster” : true,

“secondary” : false,

“hosts” : [

"10.0.2.56:27017",

"10.0.2.57:27017"

],

“passives” : [

"10.0.2.58:27017"

],

“arbiters” : [

"10.0.2.58:27027"

],

“primary” : “10.0.2.56:27017″,

“me” : “10.0.2.56:27017″,

“maxBsonObjectSize” : 16777216,

“maxMessageSizeBytes” : 48000000,

“localTime” : ISODate(“2013-07-26T03:33:28.898Z”),

“ok” : 1

}

rs1:PRIMARY> rs.status();

{

“set” : “rs1″,

“date” : ISODate(“2013-07-26T03:32:19Z”),

“myState” : 1,

“members” : [

{

"_id" : 0,

"name" : "10.0.2.56:27017",

"health" : 1,

"state" : 1,

"stateStr" : "PRIMARY",

"uptime" : 5251,

"optime" : Timestamp(1374809149, 1),

"optimeDate" : ISODate("2013-07-26T03:25:49Z"),

"self" : true

},

{

"_id" : 2,

"name" : "10.0.2.57:27017",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 390,

"optime" : Timestamp(1374809149, 1),

"optimeDate" : ISODate("2013-07-26T03:25:49Z"),

"lastHeartbeat" : ISODate("2013-07-26T03:32:18Z"),

"lastHeartbeatRecv" : ISODate("2013-07-26T03:32:17Z"),

"pingMs" : 0,

"syncingTo" : "10.0.2.56:27017"

},

{

"_id" : 4,

"name" : "10.0.2.58:27017",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 390,

"optime" : Timestamp(1374809149, 1),

"optimeDate" : ISODate("2013-07-26T03:25:49Z"),

"lastHeartbeat" : ISODate("2013-07-26T03:32:18Z"),

"lastHeartbeatRecv" : ISODate("2013-07-26T03:32:17Z"),

"pingMs" : 0,

"syncingTo" : "10.0.2.56:27017"

},

{

"_id" : 5,

"name" : "10.0.2.58:27027",

"health" : 1,

"state" : 7,

"stateStr" : "ARBITER",

"uptime" : 390,

"lastHeartbeat" : ISODate("2013-07-26T03:32:18Z"),

"lastHeartbeatRecv" : ISODate("2013-07-26T03:32:17Z"),

"pingMs" : 0

}

],

“ok” : 1

}


如果这篇文章对您有帮助,不妨微信小额赞助我一下,让我有动力继续写出高质量的帖子。

共收到 0 条回复 电商公司 MongoDB 装机文档
没有找到数据。
回复本帖 (需要登录)