1.环境
OS: CentOS 6.5 x64
MongoDB:3.2.7
MongoDB的集群安装可以参考:http://blog.csdn.net/john_chang11/article/details/51733670
2.下载mongodb
https://www.mongodb.com/download-center?jmp=nav#community
[root@VM6-56 ~]$ curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.7.tgz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 71.6M 0 527k 0 0 25212 0 0:49:40 0:00:21 0:49:19 23015
3.安装mongodb
[root@VM6-56 ~]# groupadd mongodb
[root@VM6-56 ~]# useradd -g mongodb mongodb
[root@VM6-56 ~]# passwd mongodb
[root@VM6-56 ~]# mkdir /u01/mongodb -p
[root@VM6-56 ~]# chown mongodb.mongodb /u01/mongodb
[root@VM6-56 ~]# chown mongodb.mongodb mongodb-linux-x86_64-3.2.7.tgz
[root@VM6-56 ~]# mv mongodb-linux-x86_64-3.2.7.tgz /u01/mongodb
[root@VM6-56 ~]# su - mongodb
[mongodb@VM6-56 ~]$ cd /u01/mongodb
[mongodb@VM6-56 mongodb]$ tar -zxvf mongodb-linux-x86_64-3.2.7.tgz
[mongodb@VM6-56 mongodb]$ mv mongodb-linux-x86_64-rhel62-3.2.7 mongodb327
[mongodb@VM6-56 mongodb]$ cd
[mongodb@VM6-56 mongodb]$ vi .bash_profile
export PATH=/u01/mongodb/mongodb327/bin:$PATH:$HOME/bin
3.创建目录
[mongodb@VM6-56 mongodb]$ pwd
/u01/mongodb
[mongodb@VM6-56 mongodb]$ mkdir mongodb #创建mongodb的base目录
[mongodb@VM6-56 mongodb]$ cd mongodb
[mongodb@VM6-56 mongodb]$ mkdir data logs conf #创建数据 日志 配置目录
[mongodb@VM6-56 mongodb]$ cd data
[mongodb@VM6-56data]$ mkdir mongo #创建分片数据目录
4.配置文件
mongod的启动需要指定各种参数,这些参数可以写在文件里也可以在命令行指定,这里采用写在文件里的方式:
[mongodb@VM6-56 mongodb]$ cd conf
[mongodb@VM6-56 conf]$ vi mongo.conf
port = 32001 #端口
dbpath = /u01/mongodb/mongodb/data/mongo #数据目录
logpath = /u01/mongodb/mongodb/logs/mongo.log #日志文件
logappend = true #日志记录方式
pidfilepath = /u01/mongodb/mongodb/logs/mongo.pid #pid文件位置
directoryperdb = true
oplogSize = 1024 #操作日志大小,单位为M
fork = true #独立进程运行
storageEngine=wiredTiger #新存储引擎
#journal = true #是否记日志
5.运行
[mongodb@VM6-56 ~]$ mongod -f /u01/mongodb/mongodb/conf/mongo.conf
about to fork child process, waiting until server is ready for connections.
forked process: 18789
child process started successfully, parent exiting
启动成功,查看日志,日志文件所在的位置在参数文件里有指定:
[mongodb@VM6-56 logs]$ cat mongo.log
2016-06-25T20:36:24.508+0800 I CONTROL [initandlisten] MongoDB starting : pid=19659 port=31001 dbpath=/u01/mongodb/mongodb/data/shard1 64-bit host=VM6-56
2016-06-25T20:36:24.508+0800 I CONTROL [initandlisten] db version v3.2.7
2016-06-25T20:36:24.508+0800 I CONTROL [initandlisten] git version: 4249c1d2b5999ebbf1fdf3bc0e0e3b3ff5c0aaf2
2016-06-25T20:36:24.508+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2016-06-25T20:36:24.508+0800 I CONTROL [initandlisten] allocator: tcmalloc
2016-06-25T20:36:24.508+0800 I CONTROL [initandlisten] modules: none
2016-06-25T20:36:24.508+0800 I CONTROL [initandlisten] build environment:
2016-06-25T20:36:24.508+0800 I CONTROL [initandlisten] distmod: rhel62
2016-06-25T20:36:24.508+0800 I CONTROL [initandlisten] distarch: x86_64
2016-06-25T20:36:24.508+0800 I CONTROL [initandlisten] target_arch: x86_64
2016-06-25T20:36:24.508+0800 I CONTROL [initandlisten] options: { config: "/u01/mongodb/mongodb/conf/mongo.cnf", net: { port: 32001 }, processManagement: { fork: true, pidFilePath: "/u01/mongodb/mongodb/logs/mongo.pid" }, replication: { oplogSizeMB: 1024 }, storage: { dbPath: "/u01/mongodb/mongodb/data/mongo", directoryPerDB: true, engine: "wiredTiger" }, systemLog: { destination: "file", logAppend: true, path: "/u01/mongodb/mongodb/logs/mongo.log" } }
2016-07-03T21:53:03.443+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=18G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2016-06-25T20:36:25.172+0800 I CONTROL [initandlisten]
2016-06-25T20:36:25.172+0800 I CONTROL [initandlisten] ** WARNING: You are running on a NUMA machine.
2016-06-25T20:36:25.172+0800 I CONTROL [initandlisten] ** We suggest launching mongod like this to avoid performance problems:
2016-06-25T20:36:25.172+0800 I CONTROL [initandlisten] ** numactl --interleave=all mongod [other options]
2016-06-25T20:36:25.172+0800 I CONTROL [initandlisten]
2016-06-25T20:36:25.172+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-06-25T20:36:25.172+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-06-25T20:36:25.172+0800 I CONTROL [initandlisten]
2016-06-25T20:36:25.172+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-06-25T20:36:25.172+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-06-25T20:36:25.172+0800 I CONTROL [initandlisten]
2016-06-25T20:36:25.172+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 1024 processes, 655350 files. Number of processes should be at least 327675 : 0.5 times number of files.
2016-06-25T20:36:25.172+0800 I CONTROL [initandlisten]
2016-06-25T20:36:25.175+0800 I REPL [initandlisten] Did not find local voted for document at startup; NoMatchingDocument: Did not find replica set lastVote document in local.replset.election
2016-06-25T20:36:25.175+0800 I REPL [initandlisten] Did not find local replica set configuration document at startup; NoMatchingDocument: Did not find replica set configuration document in local.system.replset
2016-06-25T20:36:25.176+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/u01/mongodb/mongodb/data/shard1/diagnostic.data'
2016-06-25T20:36:25.176+0800 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-06-25T20:36:25.177+0800 I NETWORK [initandlisten] waiting for connections on port 31001
日志里的内容特别重要,需要认真阅读和特别留意,这里重点关注上面日志里的三个警告:
** WARNING: You are running on a NUMA machine.
** We suggest launching mongod like this to avoid performance problems:
** numactl --interleave=all mongod [other options]
** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
** We suggest setting it to 'never'
** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
** We suggest setting it to 'never'
** WARNING: soft rlimits too low. rlimits set to 1024 processes, 655350 files. Number of processes should be at least 327675 : 0.5 times number of files.
解决第二、三警告,切换到root用户执行:
[root@VM6-56 ~]# echo "never" > /sys/kernel/mm/transparent_hugepage/enabled
[root@VM6-56 ~]# echo "never" > /sys/kernel/mm/transparent_hugepage/defrag
解决第四个警告,切换到mongodb用户执行:
[mongodb@VM6-56 ~]$ vi .bash_profile #在文件最后加:
ulimit -f unlimited -t unlimited -v unlimited -n 64000 -u 64000
解决第一个警告,是改变mongod的启动方式:
[mongodb@VM6-56 ~]$ numactl --interleave=all mongod -f /u01/mongodb/mongodb/conf/shard1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 6426
ERROR: child process failed, exited with error number 48
报错,查看日志:
[mongodb@VM6-56 logs]$ tail /u01/mongodb/mongodb/logs/shard1.log
2016-06-26T14:31:46.500+0800 E NETWORK [initandlisten] listen(): bind() failed errno:98 Address already in use for socket: 0.0.0.0:31001
2016-06-26T14:31:46.500+0800 E NETWORK [initandlisten] addr already in use
2016-06-26T14:31:46.500+0800 E STORAGE [initandlisten] Failed to set up sockets during startup.
2016-06-26T14:31:46.500+0800 I CONTROL [initandlisten] dbexit: rc: 48
通过日志可知错误原因是addr already in use,这是由于mongod进程正在运行需要先关闭:
[root@VM6-56 ~]# ps -ef | grep mongo
root 8083 6176 0 14:40 pts/0 00:00:00 grep mongo
mongodb 18813 1 0 Jun25 ? 00:04:11 mongod -f /u01/mongodb/mongodb/conf/shard2.conf
mongodb 19659 1 0 Jun25 ? 00:04:10 mongod -f /u01/mongodb/mongodb/conf/shard1.conf
[root@VM6-56 ~]# kill 19659
[root@VM6-56 ~]# ps -ef | grep mongo
root 8182 6176 0 14:40 pts/0 00:00:00 grep mongo
mongodb 18813 1 0 Jun25 ? 00:04:11 mongod -f /u01/mongodb/mongodb/conf/shard2.conf
再次启动:
[mongodb@VM6-56 ~]$ numactl --interleave=all mongod -f /u01/mongodb/mongodb/conf/shard1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 8699
child process started successfully, parent exiting
启动成功,再次查看日志,看看还有警告信息没有:
2016-06-26T14:43:18.705+0800 I CONTROL [initandlisten] MongoDB starting : pid=8699 port=31001 dbpath=/u01/mongodb/mongodb/data/shard1 64-bit host=VM6-56
2016-06-26T14:43:18.705+0800 I CONTROL [initandlisten] db version v3.2.7
2016-06-26T14:43:18.705+0800 I CONTROL [initandlisten] git version: 4249c1d2b5999ebbf1fdf3bc0e0e3b3ff5c0aaf2
2016-06-26T14:43:18.705+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2016-06-26T14:43:18.705+0800 I CONTROL [initandlisten] allocator: tcmalloc
2016-06-26T14:43:18.705+0800 I CONTROL [initandlisten] modules: none
2016-06-26T14:43:18.705+0800 I CONTROL [initandlisten] build environment:
2016-06-26T14:43:18.705+0800 I CONTROL [initandlisten] distmod: rhel62
2016-06-26T14:43:18.705+0800 I CONTROL [initandlisten] distarch: x86_64
2016-06-26T14:43:18.705+0800 I CONTROL [initandlisten] target_arch: x86_64
2016-06-26T14:43:18.706+0800 I CONTROL [initandlisten] options: { config: "/u01/mongodb/mongodb/conf/mongo.cnf", net: { port: 32001 }, processManagement: { fork: true, pidFilePath: "/u01/mongodb/mongodb/logs/mongo.pid" }, replication: { oplogSizeMB: 1024 }, storage: { dbPath: "/u01/mongodb/mongodb/data/mongo", directoryPerDB: true, engine: "wiredTiger" }, systemLog: { destination: "file", logAppend: true, path: "/u01/mongodb/mongodb/logs/mongo.log" } }
2016-07-03T21:53:03.443+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=18G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2016-06-26T14:43:19.452+0800 I REPL [initandlisten] Did not find local voted for document at startup; NoMatchingDocument: Did not find replica set lastVote document in local.replset.election
2016-06-26T14:43:19.452+0800 I REPL [initandlisten] Did not find local replica set configuration document at startup; NoMatchingDocument: Did not find replica set configuration document in local.system.replset
2016-06-26T14:43:19.453+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/u01/mongodb/mongodb/data/shard1/diagnostic.data'
2016-06-26T14:43:19.453+0800 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-06-26T14:43:19.455+0800 I NETWORK [initandlisten] waiting for connections on port 31001
(END)
这次没有之前的警告信息了。