Getting Started with MongoDB (Installation and Configuration)

1. Introduction

     

    MongoDB is a non-relational database (NoSql), which is a powerful, flexible and scalable data storage method. Because MongoDB is a document model, it is very free and flexible, which can make your development process extremely smooth. For big data MongoDB can handle Internet applications with high volume, high concurrency, and weak transactions. MongoDB’s built-in horizontal expansion mechanism provides data processing capabilities ranging from one million to one billion levels. It also supports MapReduce-style aggregation and geospatial indexing. support.


2. Installation and configuration

1. Download and install

 http://www.mongodb.org/downloads

  My system is Windows7 64-bit, so I download the corresponding version mongodb-win64-i386-2.6.8.zip, which is suitable for you according to your computer situation.

    

2. Installation and configuration

  Step 1: Unzip to a disk, such as my unzipped directory D:\mongodb, as shown in the figure:

    

 Step 2: Configure the directory for storing logs and data, otherwise mongoDB will not start, as shown in the figure:

   

 

   We first run cmd.exe to enter the dos command interface, and then enter the cd D:\mongodb\bin directory. Next, we need to configure the files of the directory where logs and data are stored. We use the help command to view which commands, as shown in the figure shown:

   

  

 We found (1)--logpath arg: arg is the path to set the log storage (2)--dbpath arg: arg is the path to store the data file

  First configure the directory of the data. For example, I want to store it in the directory D:\mongodb\data\db. We run cmd.exe to enter the dos command interface, then enter the cd D:\mongodb\bin directory, and run mongod.exe command, as shown in the figure:

    

  

  Description: The error is reported because we need to manually create the file path of D:\mongodb\data\db. We create the path and execute it once. This time it is successful, as shown in the figure:

   

   

   Configure the log path. For example, I want to store it in the directory D:\mongodb\data\log. We run cmd.exe to enter the dos command interface, then enter the cdD:\mongodb\bin directory, and run the mongod.exe command mongod. exe --logpath=D:\mongodb\data\log\mongodb.log , as shown:

    

 

 Step 3: Test if the configuration is normal

  We are running cmd.exe to enter the dos command interface, then enter the cd D:\mongodb\bin directory, run the mongo command, and test the connection, as shown in the figure:

    

An error occurred

  Note: If the server is not started, we need to configure the MongoDB server by ourselves. We create a mongodb.config configuration file in the D:\mongodb directory. There are two attributes logpath and dbpath, and the values ​​are set respectively, which is the path we just configured. as the picture shows:

   

 mongod.exe --config d:\mongodb\mongodb.config to start the MongoDB server, the --config option indicates that the server is configured through the information in the d:\mongodb\mongodb.config configuration file at startup. as the picture shows:

  

  Do not close the dos command interface first, the MongoDB server is running, I will open a new dos command interface, first enter the cd D:\mongodb\bin directory, and then run the mongo command to test the link, as shown in the figure:

  


This indicates that the configuration is successful at this time.

 

three. Add MongoDB to Windows Service

   If we turn off the dos command interface running the MongoDB server, so that we cannot link to MongoDB, we need to add it to the Windows Service like mySql, and then start and close the service on the command line, which is convenient for us to operate and manage the service.

    Next, we need to put MongoDB to Windows Service. The command used is --install to install MongoDB as a server to Windows Service.

   第一步:执行—install,以管理员的启动cmd, 进入dos命令界面,先进入到cd D:\mongodb\bin目录下,执行

D:\mongodb\bin>mongod.exe--dbpath=d:\mongodb\data\db --logpath=d:\mongodb\data\log\log.txt  --install --serviceName "MongoDB"  如图所示:

    

   


 第二步:启动/关闭MongoDB服务

   netstart mongodb 启动MongoDB服务

  net stop mongodb 启动MongoDB服

  

四:测试一下MongoDB的shell

  第一步:运行cmd.exe, 进入dos命令界面,先进入到cd D:\mongodb\bin目录下,运行mongo命令,进入shell,查看数据库show dbs,如图所示:

               

             

   第二步:切换并产生数据库

                 在切换数据库时,如果数据库不存在,则直接产生数据库,use test:如图所示:

                 

   第三步:创建了一个集合并保存了数据

                   创建了名为zhang的集合

                db.zheng.insert({username:'zty'})

                

  第三步:查找zhang集合所有的数据

             db.zhang.find()

            




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325747970&siteId=291194637