mongdb 安装

大城市里の小女人 提交于 2020-03-24 19:37:27

mongo

mongo安装

1. install

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

sudo echo "deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/3.0 main" > /etc/apt/sources.list.d/mongodb-org.list

sudo apt-get update

sudo apt-get install -y mongodb-org=3.0.5 mongodb-org-server=3.0.5 mongodb-org-shell=3.0.5 mongodb-org-mongos=3.0.5 mongodb-org-tools=3.0.5

2.ip,port,auth修改

cd /etc/

sudo vim /etc/mongod.conf

bind_ip = 0.0.0.0

port = 27017

auth = true

  • 重启生效

/etc/init.d/mongod restart

  • 说明:mongodb存储所有的用户信息在admin 数据库的集合system.users中,保存用户名、密码和数据库信息。mongodb默认不启用授权认证,只要能连接到该服务器,就可连接到mongod。若要启用安全认证,需要更改配置文件参数auth。

数据库用户添加

1.查看数据库

show dbs

如果提示错误信息:

root@shener-XPS-8930:/etc# mongo
MongoDB shell version: 3.0.5
connecting to: test
> show dbs
2018-12-26T12:03:01.524+0800 E QUERY    Error: listDatabases failed:{
        "ok" : 0,
        "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
        "code" : 13
}
    at Error (<anonymous>)
    at Mongo.getDBs (src/mongo/shell/mongo.js:47:15)
    at shellHelper.show (src/mongo/shell/utils.js:630:33)
    at shellHelper (src/mongo/shell/utils.js:524:36)
    at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47

执行如下,

use admin

db.auth("admin","admin")

> db.auth("admin","admin")
1
> show dbs
admin  0.078GB
local  0.078GB

  • 注:有的版本中,无admin账号,需要自行创建,请参考其他资料

2.创建用户

use admin

db.createUser(
  {
    user: "root",
    pwd: "123456",
    roles: [ { role: "readWrite", db: "admin" } ]
  }
)
> db.createUser(
...   {
...     user: "root",
...     pwd: "123456",
...     roles: [ { role: "readWrite", db: "admin" } ]
...   }
... )
Successfully added user: {
        "user" : "root",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "admin"
                }
        ]
}

  • 用户认证

    db.auth("root","123456")
    
    > db.auth("root","123456")
    1
    

3.给用户添加读写权限

db.auth("root", "123456")

db.grantRolesToUser("root", [ { role: "readWrite", db: "admin" } ])

4.注意

  • 注,在mongo shell中执行,需要进入到use admin内,执行 db.auth("admin", "admin") 才能做其他操作

卸载

apt-get --purge remove mongodb*

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!