一、安装环境及配置yum
# more /etc/redhat-release CentOS Linux release 7.2.1511 (Core)# vi /etc/yum.repos.d/mongodb-org-3.2.repo [mongodb-org-3.2]name=MongoDB Repositorybaseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/gpgcheck=1enabled=1gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
二、安装MongoDB
# yum -y install mongodb-org Loaded plugins: fastestmirror, langpacks base | 3.6 kB 00:00:00 epel/x86_64/metalink | 5.2 kB 00:00:00 extras | 3.4 kB 00:00:00 mongodb-org-3.2 | 2.5 kB 00:00:00 updates | 3.4 kB 00:00:00 mongodb-org-3.2/7/primary_db | 50 kB 00:00:03 Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.tuna.tsinghua.edu.cn * extras: mirrors.aliyun.com * updates: mirrors.aliyun.comResolving Dependencies--> Running transaction check---> Package mongodb-org.x86_64 0:3.2.10-1.el7 will be installed--> Processing Dependency: mongodb-org-tools = 3.2.10 for package: mongodb-org-3.2.10-1.el7.x86_64--> Processing Dependency: mongodb-org-shell = 3.2.10 for package: mongodb-org-3.2.10-1.el7.x86_64--> Processing Dependency: mongodb-org-server = 3.2.10 for package: mongodb-org-3.2.10-1.el7.x86_64--> Processing Dependency: mongodb-org-mongos = 3.2.10 for package: mongodb-org-3.2.10-1.el7.x86_64--> Running transaction check---> Package mongodb-org-mongos.x86_64 0:3.2.10-1.el7 will be installed---> Package mongodb-org-server.x86_64 0:3.2.10-1.el7 will be installed---> Package mongodb-org-shell.x86_64 0:3.2.10-1.el7 will be installed---> Package mongodb-org-tools.x86_64 0:3.2.10-1.el7 will be installed--> Finished Dependency ResolutionDependencies Resolved=================================================================================================== Package Arch Version Repository Size===================================================================================================Installing: mongodb-org x86_64 3.2.10-1.el7 mongodb-org-3.2 5.8 kInstalling for dependencies: mongodb-org-mongos x86_64 3.2.10-1.el7 mongodb-org-3.2 5.6 M mongodb-org-server x86_64 3.2.10-1.el7 mongodb-org-3.2 12 M mongodb-org-shell x86_64 3.2.10-1.el7 mongodb-org-3.2 6.7 M mongodb-org-tools x86_64 3.2.10-1.el7 mongodb-org-3.2 41 MTransaction Summary===================================================================================================Install 1 Package (+4 Dependent packages)Total download size: 65 MInstalled size: 201 MDownloading packages:warning: /var/cache/yum/x86_64/7/mongodb-org-3.2/packages/mongodb-org-3.2.10-1.el7.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID ea312927: NOKEY ] Public key for mongodb-org-3.2.10-1.el7.x86_64.rpm is not installed(1/5): mongodb-org-3.2.10-1.el7.x86_64.rpm | 5.8 kB 00:00:04 (2/5): mongodb-org-server-3.2.10-1.el7.x86_64.rpm | 12 MB 00:00:11 (3/5): mongodb-org-shell-3.2.10-1.el7.x86_64.rpm | 6.7 MB 00:00:06 (4/5): mongodb-org-tools-3.2.10-1.el7.x86_64.rpm | 41 MB 00:00:36 (5/5): mongodb-org-mongos-3.2.10-1.el7.x86_64.rpm | 5.6 MB 00:03:28 ---------------------------------------------------------------------------------------------------Total 322 kB/s | 65 MB 00:03:28 Retrieving key from https://www.mongodb.org/static/pgp/server-3.2.ascImporting GPG key 0xEA312927: Userid : "MongoDB 3.2 Release Signing Key" Fingerprint: 42f3 e95a 2c4f 0827 9c49 60ad d68f a50f ea31 2927 From : https://www.mongodb.org/static/pgp/server-3.2.ascRunning transaction checkRunning transaction testTransaction test succeededRunning transaction Installing : mongodb-org-tools-3.2.10-1.el7.x86_64 1/5 Installing : mongodb-org-mongos-3.2.10-1.el7.x86_64 2/5 Installing : mongodb-org-shell-3.2.10-1.el7.x86_64 3/5 Installing : mongodb-org-server-3.2.10-1.el7.x86_64 4/5 Installing : mongodb-org-3.2.10-1.el7.x86_64 5/5 Verifying : mongodb-org-server-3.2.10-1.el7.x86_64 1/5 Verifying : mongodb-org-3.2.10-1.el7.x86_64 2/5 Verifying : mongodb-org-shell-3.2.10-1.el7.x86_64 3/5 Verifying : mongodb-org-mongos-3.2.10-1.el7.x86_64 4/5 Verifying : mongodb-org-tools-3.2.10-1.el7.x86_64 5/5 Installed: mongodb-org.x86_64 0:3.2.10-1.el7 Dependency Installed: mongodb-org-mongos.x86_64 0:3.2.10-1.el7 #Author : Leshami mongodb-org-server.x86_64 0:3.2.10-1.el7 #Blog : http://blog.csdn.net/leshami mongodb-org-shell.x86_64 0:3.2.10-1.el7 mongodb-org-tools.x86_64 0:3.2.10-1.el7 Complete!
三、验证安装结果
# rpm -qa |grep mongodbmongodb-org-3.2.10-1.el7.x86_64mongodb-org-mongos-3.2.10-1.el7.x86_64mongodb-org-shell-3.2.10-1.el7.x86_64mongodb-org-server-3.2.10-1.el7.x86_64mongodb-org-tools-3.2.10-1.el7.x86_64# rpm -ql mongodb-org-server/etc/init.d/mongod/etc/mongod.conf/etc/sysconfig/mongod/usr/bin/mongod/usr/share/doc/mongodb-org-server-3.2.10/usr/share/doc/mongodb-org-server-3.2.10/GNU-AGPL-3.0/usr/share/doc/mongodb-org-server-3.2.10/MPL-2/usr/share/doc/mongodb-org-server-3.2.10/README/usr/share/doc/mongodb-org-server-3.2.10/THIRD-PARTY-NOTICES/usr/share/man/man1/mongod.1/var/lib/mongo/var/log/mongodb/var/log/mongodb/mongod.log/var/run/mongodb[root@localhost ~]# /etc/init.d/mongod startStarting mongod (via systemctl): [ OK ][root@localhost ~]# netstat -nltp|grep mongotcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 89958/mongod# mongoMongoDB shell version: 3.2.10connecting to: testWelcome to the MongoDB shell.For interactive help, type "help".For more comprehensive documentation, see http://docs.mongodb.org/Questions? Try the support group http://groups.google.com/group/mongodb-user> db.version()3.2.10
配置,启动和使用 mongodb
Centos7 开始采用 systemd
来管理服务
配置
fork=true ## 允许程序在后台运行#auth=true ## 开始认证logpath=/data/db/mongodb/logs/mongodb.log logappend=true # 写日志的模式:设置为true为追加。默认是覆盖dbpath=/data/db/mongodb/data/ ## 数据存放目录pidfilepath=/data/db/mongodb/logs/mongodb.pid # 进程ID,没有指定则启动时候就没有PID文件。默认缺省。port=27017#bind_ip=192.168.2.73 # 绑定地址。默认127.0.0.1,只能通过本地连接# 设置为true,修改数据目录存储模式,每个数据库的文件存储在DBPATH指定目录的不同的文件夹中。# 使用此选项,可以配置的MongoDB将数据存储在不同的磁盘设备上,以提高写入吞吐量或磁盘容量。默认为false。# 建议一开始就配置次选项directoryperdb=true# 禁止日志 # 对应 journal 启用操作日志,以确保写入持久性和数据的一致性,会在dbpath目录下创建journal目录nojournal = true ## max connections# 最大连接数。默认值:取决于系统(即的ulimit和文件描述符)限制。# MongoDB中不会限制其自身的连接。当设置大于系统的限制,则无效,以系统限制为准。# 设置该值的高于连接池和总连接数的大小,以防止尖峰时候的连接。# 注意:不能设置该值大于20000。maxConns=1024
但是实际配置中配置1024在系统的中告警 --maxConns too high, can only handle 819
, 暂时找到是什么原因
启动 Mongo
## 检查 mongodb 是否允许系统启动systemctl is-enabled mongodb## 使 mongodb 系统启动systemctl enable mongodb## 启动systemctl start mongodb## 查看启动状态systemctl status mongodb ## 停止systemctl stop mongodb
使用 Mongo
root@pts/4 $ mongo --helpMongoDB shell version: 3.0.6usage: mongo [options] [db address] [file names (ending in .js)]db address can be: foo foo database on local machine 192.169.0.5/foo foo database on 192.168.0.5 machine 192.169.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999Options: --shell run the shell after executing files --nodb don't connect to mongod on startup - no 'db address' arg expected --norc will not run the ".mongorc.js" file on start up --quiet be less chatty --port arg port to connect to --host arg server to connect to --eval arg evaluate javascript -h [ --help ] show this usage information --version show version information --verbose increase verbosity --ipv6 enable IPv6 support (disabled by default) --ssl use SSL for all connections --sslCAFile arg Certificate Authority file for SSL --sslPEMKeyFile arg PEM certificate/key file for SSL --sslPEMKeyPassword arg password for key in PEM file for SSL --sslCRLFile arg Certificate Revocation List file for SSL --sslAllowInvalidHostnames allow connections to servers with non-matching hostnames --sslAllowInvalidCertificates allow connections to servers with invalid certificates --sslFIPSMode activate FIPS 140-2 mode at startupAuthentication Options: -u [ --username ] arg username for authentication -p [ --password ] arg password for authentication --authenticationDatabase arg user source (defaults to dbname) --authenticationMechanism arg authentication mechanism --gssapiServiceName arg (=mongodb) Service name to use when authenticating using GSSAPI/Kerberos --gssapiHostName arg Remote host name to use for purpose of GSSAPI/Kerberos authenticationfile names: a list of files to run. files have to end in .js and will exit after unless --shell is specified
例子
root@pts/2 $ mongo localhost/testMongoDB shell version: 3.0.6connecting to: localhost/testServer has startup warnings: 2015-09-08T14:29:50.163+0800 I CONTROL [initandlisten] 2015-09-08T14:29:50.163+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.2015-09-08T14:29:50.163+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'2015-09-08T14:29:50.163+0800 I CONTROL [initandlisten] 2015-09-08T14:29:50.163+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2015-09-08T14:29:50.163+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'2015-09-08T14:29:50.163+0800 I CONTROL [initandlisten] > show dbslocal 0.078GBweic 0.203GBweictest 0.078GB> use weicswitched to db weic> show collectionsactorAudioattentionaudiosystem.indexestagInfo> db.tagInfo.findOne(){ "_id" : ObjectId("563c871f8105044098c6c761"), "id" : NumberLong(1), "name" : "声音萌软淑"}>