tflasd 2015-12-17
MongoDBDesignandDocker
IamdesignmyMongoDBschemarightnow.Actually,IjustsetupsomeJSONformatmodelthere.
Ilearnsomethingfromarticlesfromgoogle.Onedocumentcannotbeover16m,soIonlyputfewembedobjectsinonemodel,onlyIDsifthereishundreds,useonetomanywhenthereisthousandsormore.
1RecallMongoKnowledge(ThethingsIwilluse)
SQLStatementMongoStatement
altertableusersadd…
selecta,bfromusersdb.users.find({},{a:1,b:1})
2SetupMongoCluster
Basedonthedocumentsfrommeteor,wemayonlydoReplicaSetthereatfirst.
SetUptheDockerInformation
#PrepretheOS
FROMubuntu:14.04
MAINTAINERCarlLuo<[email protected]>
ENVDEBIAN_FRONTENDnoninteractive
RUNapt-get-qqupdate
RUNapt-get-qqydist-upgrade
#InstallMongoDB
RUNmkdir/tool/
WORKDIR/tool/
ADDinstall/mongodb-linux-x86_64-ubuntu1404-3.2.0.tgz/tool/
RUNln-s/tool/mongodb-linux-x86_64-ubuntu1404-3.2.0/tool/mongodb
ENVPATH/tool/mongodb/bin:$PATH
#Definemoutabledirectories
RUNmkdir-p/data/mongodb
RUNmkdir/logs/
#VOLUME["/data/","/logs/"]
#ExposeportsforMongoreplicaset
EXPOSE27017
#Copyconfigurationfiles
COPYconf//conf/
#StarttheApplication
RUNmkdir-p/app/
ADDstart.sh/app/
WORKDIR/app/
CMD["./start.sh"]
Makefile
IMAGE=sillycat/public
TAG=ubuntu-mongo-3.2
NAME=ubuntu-mongo-3.2
#REPOSITORY=sillycat.ddns.net
docker-context:
#tag-local:
#dockertag$(IMAGE):$(TAG)$(REPOSITORY)/$(IMAGE):$(TAG)
#push-local:
#dockerpush$(REPOSITORY)/$(IMAGE):$(TAG)
prepare:
wgethttps://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1404-3.2.0.tgz-Pinstall/
install:
dockerpullsillycat/public:ubuntu-mongo-3.2
mkdir-pdata/mongodb
mkdirlogs
build:docker-context
#sudodockerbuild-t$(REPOSITORY)/$(IMAGE):$(TAG).
#sudodockerbuild--no-cache-t$(IMAGE):$(TAG).
sudodockerbuild-t$(IMAGE):$(TAG).
run:
dockerrun-d--name$(NAME)-p27017:27017-v$(shellpwd)/data:/data-v$(shellpwd)/logs:/logs$(IMAGE):$(TAG)
debug:
dockerrun-ti-p27017:27017--name$(NAME)-v$(shellpwd)/data:/data-v$(shellpwd)/logs:/logs$(IMAGE):$(TAG)/bin/bash
clean:
dockerstop${NAME}
dockerrm${NAME}
logs:
dockerlogs${NAME}
publish:
dockerpush${IMAGE}
fetch:
dockerpull${IMAGE}
carl@ubuntu-build:~/work/sillycat-docker/ubuntu-mongo$catconf/mongod.conf
#mongod.conf
#Wheretostorethedata.
#Note:ifyourunmongodbasanon-rootuser(recommended)youmay
#needtocreateandsetpermissionsforthisdirectorymanually,
#e.g.,iftheparentdirectoryisn'tmutablebythemongodbuser.
dbpath=/data/mongodb/
#wheretolog
logpath=/logs/mongodb-db.log
#processid
fork=true
pidfilepath=/var/run/mongodb-db.pid
logappend=true
port=27017
smallfiles=true
#Listentolocalinterfaceonly.Commentouttolistenonallinterfaces.
#bind_ip=127.0.0.1
#Disableswrite-aheadjournaling
#nojournal=true
#EnablesperiodicloggingofCPUutilizationandI/Owait
#cpu=true
#Turnon/offsecurity.Offiscurrentlythedefault
#noauth=true
#auth=true
#Verboseloggingoutput.
#verbose=true
#Inspectallclientdataforvalidityonreceipt(usefulfor
#developingdrivers)
#objcheck=true
#Enabledbquotamanagement
#quota=true
#Setoplogginglevelwherenis
#0=off(default)
#1=W
#2=R
#3=both
#7=W+somereads
#diaglog=0
#Ignorequeryhints
#nohints=true
#EnabletheHTTPinterface(Defaultstoport28017).
#httpinterface=true
#Turnsoffserver-sidescripting.Thiswillresultingreatlylimited
#functionality
#noscripting=true
#Turnsofftablescans.Anyquerythatwoulddoatablescanfails.
#notablescan=true
#Disabledatafilepreallocation.
#noprealloc=true
#Specify.nsfilesizefornewdatabases.
#nssize=<size>
#ReplicationOptions
#inreplicatedmongodatabases,specifythereplicasetnamehere
replSet=sillycatMongoReplica
#maximumsizeinmegabytesforreplicationoperationlog
#oplogSize=1024
#pathtoakeyfilestoringauthenticationinfoforconnections
#betweenreplicasetmembers
#keyFile=/path/to/keyfile
LoginMyDockerRegistry
>sudodockerlogin
Username:sillycat
Password:
Email:[email protected]
WARNING:logincredentialssavedin/home/carl/.docker/config.json
LoginSucceeded
CommandtoPullOuttheThings
>dockerpullsillycat/public
Orwithtag
>dockerpullsillycat/public:ubuntu-mongo-3.2
RunningthingsontopofReplicaSet
SetUpTimeZoneandTimeSync
>sudodpkg-reconfiguretzdata
>sudoapt-getupdate
>sudoapt-getinstallntp
Thenwerebootthesystem,itwillautomaticallysyncthetime.
SetUpReplica
>mongo--hostubuntu-master--port27017
MongoDBshellversion:3.2.0
connectingto:ubuntu-master:27017/test
Serverhasstartupwarnings:
2015-12-16T16:55:59.526+0000ICONTROL[initandlisten]**WARNING:Youarerunningthisprocessastherootuser,whichisnotrecommended.
2015-12-16T16:55:59.526+0000ICONTROL[initandlisten]
2015-12-16T16:55:59.526+0000ICONTROL[initandlisten]
2015-12-16T16:55:59.526+0000ICONTROL[initandlisten]**WARNING:/sys/kernel/mm/transparent_hugepage/enabledis'always'.
2015-12-16T16:55:59.526+0000ICONTROL[initandlisten]**Wesuggestsettingitto'never'
2015-12-16T16:55:59.526+0000ICONTROL[initandlisten]
2015-12-16T16:55:59.526+0000ICONTROL[initandlisten]**WARNING:/sys/kernel/mm/transparent_hugepage/defragis'always'.
2015-12-16T16:55:59.526+0000ICONTROL[initandlisten]**Wesuggestsettingitto'never'
2015-12-16T16:55:59.526+0000ICONTROL[initandlisten]
Imayfixthewarninglater,butrightnow.Iwillnotspendmuchtimeonthat.
>rs.initiate()
{
"info2":"noconfigurationspecified.Usingadefaultconfigurationfortheset",
"me":"0b3c81ad5928:27017",
"ok":1
}
Itwillshareustheidofthatservice.
primary0b3c81ad5928:27017
secondary1ded2fe6b86bc:27017
secondary2a1117cd7eb5a:27017
ErrorMessagewhenItrytoaddsecondarytoprimary
>rs.add('ubuntu-dev1:27017')
{
"ok":0,
"errmsg":"Quorumcheckfailedbecausenotenoughvotingnodesresponded;required2butonlythefollowing1votingnodesresponded:0b3c81ad5928:27017;thefollowingnodesdidnotrespondaffirmatively:ubuntu-dev1:27017failedwithHostUnreachable",
"code":74
}
Solution:
https://docs.mongodb.org/manual/tutorial/enable-internal-authentication/
rs.initiate()shouldonlyberunontheprimarynode.
UseIPinstead
>mongo--host192.168.56.104--port27017
rs.add('192.168.56.106:27017')
rs.add('192.168.56.105:27017')
ErrorMessage:
rs.status()
2015-12-16T11:51:13.070-0600EQUERY[thread1]Error:errordoingquery:failed:networkerrorwhileattemptingtoruncommand'replSetGetStatus'onhost'192.168.56.104:27017':
DB.prototype.runCommand@src/mongo/shell/db.js:132:1
DB.prototype.adminCommand@src/mongo/shell/db.js:149:12
rs.status@src/mongo/shell/utils.js:1005:34
Solution:
Idonotknowwhathappened.IwilljustusesinglemongoDBfornow.
References:
mongoDBdoesnotsupportarmsystem
MongoBlogs
1~6
MongoDBDesign
http://www.cnblogs.com/egger/archive/2013/04/27/3047191.html
http://www.jianshu.com/p/bb0caddff60a
https://mongodb.github.io/casbah/
MongoDBCluster
https://www.digitalocean.com/community/tutorials/how-to-create-a-sharded-cluster-in-mongodb-using-an-ubuntu-12-04-vps
http://www.mongodbspain.com/en/2015/01/26/how-to-set-up-a-mongodb-sharded-cluster/
http://yhv5.com/mongodb-shard_821.html
http://blog.chinaunix.net/uid-25135004-id-4170240.html
http://hnr520.blog.51cto.com/4484939/1698306
https://meteorhacks.com/mongodb-replica-sets-with-meteor
Docker
https://medium.com/@arunoda/production-quality-mongodb-setup-with-docker-65136a4a9d8#.41jhrtt17
https://github.com/inlight-media/docker-mongodb-replica-set
https://docs.docker.com/engine/examples/mongodb/