首页 > 代码库 > mongo 副本集+密码认证

mongo 副本集+密码认证

Jdk8部署

  1. 文档说明

    1. 编写目的

本文档编写目的是为了安装mongoMongodb用户认证


    1. 适用范围

本文档完成后 适用Mongodb3.0以上版本适用


    1. 配置准备

Mongodb 3.4


    1. 服务器准备


本文档中操作是对内网1台服务器部署,操作系统使用的是CentOS release 6.5 (Final)

各个节点及描述如下表所示:

节点

IP

描述

服务器

192.168.21.212/24

安装mongodb


    1. 软件准备


本文档安装的大多数软件使用源代码编译安装,主要用的文件有如下:

Mongodb3.4

下载mongodb3.2版本登录mongo官网下载,网址如下:

https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.4.4.tgz


  1. Mongodb3.4安装

    1.    192.168.21.212服务端安装

安装内容:jdk8


    1. 下载mongodb3.4.4


解压jdk8

tar zxf /mongodb-linux-x86_64-rhel62-3.4.4.tgz

拷贝3 mongo 用于副本集安装使用

mv mongodb-linux-x86_64-rhel62-3.4.4 mongo3.2

cp mongo3.2 mongo3.3 –R

cp monog3.2 mongo3.4 –R

配置mongodb副本集环境,分别进入3mongodb建立 3个文件夹 config logs sharding

Cd mongo3.2

Mkdir config logs sharding

Cd mongo3.2

Mkdir config logs sharding

Cd mongo3.3

Mkdir confing logs sharding


    1. 启动mongodb参数


第一次启动mongo不加标红的参数,标红参数用于用户认证使用

1,  /home/telehealth/software/mongodb3.2/bin/mongod -shardsvr -replSet shard1 --port 4001 --dbpath=/home/telehealth/software/mongodb3.2/sharding/ --storageEngine wiredTiger --logpath=/home/telehealth/software/mongodb3.2/logs/shard1.log --logappend --fork  --keyFile=/home/telehealth/software/mongodb3.2/mongodb-keyfile

2,  /home/telehealth/software/mongodb3.3/bin/mongod -shardsvr -replSet shard1 --port 4002 --dbpath=/home/telehealth/software/mongodb3.3/sharding/ --storageEngine wiredTiger --logpath=/home/telehealth/software/mongodb3.3/logs/shard1.log --logappend --fork  --keyFile=/home/telehealth/software/mongodb3.3/mongodb-keyfile

  1. /home/telehealth/software/mongodb3.4/bin/mongod -shardsvr -replSet shard1 --port 4003 --dbpath=/home/telehealth/software/mongodb3.4/sharding/ --storageEngine wiredTiger --logpath=/home/telehealth/software/mongodb3.4/logs/shard1.log --logappend --fork  --keyFile=/home/telehealth/software/mongodb3.4/mongodb-keyfile

进入mongodb,进行副本集配置

/home/telehealth/software/mongodb3.4/bin/mongo 127.0.0.1:4001

config = {_id: ‘shard1‘, members:[ {_id: 0, host: ‘192.168.21.212:4001‘}, {_id: 1, host: ‘192.168.21.212:4002‘}, {_id: 2, host: ‘192.168.21.212:4003‘}]};

初始化副本集

rs.initiate(config);

退出Mongo并重新进入查看副本集状态

shard1:SECONDARY> rs.conf()

{

       "_id" : "shard1",

       "version" : 1,

       "protocolVersion" : NumberLong(1),

       "members" : [

              {

                     "_id" : 0,

                     "host" : "10.0.1.161:4001",

                     "arbiterOnly" : false,

                     "buildIndexes" : true,

                     "hidden" : false,

                     "priority" : 1,

                     "tags" : {

                            

                     },

                     "slaveDelay" : NumberLong(0),

                     "votes" : 1

              },

              {

                     "_id" : 1,

                     "host" : "10.0.1.161:4002",

                     "arbiterOnly" : false,

                     "buildIndexes" : true,

                     "hidden" : false,

                     "priority" : 1,

                     "tags" : {

                            

                     },

                     "slaveDelay" : NumberLong(0),

                     "votes" : 1

              },

              {

                     "_id" : 2,

                     "host" : "10.0.1.161:4003",

                     "arbiterOnly" : false,

                     "buildIndexes" : true,

                     "hidden" : false,

                     "priority" : 1,

                     "tags" : {

                            

                     },

                     "slaveDelay" : NumberLong(0),

                     "votes" : 1

              }

       ],

       "settings" : {

              "chainingAllowed" : true,

              "heartbeatIntervalMillis" : 2000,

              "heartbeatTimeoutSecs" : 10,

              "electionTimeoutMillis" : 10000,

              "catchUpTimeoutMillis" : 2000,

              "getLastErrorModes" : {

                     

              },

              "getLastErrorDefaults" : {

                     "w" : 1,

                     "wtimeout" : 0

              },

              "replicaSetId" : ObjectId("5926c65e692eca7af2692ac1")

       }

}

shard1:SECONDARY>

配置成功

  1. Mongodb用户认证

进入mongo3.2

认证用户

> use admin

switched to db admin

> db.createUser(

...   {

...     user: "dba",

...     pwd: "dba",

...     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]

...   }

... )

添加dba用户密码dba 认证dbname admin

roles:指定用户的角色,可以用一个空数组给新用户设定空角色;在roles字段,可以指定内置角色和用户定义的角色。role里的角色可以选:

  Built-In Roles(内置角色):

    1. 数据库用户角色:readreadWrite;

    2. 数据库管理角色:dbAdmindbOwneruserAdmin

    3. 集群管理角色:clusterAdminclusterManagerclusterMonitorhostManager

    4. 备份恢复角色:backuprestore

    5. 所有数据库角色:readAnyDatabasereadWriteAnyDatabaseuserAdminAnyDatabasedbAdminAnyDatabase

    6. 超级用户角色:root 

    // 这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner userAdminuserAdminAnyDatabase

    7. 内部角色:__system

具体角色:

Read:允许用户读取指定数据库

readWrite:允许用户读写指定数据库

dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile

userAdmin:允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户

clusterAdmin:只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限。

readAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读权限

readWriteAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读写权限

userAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的userAdmin权限

dbAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限。

root:只在admin数据库中可用。超级账号,超级权限

关闭副本集 把所有mongodb kill掉,生成mongo-key文件

Cd mongo3.2

openssl rand -base64 100 >/home/telehealth/software/mongodb3.2/mongodb-keyfile

chmod 600 mongodb-keyfile

生成秘钥文件后把文件拷贝到mongodb3.3 mongo3.4

Cp monodb-keyfile /home/telehealth/software/mongodb3.3

Cp monodb-keyfile /home/telehealth/software/mongodb3.4

启动mongo

/home/telehealth/software/mongodb3.2/bin/mongod -shardsvr -replSet shard1 --port 4001 --dbpath=/home/telehealth/software/mongodb3.2/sharding/ --storageEngine wiredTiger --logpath=/home/telehealth/software/mongodb3.2/logs/shard1.log --logappend --fork  --keyFile=/home/telehealth/software/mongodb3.2/mongodb-keyfile

/home/telehealth/software/mongodb3.3/bin/mongod -shardsvr -replSet shard1 --port 4002 --dbpath=/home/telehealth/software/mongodb3.3/sharding/ --storageEngine wiredTiger --logpath=/home/telehealth/software/mongodb3.3/logs/shard1.log --logappend --fork  --keyFile=/home/telehealth/software/mongodb3.3/mongodb-keyfile

/home/telehealth/software/mongodb3.4/bin/mongod -shardsvr -replSet shard1 --port 4003 --dbpath=/home/telehealth/software/mongodb3.4/sharding/ --storageEngine wiredTiger --logpath=/home/telehealth/software/mongodb3.4/logs/shard1.log --logappend --fork  --keyFile=/home/telehealth/software/mongodb3.4/mongodb-keyfile

验证Mongodb登录

/home/telehealth/software/mongodb3.2/bin/mongo 127.0.0.1:4001 -u dba -p dba

技术分享

配置成功


本文出自 “学习linux” 博客,请务必保留此出处http://10265013.blog.51cto.com/10255013/1953429

mongo 副本集+密码认证