Mongodb主從配置
一:使用命令列方式配置mongodb主從
[root@server11 ~]# /usr/local/mongodb/bin/mongod --bind_ip 192.168.1.112 -port 3306 --dbpath /data/mongodb/db1/ --logpath /usr/local/mongodb/logs/server1.log --rest --master &
[1] 14449
[root@server12 ~]# /usr/local/mongodb/bin/mongod --bind_ip 192.168.1.113 --port 3306 --dbpath /data/mongodb/db2/ --logpath /usr/local/mongodb/logs/server2.log --rest --slave --source 192.168.1.112:3306 &
[1] 16853
二:連線測試資料同步情況
[root@server11 ~]# /usr/local/mongodb/bin/mongo 192.168.1.112:3306
MongoDB shell version: 2.2.2
connecting to: 192.168.1.112:3306/test
> use test
switched to db test
> db.test.save({b:2})
> db.test.find()
{ "_id" : ObjectId("50fcb7f05712bfb21c866dc6"), "a" : 1 }
{ "_id" : ObjectId("50fcdccf252ef3433457646f"), "b" : 2 }
[root@server12 ~]# /usr/local/mongodb/bin/mongo 192.168.1.113:3306
MongoDB shell version: 2.2.2
connecting to: 192.168.1.113:3306/test
> use test
switched to db test
> db.test.find()
{ "_id" : ObjectId("50fcb7f05712bfb21c866dc6"), "a" : 1 }
{ "_id" : ObjectId("50fcdccf252ef3433457646f"), "b" : 2 }
三:建立repl使用者,主要用於後續的安全認證同步
[root@server11 ~]# /usr/local/mongodb/bin/mongo 192.168.1.112:3306
> use local
switched to db local
> db.addUser('repl','replication')
{
"user" : "repl",
"readOnly" : false,
"pwd" : "418b80a28664aeaeb1ec8bf792ea3052",
"_id" : ObjectId("50fce98cc4553449b56c6e9f")
}
[root@server12 ~]# /usr/local/mongodb/bin/mongo 192.168.1.113:3306
> use local
switched to db local
> db.addUser('repl','replication')
{
"user" : "repl",
"readOnly" : false,
"pwd" : "418b80a28664aeaeb1ec8bf792ea3052",
"_id" : ObjectId("50fce98cc4553449b56c6e9f")
}
四:建立普通使用者yang,master端建立即可
> use admin
switched to db admin
> db.addUser('yang','123')
> show users
{
"_id" : ObjectId("50fce0bd15861bedf081584a"),
"user" : "yang",
"readOnly" : false,
"pwd" : "c26040a2869fb7579c83e85c54faaffa"
}
> db.system.users.find()
{ "_id" : ObjectId("50fce0bd15861bedf081584a"), "user" : "yang", "readOnly" : false, "pwd" : "c26040a2869fb7579c83e85c54faaffa" }
五:重啟mongodb主從例項,以auth方式啟動,使用者登入測試
[root@server11 ~]# /usr/local/mongodb/bin/mongod --bind_ip 192.168.1.112 --auth --port 3306 --dbpath /data/mongodb/db1/ --logpath /usr/local/mongodb/logs/server1.log
--rest --master &
[root@server12 ~]# /usr/local/mongodb/bin/mongod --bind_ip 192.168.1.113 --auth --port 3306 --dbpath /data/mongodb/db2/ --logpath /usr/local/mongodb/logs/server2.log
--rest --slave --source 192.168.1.112:3306 &
[root@server11 ~]# /usr/local/mongodb/bin/mongo 192.168.1.112:3306
MongoDB shell version: 2.2.2
connecting to: 192.168.1.112:3306/test
> use admin
switched to db admin
> show users
Mon Jan 21 14:42:47 uncaught exception: error: {
"$err" : "unauthorized db:test ns:test.system.users lock type:1 client:192.168.1.112",
"code" : 10057
}
> db.auth('yang','123')
1
> show users
{
"_id" : ObjectId("50fce0bd15861bedf081584a"),
"user" : "yang",
"readOnly" : false,
"pwd" : "c26040a2869fb7579c83e85c54faaffa"
}
再次登入web介面需要輸入使用者名稱和密碼!
五:使用配置檔案管理mongodb
[root@server11 ~]# cat /etc/mongodb.conf
fork = true
quiet = true
bind_ip = 192.168.1.112
port = 3306
dbpath = /data/mongodb/db1
logpath = /usr/local/mongodb/logs/server1.log
logappend = true
journal = true
rest = true
master= true
auth = true
[root@server11 ~]# /usr/local/mongodb/bin/mongod -f /etc/mongodb.conf
all output going to: /usr/local/mongodb/logs/server1.log
forked process: 5831
child process started successfully, parent exiting
[root@server11 ~]# netstat -ntpl |grep mongo
tcp 0 0 192.168.1.112:3306 0.0.0.0:* LISTEN 5831/mongod
tcp 0 0 192.168.1.112:4306 0.0.0.0:* LISTEN 5831/mongod
[root@server12 ~]# cat /etc/mongodb.conf
fork = true
quiet = true
bind_ip = 192.168.1.113
port = 3306
dbpath = /data/mongodb/db2
logpath = /usr/local/mongodb/logs/server2.log
logappend = true
journal = true
rest = true
slave = true
source = 192.168.1.112:3306
auth = true
[root@server12 ~]# /usr/local/mongodb/bin/mongod -f /etc/mongodb.conf
all output going to: /usr/local/mongodb/logs/server2.log
forked process: 3064
child process started successfully, parent exiting
[root@server11 ~]# /usr/local/mongodb/bin/mongod -f /etc/mongodb.conf --shutdown
[root@server12 ~]# /usr/local/mongodb/bin/mongod -f /etc/mongodb.conf --shutdown
參考文章:
©著作權歸作者所有:來自51CTO部落格作者ylw6006的原創作品,謝絕轉載,否則將追究法律責任
masterslavemongodbWorkSpace
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/756/viewspace-2822149/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mongodb主從仲裁節點配置MongoDB
- Redis主從配置Redis
- mysql主從配置MySql
- MySQL 主從配置-之-一主一從MySql
- docker 配置 Redis 主從DockerRedis
- mysql 5.7主從配置MySql
- MySQL主從同步配置MySql主從同步
- docker mysql 主從配置DockerMySql
- 基於 Docker 的 MongoDB 主從叢集DockerMongoDB
- Mongodb資料同步和主從切換MongoDB
- MySQL主從同步(一主一從、一主多從、主從從)等結構的概述與配置MySql主從同步
- Docker配置redis主從哨兵DockerRedis
- Mysql 一主一從配置MySql
- MySQL雙主雙從配置MySql
- MongoDB日常運維-03主從複製搭建MongoDB運維
- Redis主從同步配置實踐Redis主從同步
- keepalived配置redis主從切換Redis
- mysql主從配置(清晰的思路)MySql
- MYSQL主從複製配置(整理)MySql
- docker 配置 Mysql主從叢集DockerMySql
- thinkphp5.1redis主從配置PHPRedis
- mysql主從資料庫配置MySql資料庫
- MySQL常用操作和主從配置MySql
- 使用laradock配置mysql主從同步MySql主從同步
- mysql雙主雙從 搭建配置MySql
- MongoDB主從模式手動強制同步資料方法MongoDB模式
- mysql複製--主從複製配置MySql
- MySQL 配置多主一從 ( 8.0.18 版本 )MySql
- MySQL主從配置及mysqldump備份MySql
- redis原理及叢集主從配置Redis
- 基於Linux的mysql主從配置LinuxMySql
- 8、MySQL主從資料庫配置MySql資料庫
- 【實操記錄】MySQL主從配置MySql
- mysql主從配置,主從伺服器都是全新安裝myql的情景MySql伺服器
- MongoDB安全配置MongoDB
- MongoDB資料庫之主從複製配置實戰【轉】MongoDB資料庫
- MySQL 主從複製安裝部署配置MySql
- Docker 教程十五配置MySQL 主從複製DockerMySql