mongdb 安装

Posted 小菜鸟!!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongdb 安装相关的知识,希望对你有一定的参考价值。

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

如果提示错误信息:

[email protected]:/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*

以上是关于mongdb 安装的主要内容,如果未能解决你的问题,请参考以下文章

Nodejs和mongdb的连接

node和mongdb安装基础教程

Mongdb 安装,环境变量配置,与运行

Centos7安装MongDB六条命令搞定

Centos7安装MongDB六条命令搞定

centos 安装 mongdb