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
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':{>: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;
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 —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
}