189 8069 5689

配置mongodb副本集-创新互联

主机信息:
[root@my2 db]# cat /etc/hosts
127.0.0.1  localhost localhost.localdomain localhost4 localhost4.localdomain4
192.168.56.101   my1.ml.com  my1
192.168.56.102   my2.ml.com  my2
192.168.56.103   my3.ml.com  my3

关闭大页内存:
echo never >> /sys/kernel/mm/transparent_hugepage/enabled
echo never >> /sys/kernel/mm/transparent_hugepage/defrag

在每个主机上面创建目录:
mkdir /data/db -p

在每个主机上面编辑配置文件:
vim /data/db/mongodb.conf

storage:
   dbPath: "/data/db"
   directoryPerDB: true
   journal:
       enabled: true
   wiredTiger:
       engineConfig:
           cacheSizeGB: 1
systemLog:
   destination: file
   path: "/data/db/mongodb.log"
   logRotate: rename
   logAppend: true
   timeStampFormat: iso8601-local
operationProfiling:
  slowOpThresholdMs: 200
  mode: slowOp
replication:
   oplogSizeMB: 10240
   replSetName: "repsetzhou"
processManagement:
   fork: true
net:
   bindIp: 0.0.0.0
   port: 27017
security:
   keyFile: "/data/db/mongodb-keyfile"
   authorization: "enabled"

生成认证文件:
[root@my1 db]# openssl rand -base64 800 > /data/db/mongodb-keyfile

scp到其他节点:
[root@my1 db]# scp /data/db/mongodb-keyfile 192.168.56.102:/data/db/
mongodb-keyfile                                                                                                   100% 1390    1.4KB/s  00:00   
[root@my1 db]# scp /data/db/mongodb-keyfile 192.168.56.103:/data/db/
mongodb-keyfile
启动三个节点上的mongodb:
/usr/local/mongodb/bin/mongod --config /data/db/mongodb.conf

登录192.168.56.101上的mongodb进行配置初始化复制集:
/usr/local/mongodb/bin/mongo
> use admin
switched to db admin

配置复制集:
config = {
   _id : "repsetzhou",
   members : [
        {_id : 0, host : "192.168.56.101:27017"},
        {_id : 1, host : "192.168.56.102:27017"},
        {_id : 2, host : "192.168.56.103:27017"},
  ]
}

初始化配置集:
rs.initiate(config)


操作:
> config = {
...    _id : "repsetzhou",
...    members : [
...         {_id : 0, host : "192.168.56.101:27017"},
...         {_id : 1, host : "192.168.56.102:27017"},
...         {_id : 2, host : "192.168.56.103:27017"},
...   ]
... }
{
   "_id" : "repsetzhou",
   "members" : [
      {
         "_id" : 0,
         "host" : "192.168.56.101:27017"
      },
      {
         "_id" : 1,
         "host" : "192.168.56.102:27017"
      },
      {
         "_id" : 2,
         "host" : "192.168.56.103:27017"
      }
   ]
}
>
> rs.initiate(config)
{ "ok" : 1 }



登录确认:
repsetzhou:PRIMARY> use admin
switched to db admin
repsetzhou:PRIMARY> rs.config()
{
   "_id" : "repsetzhou",
   "version" : 1,
   "protocolVersion" : NumberLong(1),
   "members" : [
      {
         "_id" : 0,
         "host" : "192.168.56.101:27017",
         "arbiterOnly" : false,
         "buildIndexes" : true,
         "hidden" : false,
         "priority" : 1,
         "tags" : {
            
         },
         "slaveDelay" : NumberLong(0),
         "votes" : 1
      },
      {
         "_id" : 1,
         "host" : "192.168.56.102:27017",
         "arbiterOnly" : false,
         "buildIndexes" : true,
         "hidden" : false,
         "priority" : 1,
         "tags" : {
            
         },
         "slaveDelay" : NumberLong(0),
         "votes" : 1
      },
      {
         "_id" : 2,
         "host" : "192.168.56.103:27017",
         "arbiterOnly" : false,
         "buildIndexes" : true,
         "hidden" : false,
         "priority" : 1,
         "tags" : {
            
         },
         "slaveDelay" : NumberLong(0),
         "votes" : 1
      }
   ],
   "settings" : {
      "chainingAllowed" : true,
      "heartbeatIntervalMillis" : 2000,
      "heartbeatTimeoutSecs" : 10,
      "electionTimeoutMillis" : 10000,
      "getLastErrorModes" : {
         
      },
      "getLastErrorDefaults" : {
         "w" : 1,
         "wtimeout" : 0
      },
      "replicaSetId" : ObjectId("5b9724ff670e7d5d4d9c6c7e")
   }
}
repsetzhou:PRIMARY>



添加db认证:
use admin
 
db.createUser(
{
   user:"admin",
   pwd:"admin123",
   roles:[{role:"userAdminAnyDatabase",db:"admin"}]
}
);




repsetzhou:PRIMARY> use admin
switched to db admin
repsetzhou:PRIMARY> db.createUser(
... {
... user:"admin",
... pwd:"admin123",
... roles:[{role:"userAdminAnyDatabase",db:"admin"}]
... }
... );
Successfully added user: {
   "user" : "admin",
   "roles" : [
      {
         "role" : "userAdminAnyDatabase",
         "db" : "admin"
      }
   ]
}

授权:

db.grantRolesToUser("admin", ["clusterAdmin"])
db.grantRolesToUser("admin", ["readWriteAnyDatabase"])

成都创新互联是一家专注于成都网站建设、做网站与策划设计,宁强网站建设哪家好?成都创新互联做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:宁强等地区。宁强做网站价格咨询:13518219792





另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享名称:配置mongodb副本集-创新互联
文章路径:http://cdxtjz.cn/article/dhgsij.html

其他资讯