mongodb(4)Latest Version on MAC and Setup Replication

mongodb(4)Latest Version on MAC and Setup Replication

1. Installation on MAC
Get the latest file from here: http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-2.4.7.tgz
unzip the file and find a right place
>cd /Users/carl/tool/mongodb-osx-x86_64-2.4.7 
>sudo ln -s /Users/carl/tool/mongodb-osx-x86_64-2.4.7 /opt/mongodb-2.4.7
>sudo ln -s /opt/mongodb-2.4.7 /opt/mongodb

By default, mongo writes data to the /data/db directory.
>sudo mkdir -p /data/db
>sudo chown carl /data/db

If this path is not what you want, you can use --dbpath option to mongod 
>sudo vi ~/.profile
export PATH=/opt/mongodb/bin:$PATH
>. ~/.profile 

>vi mongodb.conf
fork = true  //daemon

bind_ip = 127.0.0.1

port = 27017

quiet = true        

dbpath = /data/db/mongodb

logpath = /var/log/mongodb/mongod.log

logappend = true

journal = true

Start the Server
>mongod -f mongodb.conf

>ps -ef | grep mongo

Try with Client to connect to it.
>mongo
mongo>db.test.save({a:1,b:2})
mongo>db.test.find()
{ "_id" : ObjectId("52684c242cb20b7935c6537d"), "a" : 1, "b" : 2 }

Some Grammar of SQL
SQL Statement                                 Mongo Statement 
create table users                             db.createCollection("users"); 

insert into users values ...                 db.users.insert({a:3,b:5}) 
select a,b from users                        db.users.find({},{a:1,b:1}) 

select * from users                            db.users.find() 
select a,b from users where age =33    db.users.find({age:33},{a:1,b:1}) 
select * from users where age=33 order by name  db.users.find({age:33}).sort({name:1}) 
select * from users where name like "%sillycat%"    db.users.find({name:/sillycat/}) 

select * from users where name like "sillycat%"   db.users.find({name:/^sillycat/}) 

select * from users where age>33 and age <=40      db.users.find({'age':{&gt:33,$lte:40}}) 

select * from users order by name desc     db.users.find().sort({name:-1}) 

select * from users where a=1 and b='q'    db.users.find({a:1,b:'q'}) 

select * from users limit 10 skip 20     db.users.find().limit(10).skip(20) 
select * from users where a=1 or b=2     db.users.find({$or : [ {a:1},{b:2} ] } ) 
select * from users limit 1          db.users.findOne() 

select count() from users             db.users.count() 
select count() from users where age > 30    db.users.find({age:{'$gt':30}}).count() 

update users set a=1 where b='q'    db.users.update({b:'q'},{$set:{a:1}},false,true) 

delete from users where z = 'abc'      db.users.remove({z:'abc'}) 

Some Useful Command:
>help;
>use databaseName;
>db;
>show dbs;

2. Replication 
client application —— Primary Secondary Secondary ——— Arbiter

Primary — receives all write operations, and put the operations on the primary’s oplog.
Secondaries —  replications, may be non-voting or priority 0.
Arbiter — Arbiters allow replica sets to have an uneven number of members. — Do not run an arbiter on systems that also host the primary or the secondary members
Priority 0 Replica Set Members — can not become primary.
Hidden Replica Set Members — can not become primary, do backup and log.
Delayed Replica Set Members — rolling back

A replica set can have up to 12 members. Only 7 members can vote at a time.

Replica Set Deployment Architectures
Use Journaling to protect Against Power Failures.

3. Deploy a Replica Set with 2 secondaries and 1 primary
I plan to deploy 1 primary and 2 secondaries on my local machine.
The configuration and commands are follow:
mongodb-master.conf
fork = true

bind_ip = 127.0.0.1

port = 27017

quiet = true

dbpath = /data/db/mongodb-master

logpath = /var/log/mongodb/mongod-master.log

logappend = true

journal = true
replSet = sillycat

mongo-client1.conf
fork = true

bind_ip = 127.0.0.1

port = 27018

quiet = true

dbpath = /data/db/mongodb-client1

logpath = /var/log/mongodb/mongod-cient1.log

logappend = true

journal = true
replSet = sillycat

mongo-client2.conf
fork = true

bind_ip = 127.0.0.1

port = 27019

quiet = true

dbpath = /data/db/mongodb-client2

logpath = /var/log/mongodb/mongod-client2.log

logappend = true

journal = true
replSet = sillycat

>mongod -f mongodb-master.conf
>mongod -f mongodb-client1.conf
>mongod -f mongodb-client2.conf

>mongo —host 127.0.0.1 —port 27017
>rs.in
Error Message:
couldn't initiate : can't find self in the replset config

Solution:
I configure the name of the computer sparkworker1.local:27017 to /etc/hosts to fix this problem.
{

"info2" : "no configuration explicitly specified -- making one",

"me" : "sparkworker1.local:27017",

"info" : "Config now saved locally.  Should come online in about a minute.",

"ok" : 1
}

>rs.conf()
{

"_id" : "sillycat",

"version" : 1,

"members" : [ { "_id" : 0, "host" : "sparkworker1.local:27017" } ]
}

Some details are here:
rs.add() http://docs.mongodb.org/manual/reference/method/rs.add/#rs.add   

rs.add(host, arbiterOnly) 
                             eg.  rs.add(‘127.0.0.1:27018') 
rs.initiate() http://docs.mongodb.org/manual/reference/method/rs.initiate/#rs.initiate   rs.initiate(config)
rs.conf() http://docs.mongodb.org/manual/reference/method/rs.conf/#rs.conf      
rs.reconfig() http://docs.mongodb.org/manual/reference/method/rs.reconfig/#rs.reconfig    rs.reconfig(config, { force: true }) 

>rs.add("sparkworker1.local:27018")
>rs.add("sparkworker1.local:27019")

>rs.conf()
{

"_id" : "sillycat",

"version" : 3,

"members" : [ {

"_id" : 0,

"host" : "sparkworker1.local:27017"

}, {

"_id" : 1,

"host" : "sparkworker1.local:27018"

}, {

"_id" : 2,

"host" : "sparkworker1.local:27019" } ]
}

>rs.status()

Save some data in primary with these commands
>db.users.insert({name:"Carl", age:31});

Find this data in Primary
>db.users.find();
{ "_id" : ObjectId("526abebcb32108edb1b0d203"), "name" : "Carl", "age" : 31 }

Check the data on Secondaries
>mongo --host 127.0.0.1 --port 27018
>db.users.find();

Error Message
error: { "$err" : "not master and slaveOk=false", "code" : 13435 }

Solution:
Execute on the Secondary
>rs.slaveOk(); 
> db.users.find();
{ "_id" : ObjectId("526abebcb32108edb1b0d203"), "name" : "Carl", "age" : 31 }

Once I kill the master 1, the secondary will become the PRIMARY. After I restart the old master 1, it will join the net as secondary.

4. Clients
come soon...
http://docs.mongodb.org/ecosystem/drivers/scala/

References:
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/
http://docs.mongodb.org/manual/administration/configuration/

http://docs.mongodb.org/manual/reference/replica-configuration/#replica-set-reconfiguration-usage
http://docs.mongodb.org/manual/reference/sql-comparison/
http://docs.mongodb.org/manual/tutorial/deploy-replica-set/

http://blog.csdn.net/luonanqin/article/details/8497860

Old Blogs
http://sillycat.iteye.com/blog/1547291
http://sillycat.iteye.com/blog/1547292
http://sillycat.iteye.com/blog/1547294

http://sillycat.iteye.com/blog/603890

猜你喜欢

转载自sillycat.iteye.com/blog/1965857