`
sillycat
  • 浏览: 2551611 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

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

分享到:
评论

相关推荐

    MongoDB可视化工具Mac版本

    "MongoDB可视化工具Mac版本"特指的是Robo 3T(前身为Robomongo),它是免费且强大的MongoDB管理工具,尤其适合Mac用户。 Robo 3T是Robomongo的升级版,继承了Robomongo的轻量级特性,并在其基础上进行了功能扩展和...

    mongodb 4.2.9 Community version zip版本

    MongoDB 4.2.9 社区版是一款流行的开源文档型数据库,专为现代应用程序设计,具有高可用性、可扩展性和灵活性。这个版本是针对Windows Server 2012和2008优化的,确保在这些操作系统上稳定运行,同时也兼容其他操作...

    Mastering MongoDB 4.x, 2nd Edition

    Master the new features and capabilities of MongoDB 4.x Implement advanced data modeling, querying, and administration techniques in MongoDB Includes rich case-studies and best practices followed ...

    mongodb管理工具 Mac破解版

    mongodb 版本 破解版本,好用, 啊又免费啊

    MongoDB and Python

    Python MongoDB 应用开发,构建高效稳定数据库应用系统

    Mastering MongoDB 3.x

    The book is based on MongoDB 3.x and covers topics ranging from database querying using the shell, built in drivers, and popular ODM mappers to more advanced topics such as sharding, high ...

    mongodb4.4.10.tgz(mongo4目前最新稳定版本)

    MongoDB 4.4.10是MongoDB数据库的一个稳定版本,专为那些希望在Linux环境下运行MongoDB的用户设计。MongoDB是一个流行的开源、分布式文档数据库,它以JSON格式存储数据,支持丰富的查询功能,提供了高可用性、水平...

    mongodb-linux-x86_64-rhel70-4.4.13安装包和conf配置文件

    4. **网络配置**:可以使用`bindIp`设置MongoDB服务监听的IP地址,如果希望MongoDB对外提供服务,可以设置为0.0.0.0。 5. **权限与安全**:为了保护数据库,你可以启用身份验证(`auth`),并配置`security.keyFile`...

    slf4j输入日志到mongodb

    SLF4J(Simple Logging Facade for Java)是Java中一个用于日志记录的抽象层,它提供了一种标准的日志记录接口,使得开发者可以选择不同的日志实现框架,如Log4j、Logback等。MongoDB则是一个流行的NoSQL数据库系统...

    Practical MongoDB - Architecting, Developing, and Administering

    根据提供的文件信息,本书《Practical MongoDB - Architecting, Developing, and Administering》由Shakuntala Gupta Edward和Navin Sabharwal合著,旨在为读者提供MongoDB的全面指南,涵盖架构、开发和管理等方面。...

    Mastering MongoDB 4.x - Second Edition.pdf

    《Mastering MongoDB 4.x - Second Edition》这本书旨在帮助读者掌握MongoDB 4.x版本的高级知识与技术,以便于运行高容量和容错性的数据库解决方案。 首先,要掌握MongoDB 4.x的相关知识,需要了解其核心概念和特性...

    深入学习MongoDB:Scaling MongoDB && 50 Tips and Tricks for MongoDB Developers

    深入学习MongoDB:Scaling MongoDB && 50 Tips and Tricks for MongoDB Developers深入学习MongoDB中文版Scaling MongoDB英文版50 Tips and Tricks for MongoDB Developers英文版高清完整目录3本打包合集

    MongoDB on Kubernetes技术解决方案.pptx

    MongoDB on Kubernetes技术解决方案 MongoDB on Kubernetes技术解决方案旨在提供一种快速、灵活和高效的方式来部署和管理MongoDB数据库在Kubernetes集群中。该解决方案利用Kubernetes的强大API和Operator来实现...

    MongoDBDemo.rar

    &lt;package id="MongoDB.Bson" version="2.8.1" targetFramework="net46" /&gt; &lt;package id="MongoDB.Driver" version="2.8.1" targetFramework="net46" /&gt; &lt;package id="MongoDB.Driver.Core" version="2.8.1" target...

Global site tag (gtag.js) - Google Analytics