mongodb 主:10.23.2.3
mongodb 从:10.23.2.4
配置文件参考链接如下:
http://segmentfault.com/a/1190000002924522
以下内容在主上操作:
#mkdir -p /home/tools //创建软件包存储目录,可以自己定义
#cd /home/tools/ //进入软件包目录
#wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.3.tgz //下载软件包
#tar -zxvf mongodb-linux-x86_64-3.0.3.tgz //解压文件
#mv mongodb-linux-x86_64-3.0.3/ /usr/local/mongodb
#vim /etc/mongodb.cnf
#指定数据库路径
dbpath=/root/home/mongodb/data/
#指定MongoDB日志文件,注意是指定文件不是目录
logpath=/root/home/mongodb/data/mongodb.log
#指定服务端口号,默认端口27017
port=27017
#以守护进程的方式运行MongoDB,创建服务器进程
fork=true
#是否启用验证
#auth=true
#关闭http接口,默认关闭27018端口访问
nohttpinterface=true
#绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定默认本地所有IP
#bind_ip=10.172.185.52
#mkdir -p /home/mongodb/data/
#/usr/local/mongodb/bin/mongod -f /etc/mongodb.cnf
#/usr/local/mongodb/bin/mongo
>use admin
>db.createUser({user:”admin”,pwd:”admin”,roles:[“root”]});
>db.system.users.find();
>use etalk
>db.foo.save({“id”:1,”name”:’mazhenguo’});
>db.createUser({user:”etalk”,pwd:”etalk”,roles:[{role:”readWrite”,db:”etalk”}]});
>use admin
>db.system.users.find();
>use admin
>db.shutdownServer()
#vim /etc/mongodb.cnf //开启认证
#指定数据库路径
dbpath=/root/home/mongodb/data/
#指定MongoDB日志文件,注意是指定文件不是目录
logpath=/root/home/mongodb/data/mongodb.log
#指定服务端口号,默认端口27017
port=27017
#以守护进程的方式运行MongoDB,创建服务器进程
fork=true
#是否启用验证
auth=true
#关闭http接口,默认关闭27018端口访问
nohttpinterface=true
#绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定默认本地所有IP
#bind_ip=10.172.185.52
#/usr/local/mongodb/bin/mongod -f /etc/mongodb.cnf
#/usr/local/mongodb/bin/mongo
>use admin
switched to db admin
> show dbs
2015-07-25T12:34:01.674+0800 E QUERY Error: listDatabases failed:{
“ok” : 0,
“errmsg” : “not authorized on admin to execute command { listDatabases: 1.0 }”,
“code” : 13
}
at Error ()
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
switched to db admin
> db.auth (“admin”,”admin”)
1
> show dbs;
admin 0.078GB
etalk 0.078GB
local 0.078GB
> use etalk
switched to db etalk
> db.foo.save({“id”:2,”name”:’zhangsan’});
WriteResult({
“writeError” : {
“code” : 13,
“errmsg” : “not authorized on etalk to execute command { insert: \”foo\”, documents: [ { id: 2.0, name: \”zhangsan\”, _id: ObjectId(’55b31f296109928ba3bfd8db’) } ], ordered: true }”
}
})
> db.auth(“etalk”,”etalk”)
1
> db.foo.save({“id”:2,”name”:’zhangsan’});
WriteResult({ “nInserted” : 1 })
>
#openssl rand -base64 741 > /usr/localhsot/mongodb/mongo-keyfile
#cd /usr/localhsot/mongodb/
#chmod 400 mongo-keyfile
#cd /usr/local/
#scp -r mongodb/ root@10.23.2.4:/usr/local/
#cd /root/home/
#scp -r mongodb/ root@10.23.2.4:/root/home/
#vim /etc/mongodb.cnf
dbpath=/root/home/mongodb/data/
logpath=/root/home/mongodb/data/mongodb.log
port=27017
fork=true
keyFile=/usr/local/mongodb/mongo-keyfile
auth=true
nohttpinterface=true
#bind_ip=10.172.185.52
master=true
source=10.23.2.4:27017
logappend=true
oplogSize=100
#/usr/local/mongodb/bin/mongod -f /etc/mongodb.cnf
about to fork child process, waiting until server is ready for connections.
forked process: 4499
child process started successfully, parent exiting
# /usr/local/mongodb/bin/mongo
MongoDB shell version: 3.0.3
connecting to: test
> use admin
switched to db admin
> db.auth(“admin”,”admin”)
1
> use etalk
switched to db etalk
> show dbs
admin 0.078GB
etalk 0.078GB
local 0.203GB
> use etalk
switched to db etalk
> db.foo.save({“id”:3,”name”:’zhangsan1′});
WriteResult({ “nInserted” : 1 })
> db.foo.save({“id”:4,”name”:’zhangsan2′});
WriteResult({ “nInserted” : 1 })
> db.foo.save({“id”:6,”name”:’mazhenguo’});
WriteResult({ “nInserted” : 1 })
>
以下是从服务器的操作
#vim /etc/mongodb.cnf
dbpath=/root/home/mongodb/data/
logpath=/root/home/mongodb/data/mongodb.log
port=27017
fork=true
auth=true
nohttpinterface=true
#bind_ip=10.172.185.52
slave = true
source = 10.23.2.3:27017
keyFile=/usr/local/mongodb/mongo-keyfile
logappend=true
oplogSize=100
#/usr/local/mongodb/bin/mongod -f /etc/mongodb.cnf
about to fork child process, waiting until server is ready for connections.
forked process: 4258
child process started successfully, parent exiting
[root@bogon mongodb]# /usr/local/mongodb/bin/mongo
MongoDB shell version: 3.0.3
connecting to: test
> use admin
switched to db admin
> show dbs
2015-07-25T16:58:12.143+0800 E QUERY Error: listDatabases failed:{
“ok” : 0,
“errmsg” : “not authorized on admin to execute command { listDatabases: 1.0 }”,
“code” : 13
}
at Error ()
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
> db.auth(“admin”,”123456″)
Error: 18 Authentication failed.
0
> db.auth(“admin”,”admin”)
1
> show dbs
2015-07-25T16:59:01.454+0800 E QUERY Error: listDatabases failed:{ “note” : “from execCommand”, “ok” : 0, “errmsg” : “not master” }
at Error ()
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 etalk
switched to db etalk
> db.foo.find({“id”:3})
{ “_id” : ObjectId(“55b35027d6ad130f3ddb656c”), “id” : 3, “name” : “zhangsan1” }
> db.foo.find({“id”:2})
> db.foo.find({“id”:4})
{ “_id” : ObjectId(“55b3502dd6ad130f3ddb656d”), “id” : 4, “name” : “zhangsan2” }
> db.foo.find({“id”:6})
{ “_id” : ObjectId(“55b3508dd6ad130f3ddb656e”), “id” : 6, “name” : “mazhenguo” }
在启动前,先把所有的配置文件修改好,然后先启动主,再启动从,在主里面插入数据,在从上查询,看看能否查到数据,根据实验的结果,一定能查到。