操作系统redhat6.4,采用网络yum源的方式进行安装
一、linux下mongodb的rpm包安装mongodb
1、配置yum源
[root@localhost ~]#vim /etc/yum.repos.d/mongodb-org-3.6.repo [mongodb-org-3.6]
name = MongoDB Repository
baseurl = https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck =
enabled =
gpgkey = https://www.mongodb.org/static/pgp/server-3.6.asc
2、安装mongodb
1)要安装特定版本的mongodb,需单独制定每个组件包,并将版本号附加到包名称
[root@localhost ~]#yum install -y mongodb-org-3.6. mongodb-org-server-3.6. mongodb-org-shell-3.6. mongodb-org-mongos-3.6. mongodb-org-tools-3.6.
注:会依赖安装openssl及openssl-devel,但对于操作系统6.4来说它自带的openssl版本太低,需要更新通过网易的CentOS yum源自动更新openssl,具体方法参照:https://blog.csdn.net/qq_34889607/article/details/80393668
网易163yum源:http://mirrors.163.com/centos/6/os/x86_64/Packages/ 可下载符合版本的openssl
mongodb-org-mongos-3.6.6-1.el6.x86_64.rpm
mongodb-org-server-3.6.6-1.el6.x86_64.rpm
mongodb-org-shell-3.6.6-1.el6.x86_64.rpm
mongodb-org-tools-3.6.6-1.el6.x86_64.rpm
openssl-1.0.1e-57.el6.x86_64.rpm
openssl-devel-1.0.1e-57.el6.x86_64.rpm
2)mongodb部署信息
启动脚本:/etc/init.d/mongod
主配置文件/etc/mongod.conf
日志文件路径:/var/log/mongodb/mongod.log
pid文件路径:/var/run/mongodb/mongod.pid
数据库目录路径:/data/mongodb/
端口:27017(默认)
ip:192.168.214.214
注:需修改存放mongodb数据目录/data/mongodb的属主属组
[root@localhost ~]#chown -R mongod:mongod /data/mongodb
3、启动mongodb
[root@localhost ~]#service mongod start
启动时日志中hui报出几个警告信息.
--20T12::34.842+ I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
--20T12::34.842+ I CONTROL [initandlisten] ** We suggest setting it to 'never'
--20T12::34.842+ I CONTROL [initandlisten]
--20T12::34.842+ I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
--20T12::34.842+ I CONTROL [initandlisten] ** We suggest setting it to 'never'
--20T12::34.842+ I CONTROL [initandlisten]
--20T12::34.842+ I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to processes, files. Number of processes should be at least : 0.5 times number of files.
1) /sys/kernel/mm/transparent_hugepage/enabled is 'always'.是对大内存的限制,提示可关闭suggest setting it to 'never'
命令行执行:(重启系统则无效,临时生效)
[root@localhost~]#echo never >> /sys/kernel/mm/transparent_hugepage/enabled [root@localhost~]#echo never >> /sys/kernel/mm/transparent_hugepage/defrag
mongod soft nofile
mongod hard nofile
mongod soft nproc
mongod hard nproc
3)重启mongodb,就没有报警了
[root@localhost mongodb]# service mongod restart
二、windows下安装mongodb
官网下载安装程序,开始安装
1.首先安装系统补丁,451413_intl_x64_zip.exe(不需要则可跳过此步)
官网解释为:Windows Server 2008 R2 and Windows 7 need KB2731284 to resolve an issue with memory mapped files on Windows.解决内存映射问题。
1)安装补丁
2)选择解压路径
2.安装mongodb,mongodb-win32-x86_64-2008plus-ssl-3.6.6-signed.msi
客户端工具,取消勾选
3)配置
添加配置文件,并设置数据文件路径、日志文件路径、端口号、绑定IP
D:\Program Files\MongoDB\Server\3.6\mongod.cfg 新建mongod.cfg文件,内容如下:
systemLog:
destination: "file"
path: "E:\\mongoDB_data\\log\\mongod.log"
storage:
dbPath: "E:\\mongoDB_data\\db"
net:
port: 27017
bindIp: 192.168.10.62
4)注册成服务(以管理员身份运行,否则报错)
C:\Users\Administrator> sc.exe create MongoDB binPath= "\"D:\Program Files\MongoDB\Server\3.6\bin\mongod.exe\" --service --config=\"D:\Program Files\MongoDB\Server\3.6\mongod.cfg\"" DisplayName= "MongoDB" start= auto
启动mongodb服务,添加数据库、用户并授权
启动服务:net start mongodb
创建数据库:use ccxe2CacheTest use 是进入数据库命令,如果数据库不存在则创建
在ccxe2CacheTest创建集合:db.createCollection("PUB_ORG_INFO")
给集合创建索引:1.db.PUB_ORG_INFO.createIndex({ORG_UNI_CODE:1,ORG_CHI_NAME:1,STK_CODE:1}, {background: true})
2.db.PUB_ORG_INFO.createIndex({UPDATETIME:-1}, {background: true})
*.1表示按升序创建索引;-1表示按降序创建索引
在ccxe2CacheTest数据库中添加ccxe用户:
db.createUser({
... user:'ccxe',
... pwd:'123456',
... roles:[
... {role:'readWrite',db:'ccxe2CacheTest'}
... ]})
在admin数据库中添加管理员账户:
use admin
db.createUser({
... user: "admin",
... pwd: "admin",
... roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
... });
创建超级管理员用户:
db.createUser(
... {
... user: "root",
... pwd: "root",
... roles: [ { role: "root", db: "admin" } ]
... }
... );
5)修改配置文件,启动认证功能
编辑D:\Program Files\MongoDB\Server\3.6\mongod.cfg,添加一下内容:
security:
authorization: enabled
重启mongodb服务,验证用户
三、下载mongodb源码包部署mongodb
1、解压
[root@localhost ~]#tar -xf mongodb-linux.tar.gz -C /usr/local/mongodb [root@localhost ~]#cp -r mongodb-linux/bin /usr/local/mongodb/
2.创建相应目录
[root@localhost ~]#mkdir -p /usr/local/mongodb/etc /log /data/db (固定格式,存放数据的目录)
3、创建配置文件
[root@localhost ~]#vim /usr/local/mongodb/etc/mongodb.conf logpath=/usr/local/mongodb/log/mongodb.log
logappend=true //追加的方式记录日志信息
dbpath=/usr/local/mongodb/data/db
fork=true //守护的方式运行
port=
bind_ip=192.168.214.214
#以下为源码包构建副本集,配置文件指定格式写法
replSet=rs1 //指定副本集名称,追加写入,可定义名称
10 auth=true //开启用户认证
11 keyFile=/data/mongodb/keyfile //指定认证秘钥路径
12 journal=true //开启journal日志,默认为开启
4、启动mongodb服务
[root@localhost ~]#./bin/mongod -f /usr/local/mongodb/etc/mongodb.conf [root@localhost ~]#./bin/mongodb --shutdown -f /usr/local/mongodb/etc/mongodb.conf
5、进入数据库,连接服务
[root@localhost ~]#/usr/local/mongodb/bin/mongo --host 192.168.214.214 --port