标签:mongo
https://repo.mongodb.org/yum/redhat/6Server/mongodb-org/3.4/x86_64/RPMS/
1.下载所有的rpm包
上传到服务器
[root@localhost ~]# ll total 93528 -rw-------. 1 root root 1231 Feb 24 02:07 anaconda-ks.cfg -rw-r--r-- 1 root root 5900 Jul 22 06:42 mongodb-org-3.4.6-1.el6.x86_64.rpm -rw-r--r-- 1 root root 12182737 Jul 22 06:42 mongodb-org-mongos-3.4.6-1.el6.x86_64.rpm -rw-r--r-- 1 root root 20608908 Jul 22 06:42 mongodb-org-server-3.4.6-1.el6.x86_64.rpm -rw-r--r-- 1 root root 11767549 Jul 22 06:42 mongodb-org-shell-3.4.6-1.el6.x86_64.rpm -rw-r--r-- 1 root root 51195582 Jul 22 06:42 mongodb-org-tools-3.4.6-1.el6.x86_64.rpm [root@localhost ~]#
2.安装
yum localinstall mongodb-org-*
3.修改配置文件
vi /etc/mongod.conf
# mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # where to write logging data. systemLog: destination: file logAppend: true path: /opt/mongo/log/mongo.log #日志文件 # Where and how to store data. storage: dbPath: /opt/mongo/data #数据 journal: enabled: true # engine: # mmapv1: # wiredTiger: # how the process runs processManagement: fork: true # fork and run in background pidFilePath: /opt/mongo/mongopid/mongo.pid # location of pidfile #pid # network interfaces net: port: 62114 #端口和监听的IP bindIp: 192.168.56.15 # Listen to local interface only, comment to listen on all interfaces. #security: #operationProfiling: #replication: #sharding: ## Enterprise-Only Options #auditLog: #snmp: [root@localhost ~]# chown -R mongod.mongod /opt/mongo
4.启动mongo
systemctl start mongod
5.进入mongo
mongo --host 192.168.56.15 --port 62114 Server has startup warnings: 2017-07-22T07:07:43.607+0800 I CONTROL [initandlisten] 2017-07-22T07:07:43.607+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2017-07-22T07:07:43.608+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2017-07-22T07:07:43.608+0800 I CONTROL [initandlisten] 2017-07-22T07:07:43.608+0800 I CONTROL [initandlisten] 2017-07-22T07:07:43.608+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always‘. 2017-07-22T07:07:43.608+0800 I CONTROL [initandlisten] ** We suggest setting it to ‘never‘ 2017-07-22T07:07:43.608+0800 I CONTROL [initandlisten] 2017-07-22T07:07:43.608+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always‘. 2017-07-22T07:07:43.608+0800 I CONTROL [initandlisten] ** We suggest setting it to ‘never‘ 2017-07-22T07:07:43.608+0800 I CONTROL [initandlisten] 2017-07-22T07:07:43.608+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 1024000 files. Number of processes should be at least 512000 : 0.5 times number of files. 2017-07-22T07:07:43.608+0800 I CONTROL [initandlisten] 解决办法 [root@localhost ~]# tail -n 4 /etc/security/limits.conf mongod soft nofile 64000 mongod hard nofile 64000 mongod soft nproc 32000 mongod hard nproc 32000 [root@localhost ~]# [root@localhost ~]# echo "never" >/sys/kernel/mm/transparent_hugepage/enabled [root@localhost ~]# echo "never" >/sys/kernel/mm/transparent_hugepage/defrag never 此处系统重启失效 所以把这个两个echo 放在/etc/rc.local 中
6.增加用户名和密码
增加超级管理员 mongo --host 192.168.56.15 --port 62114 db.createUser({user:"admin",pwd:"123456",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
7.退出 让mongo用户认证登陆
vim /etc/mongod.conf security: #打开注释 authorization: enabled #增加一行 systemctl restart mongod 测试: [root@localhost ~]# mongo --host 192.168.56.15 --port 62114 MongoDB shell version v3.4.6 connecting to: mongodb://192.168.56.15:62114/ MongoDB server version: 3.4.6 > show dbs; 2017-07-22T07:43:14.962+0800 E QUERY [thread1] Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }", "code" : 13, "codeName" : "Unauthorized" } : _getErrorWithCode@src/mongo/shell/utils.js:25:13 Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1 shellHelper.show@src/mongo/shell/utils.js:769:19 shellHelper@src/mongo/shell/utils.js:659:15 @(shellhelp2):1:1 > 测试通过 [root@localhost ~]# mongo --host 192.168.56.15 --port 62114 -u admin -p 123456 MongoDB shell version v3.4.6 connecting to: mongodb://192.168.56.15:62114/ MongoDB server version: 3.4.6 > show dbs; admin 0.000GB local 0.000GB >
8.管理员增加一个库,并赋予特定的用户(库是创建了 默认不写入东西看不见)
> db.createUser({ user: "test", ... pwd: "123456", ... roles:[{"role":"readWrite","db":"mall"}, ... {"role":"dbAdmin","db":"mall"}, ... {"role":"dbOwner","db":"mall"}, ... {"role":"read","db":"mall"}]}) Successfully added user: { "user" : "test", "roles" : [ { "role" : "readWrite", "db" : "mall" }, { "role" : "dbAdmin", "db" : "mall" }, { "role" : "dbOwner", "db" : "mall" }, { "role" : "read", "db" : "mall" } ] } > > show dbs; #管理员登陆也看不见 admin 0.000GB local 0.000GB > mongo --host 192.168.56.15 --port 62114 -u test -p 123456 > db.auth("test","123456") 1 > use mall; #test 用户登陆可以switch switched to db mall >
9.插入数据 用test用户登录
test用户 > db.inventory.insert( { _id: 10, type: "misc", item: "card", qty: 15 } ) WriteResult({ "nInserted" : 1 }) > admin用户 插入数据后可以看到了 [root@localhost ~]# mongo --host 192.168.56.15 --port 62114 -u admin -p 123456 MongoDB shell version v3.4.6 connecting to: mongodb://192.168.56.15:62114/ MongoDB server version: 3.4.6 > show dbs; admin 0.000GB local 0.000GB mall 0.000GB >
10服务没关闭 直接关闭服务器 导致mongodb启动不起来
解决办法:
[root@localhost mongopid]# mv mongo.pid mongo.pid.ori [root@localhost mongopid]# pwd /opt/mongo/mongopid [root@localhost mongopid]#
11.windows客户端连接mongodb
本文出自 “砖家博客” 博客,请务必保留此出处http://wsxxsl.blog.51cto.com/9085838/1949913
标签:mongo
原文地址:http://wsxxsl.blog.51cto.com/9085838/1949913