MongoDB入坑

MongoDB入坑

安装略

MongoDB启动方式

安装完mongodb后,就可以这样启动,

➜  mongodb pwd
/Users/xinxingegeya/IDE/mongodb
➜  mongodb bin/mongod

还可以通过指定参数方式启动,

➜  mongodb bin/mongod --dbpath=/data/db --bind_ip=127.0.0.1 --port=12345

然后就可以通过下面的方式连接到mongodb,

➜  mongodb bin/mongo 127.0.0.1:12345
MongoDB shell version: 3.2.4
connecting to: 127.0.0.1:12345/test
>

以上两种方式都是以非daemon方式启动的。如何以daemon方式启动呢?

可以使用mongodb自带的参数--fork,还要注意当使用fork参数的时候,必须指定logpath参数,如下,

➜  mongodb bin/mongod --dbpath=/data/db --bind_ip=127.0.0.1 --port=12345 --fork
BadValue: --fork has to be used with --logpath or --syslog
try 'bin/mongod --help' for more information
➜  mongodb bin/mongod --dbpath=/data/db --bind_ip=127.0.0.1 --port=12345 --fork --logpath=/Users/xinxingegeya/IDE/mongodb/log/mongodb.log
about to fork child process, waiting until server is ready for connections.
forked process: 1620
child process started successfully, parent exiting
➜  mongodb ps -ef | grep mongod
  501  1620     1   0 12:11下午 ??         0:01.15 bin/mongod --dbpath=/data/db --bind_ip=127.0.0.1 --port=12345 --fork --logpath=/Users/xinxingegeya/IDE/mongodb/log/mongodb.log
  501  1632   965   0 12:11下午 ttys002    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn mongod
➜  mongodb

为了方便,可以把配置参数写入到配置文件中,每次启动的时候都可以指定配置文件的方式启动。

mongodb的配置文件是基于yaml格式的,如下是部分配置,

systemLog:
   destination: file
   path: ./log/mongodb.log
   logAppend: true
net:
   port: 12345
   bindIp: 127.0.0.1
   maxIncomingConnections: 1000
processManagement:
   fork: true
security:
    authorization: disabled

指定配置文件方式启动,

➜  mongodb bin/mongod --config mongodb.yaml
about to fork child process, waiting until server is ready for connections.
forked process: 1717
child process started successfully, parent exiting
➜  mongodb ps -ef |grep mongod
  501  1717     1   0 12:26下午 ??         0:00.76 bin/mongod --config mongodb.yaml
  501  1723   965   0 12:26下午 ttys002    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn mongod
➜  mongodb

好的可以看到启动成功。


MongoDB添加用户

像mysql一样,mongodb也可以使用用户名和密码登录指定的数据库,如果不指定登录的数据库的话,默认是test数据库。如下,

➜  mongodb bin/mongo 127.0.0.1:12345
MongoDB shell version: 3.2.4
connecting to: 127.0.0.1:12345/test

下面就来演示一下如何使用用户名和密码来登。

首先在mydb数据库中添加用户,

> show dbs
testdb  0.000GB
> use mydb
switched to db mydb
> db.myCollection.insert({"name":"hello world"})
WriteResult({ "nInserted" : 1 })
> show dbs
mydb    0.000GB
testdb  0.000GB
> db.createUser(
...   {
...     user: "root",
...     pwd: "034039",
...     roles: [
...        { role: "readWrite", db: "mydb" },
...     ]
...   }
...  )
Successfully added user: {
	"user" : "root",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "mydb"
		}
	]
}
> show users
{
	"_id" : "mydb.root",
	"user" : "root",
	"db" : "mydb",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "mydb"
		}
	]
}
> show dbs
admin   0.000GB
mydb    0.000GB
testdb  0.000GB
> use admin
switched to db admin
> show users
> show collections
system.users
system.version
> db.system.users.find()
{ "_id" : "mydb.root", "user" : "root", "db" : "mydb", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "ru0tsPFgcsem2Wca1ALZgA==", "storedKey" : "TEjwrgDStoS/C4CEXX0YWp5US/8=", "serverKey" : "Yx0Eji3t3RnrO0/LfFP6+rTs5es=" } }, "roles" : [ { "role" : "readWrite", "db" : "mydb" } ] }
>

从上面的操作来看,在mydb创建用户之后,多了一个admin数据库。

其实admin数据库是用来管理其他数据库的用户的。

总之现在在mydb数据库的用户创建好了,看一下如何来使用该用户认证,首先要开启mongodb的--auth参数,这里我们使用yaml配置文件选项来开启auth,

systemLog:
   destination: file
   path: ./log/mongodb.log
   logAppend: true
net:
   port: 12345
   bindIp: 127.0.0.1
   maxIncomingConnections: 1000
processManagement:
   fork: true
security:
    authorization: enabled

重启mongodb,

➜  mongodb bin/mongo 127.0.0.1:12345
MongoDB shell version: 3.2.4
connecting to: 127.0.0.1:12345/test
> show dbs
2016-04-15T16:07:30.725+0800 E QUERY    [thread1] Error: listDatabases failed:{
	"ok" : 0,
	"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
	"code" : 13
} :
_getErrorWithCode@src/mongo/shell/utils.js:23:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:53:1
shellHelper.show@src/mongo/shell/utils.js:700:19
shellHelper@src/mongo/shell/utils.js:594:15
@(shellhelp2):1:1

> use mydb
switched to db mydb
> show collections
2016-04-15T16:07:44.433+0800 E QUERY    [thread1] Error: listCollections failed: {
	"ok" : 0,
	"errmsg" : "not authorized on mydb to execute command { listCollections: 1.0, filter: {} }",
	"code" : 13
} :
_getErrorWithCode@src/mongo/shell/utils.js:23:13
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:746:1
DB.prototype.getCollectionInfos@src/mongo/shell/db.js:758:15
DB.prototype.getCollectionNames@src/mongo/shell/db.js:769:12
shellHelper.show@src/mongo/shell/utils.js:695:9
shellHelper@src/mongo/shell/utils.js:594:15
@(shellhelp2):1:1

> db.auth("root","034039")
1
> show collections
myCollection
>

可以看到,只有在auth通过后,命令才能执行成功。

也可以直接通过登录的时候完成用户认证,如下操作

➜  mongodb bin/mongo 127.0.0.1:12345/mydb -uroot -p034039
MongoDB shell version: 3.2.4
connecting to: 127.0.0.1:12345/mydb
> show collections
myCollection
>

=========END=========

转载于:https://my.oschina.net/xinxingegeya/blog/660750

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值