设为首页 收藏本站
查看: 661|回复: 0

[经验分享] mongodb(4)Latest Version on MAC and Setup Replication

[复制链接]

尚未签到

发表于 2016-5-18 10:58:45 | 显示全部楼层 |阅读模式
  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.iyunv.com/blog/1547291
http://sillycat.iyunv.com/blog/1547292
http://sillycat.iyunv.com/blog/1547294

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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-218645-1-1.html 上篇帖子: I'm a Mac:雄狮训练手册 下篇帖子: Ext中浏览器、OS以及CSS、数据类型的判定
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表