一.Preface

https://en.wikipedia.org/wiki/MongoDB


二.Installation

(1)Win7:

1.Adrress: https://www.mongodb.com/download-center#community. mongodb-win32-x86_64-2008plus-ssl-3.4.3-signed.msi


2. install

3.
create db file and log file under your mongoDB path; etc: D:\mongoDB

4.run cmd under d:\mongoDB(shift+right-cilck)
D:\mongoDB\bin>mongod.exe --logpath "D:\mongoDB\data\log\mongodb.log" --logappend --dbpath "D:\mongoDB\data\db" --serviceName "Mongodb" --install

params introduction:

--bind_ip: binding IP.
--logpath: designate path of log file of MongoDB.
--logappend: using append method to record log
--dbpath: DB path
--port: default 27017
--serviceName
--serviceDisplayName: when there are several mongoDB server
--install: as a Windows server

5.start server and import data
*:if we tend to recover or copy data in Windows, we need to enter bin directory.
Example: D:mongoDB\bin> mongorestore.exe -h 127.0.0.1 --port 27017 -d SleepMult --drop D:\SleepMult


(2)Win10:

1. setting environment variable:

The steps of 2,3,4 is same as WIN7


2.create mongod.cfg and enter the following content:

systemLog:
    destination: file
    path: D:\mongoDB\logs\mongodb.log
storage:
    dbPath: D:\mongoDB\data\db


3.rum cmd:

D:\mongoDB\bin\mongod.exe --dbpath D:\mongoDB\data\db


4.do not close this cmd and open another cmd:

D:\mongoDB\bin\mongo.exe


5.if success, close these tow cmd, and open a cmd:

D:\mongoDB\bin\mongod.exe --config D:\mongoDB\mongod.cfg --install


6.start service :

cmd: net start MongoDB


Common problem:

1.ServiceName error:

Two ways: your ServiceName spell wrong or do not run cmd under administrator


2.Server cannot start:

delete mognd.lock under db directory.


三.MongoDB-Cluster:

1.Project Requirement

With the increase of business data and concurrency, if only use a mongo server, there is a risk of power outages and data problems, so adopt the way of directing a duplicate set, to improve project performance such as high availability and security.


1.2.Replica set construction

This time, three mongodb nodes are installed in a single Windows to simulate the replica set. In the real mongodb replication set, we only need to change the IP and port number .


1.2.1.Environment Preparation

Installed MongoDB


1.2.2.Construction

1.2.2.1

Create a folder in the F disk to store the Mongodb replica set, and copy the previous Mondb (single machine) installation files into the folder under this replica set, and name it Mongodb1, Mongodb2, and Mongodb3.



1.2.2.2

Create a data folder under the replicate set folder, which is used to store the relevant data for the replicate set.
Enter the data folder to create the data, key, and log folders, respectively.



1.2.2.3

Under the data folder, I created three folders to store the data of each node, and I named them r1, r2 and r3 respectively.



1.2.2.4

Enter into the replicate sets' secret key folder, create a TXT file, write some data in the file, and then caopy tow of them (make sure each node of the secret key file content is consistent, otherwise the replicate set create failure).




1.2.2.5

Three log files are created in the log folder to store log information for each node.



1.2.2.6

Under the mongodb1/bin directory, press shift+ right click to open the command prompt line here, perform the initializing mongodb operation, and also perform the corresponding operation in mongodb /bin, mongodb3/bin.


Command:

mongod.exe --replSet  (Sets name)  --port (port num) --dbpath (database path) --logpath=(logpath) –logappend

Note: the name of the replicate set of the three nodes must be consistent. Since we are starting three nodes on the same machine, the port number of each node cannot be repeated.
- logappend indicates that log information is an appended mode.


For example:

Node1:

mongod.exe --replSet mongodbCluster --port 28010 --dbpath E:\MongodbCluster\data\data\r1 --logpath=E:\MongodbCluster\data\log\log1.log --logappend

 

Node2:

mongod.exe --replSet mongodbCluster --port 28011 --dbpath E:\MongodbCluster\data\data\r2 --logpath=E:\MongodbCluster\data\log\log2.log --logappend

 

Node3:

mongod.exe --replSet mongodbCluster  --port 28012 --dbpath E:\MongodbCluster\data\data\r3 --logpath=E:\MongodbCluster\data\log\log3.log –logappend


Success:



Check log:



1.2.2.7

Open a command prompt in the bin directory of any node, and then connect with the mongo.


Command:

mongo.exe –port (Main node port num)


Further configure the node information and initialize the Replica Sets environment to execute the following js code.

var conf = {
  _id : "mongodbCluster",
  members : [
        { _id : 2, host : "localhost:28011"},
{ _id : 1, host : "localhost:28010"},
        { _id : 3, host : "localhost:28012"}
  ]
}

rs.initiate(conf);


Priority: for configuration priority, when priority=0, this instance will never be set to primary and can only exist as a slave.
When no priority is set for all nodes, primary is elected by the system.


Check Log



1.2.2.8

You can run rs. Status () to see the status of the replicate set.


Where, the _id table name node id.
Health: mark of normal or not, 1 is normal, 0 is abnormal.
State: this node ACTS as a role in the replicate set, 1 for primary and 0 for slave.
In addition to looking at the status of the member nodes in the replication set with the rs. Status (), you can also view it with the rs. IsMaster command.


At this point, the mongodb replica set is installed.

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐