Linux_MongoDB 安装笔记

环境:Vmware Workstation 10,CentOS-7-x86_64-DVD-1511.iso,Xshell 4.0,ip:192.168.216.140.

[root@localhost ~]# yum list mongodb-org

已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* extras: mirrors.tuna.tsinghua.edu.cn
* updates: mirrors.tuna.tsinghua.edu.cn
错误:没有匹配的软件包可以列出

[root@localhost ~]# vim /etc/yum.repos.d/mongodb-org-3.0.repo

这里写图片描述

[root@localhost ~]# yum list mongodb-org

已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* extras: mirrors.tuna.tsinghua.edu.cn
* updates: mirror.bit.edu.cn
可安装的软件包
mongodb-org.x86_64 3.0.15-1.el7 mongodb-org-3.0

[root@localhost ~]# yum install mongodb-org

[root@localhost ~]# vim /etc/mongod.conf

如果还是远程访问不了,可以改成 bindIp:0.0.0.0

这里写图片描述

[root@localhost ~]# service mongod start

Starting mongod (via systemctl): [ 确定 ]

[root@localhost ~]# service mongod status

● mongod.service - SYSV: Mongo is a scalable, document-oriented database.
Loaded: loaded (/etc/rc.d/init.d/mongod; bad; vendor preset: disabled)
Active: active (running) since 六 2017-05-27 14:55:55 CST; 21s ago
Docs: man:systemd-sysv-generator(8)
Process: 4470 ExecStart=/etc/rc.d/init.d/mongod start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/mongod.service
└─4481 /usr/bin/mongod -f /etc/mongod.conf

5月 27 14:55:55 localhost systemd[1]: Starting SYSV: Mongo is a scalable, document-oriented database….
5月 27 14:55:55 localhost runuser[4477]: pam_unix(runuser:session): session opened for user mongod by (uid=0)
5月 27 14:55:55 localhost mongod[4470]: Starting mongod: [ 确定 ]
5月 27 14:55:55 localhost systemd[1]: Started SYSV: Mongo is a scalable, document-oriented database..

[root@localhost ~]# less /var/log/mongodb/mongod.log

[root@localhost ~]# mongo

MongoDB shell version: 3.0.15
connecting to: test

> show dbs;
local 0.078GB
> use demo;
switched to db demo
> show collections;
system.indexes
user
> db.user.find();
{ “_id” : ObjectId(“5937cdcd8340ed3ab0b69800”), “_class” : “com.demo.mongo.bean.User”, “username” : “zhangsan”, “password” : “123456” }


如果机器不能联网,从官网下载mongodb的tgz包
https://www.mongodb.com/download-center?jmp=nav#community

[root@localhost ~]# tar -zxvf mongodb-linux-x86_64-3.4.5.tgz -C /usr/local/

[root@localhost ~]# mv /usr/local/mongodb-linux-x86_64-3.4.5/ /usr/local/mongodb

[root@localhost ~]# mkdir /usr/local/mongodb/data /usr/local/mongodb/logs

[root@localhost ~]# touch /usr/local/mongodb/logs/mongodb.log

注意:下面命令中“–”是两个“-”,csdn显示有误。

[root@localhost ~]# /usr/local/mongodb/bin/mongod –dbpath /usr/local/mongodb/data/ –fork –logpath /usr/local/mongodb/logs/mongodb.log –logappend

about to fork child process, waiting until server is ready for connections.
forked process: 2767
child process started successfully, parent exiting

[root@localhost ~]# ps -ef | grep mongod

root 2767 1 1 16:25 ? 00:00:00 /usr/local/mongodb/bin/mongod –dbpath /usr/local/mongodb/data/ –fork –logpath /usr/local/mongodb/logs/mongodb.log –logappend
root 2788 2724 0 16:26 pts/0 00:00:00 grep –color=auto mongod

[root@localhost ~]# /usr/local/mongodb/bin/mongo

MongoDB shell version v3.4.5
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.5
Welcome to the MongoDB shell.

show dbs;
admin 0.000GB
local 0.000GB
use test;
switched to db test
db.foo.insert({a:1});
WriteResult({ “nInserted” : 1 })
db.foo.find();
{ “_id” : ObjectId(“59478b4e7d85e6f9b74e5c04”), “a” : 1 }
quit();


在admin数据库中创建管理员,管理员有管理用户和角色的权限,不具备操作数据库的权限。

use admin
db.createUser(
  {
    user: "admin",
    pwd: "admin",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

mongod采用auth模式,第一步修改mongod.conf

security:
  authorization: enabled

第二步重启mongod,auth登录

[visitor@bogon ~]$ mongo –port 27017 -u “admin” -p “admin” –authenticationDatabase “admin”

MongoDB shell version v4.0.1
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 4.0.1
> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB
> use admin;
switched to db admin
> show collections;
> quit()

非auth登录

[visitor@bogon ~]$ mongo

MongoDB shell version v4.0.1
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.1

> show dbs;
2018-08-15T11:03:27.287+0800 E QUERY    [js] Error: listDatabases failed:{
    "ok" : 0,
    "errmsg" : "command listDatabases requires authentication",
    "code" : 13,
    "codeName" : "Unauthorized"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:67:1
shellHelper.show@src/mongo/shell/utils.js:876:19
shellHelper@src/mongo/shell/utils.js:766:15
@(shellhelp2):1:1
> use admin;
switched to db admin
> show collections;
Warning: unable to run listCollections, attempting to approximate collection names by parsing connectionStatus
> quit()

非auth登录,登录后再认证

[visitor@bogon ~]$ mongo

MongoDB shell version v4.0.1
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.1

> use admin;
switched to db admin
> show collections;
Warning: unable to run listCollections, attempting to approximate collection names by parsing connectionStatus
> db.auth("admin","admin")
1
> show collections;
> quit()

创建数据库用户

[visitor@bogon ~]$ mongo –port 27017 -u “admin” -p “admin” –authenticationDatabase “admin”

进入到visitor数据库,为此数据库添加用户

MongoDB shell version v4.0.1
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 4.0.1
> use visitor
switched to db visitor
> db.createUser(
...   {
...     user: "visitor",
...     pwd: "visitor",
...     roles: [ { role: "readWrite", db: "visitor" },
...              { role: "read", db: "test" } ]
...   }
... )
Successfully added user: {
    "user" : "visitor",
    "roles" : [
        {
            "role" : "readWrite",
            "db" : "visitor"
        },
        {
            "role" : "read",
            "db" : "test"
        }
    ]
}
> quit()

登录visitor数据库

[visitor@bogon ~]$ mongo

MongoDB shell version v4.0.1
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.1
> use visitor
switched to db visitor
> db.auth("visitor","visitor")
1
> show collections;
> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值