http://blog.chinaunix.net/uid-24086995-id-1754660.html 在同一台机器上用2个不同的端口,启动mongodb 在数据库目录下建立2个新目录 [ root @ localhost ~ ] # mkdir / data / db / master [ root @ localhost ~ ] # mkdir / data / db / slave 主:只需要
http://blog.chinaunix.net/uid-24086995-id-1754660.html
在同一台机器上用2个不同的端口,启动mongodb
在数据库目录下建立2个新目录
-
[root@localhost ~]# mkdir /data/db/master
[root@localhost ~]# mkdir /data/db/slave
主:只需要带上--master参数,表明这是个主就可以了,相当的方便。
-
[root@localhost ~]# /usr/local/bin/mongod --master
-dbpath=/data/db/master -port=11536 &
-
[1] 10939
-
[root@localhost ~]# Mon
Jul 25 20:21:59 [initandlisten] MongoDB
starting : pid=10939 port=11536 dbpath=/data/db/master
master=1 32-bit
从:带上--slave参数指明是个从,指定--source,主的地址和端口,就可以了,比MYSQL方便很多。
-
[root@localhost bin]# /usr/local/bin/mongod -port
11537 --slave -dbpath=/data/db/slave --source 127.0.0.1:11536
-
Mon Jul 25 20:25:52 [initandlisten] MongoDB
starting : pid=11158 port=11537 dbpath=/data/db/slave
slave=1 32-bit
新开一个终端,登录11536(主),并写入一条记录
-
[root@localhost ~]# /usr/local/bin/mongo --port=11536
-
MongoDB shell version: 1.8.2
-
connecting to: 127.0.0.1:11536/test
-
> use brucezuo
-
switched to db brucezuo
-
> db.createCollection("table1")
-
{ "ok" : 1 }
-
> db.table1.insert({id:1,name:"zxy",age:29})
-
>
新开一个终端,登录11537(从),查看数据是否一致
-
[root@localhost ~]# /usr/local/bin/mongo --port=11537
-
MongoDB shell version: 1.8.2
-
connecting to: 127.0.0.1:11537/test
-
> show dbs
-
admin (empty)
-
brucezuo 0.0625GB
-
local 0.0625GB
-
> use brucezuo
-
switched to db brucezuo
-
> db.table1.find()
-
{ "_id" : ObjectId("4e2e348ed502dbae1aee469e"), "id" : 1, "name" : "zxy", "age" : 29 }
-
>
两端数据完全一致。
从上面的测试来看,mongodb对主从复制的配置比MYSQL要方便太多了。
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn