Part 1 basics Chapter 2 install MongoDB and basic mongodb

Source: Internet
Author: User
Tags install mongodb mongodb client mongodb server

Part 1 basics Chapter 2 install MongoDB and basic mongodb

Statement: This article is mainly from the book "MongoDB Practice". I want to learn the knowledge and understanding of MongoDB through this book, so I wrote it in my blog post, as recorded in the last chapter, there will be a Java EE web application that integrates MongoDB database applications.

1. Download

MongoDB official download site is http://www.mongodb.org/downloads can download the latest installer on the above, on the download page can be seen, it supports the operating system is very comprehensive, for example, OS X, Linux, Windows, and Solaris are supported, and each of them has its own 32-bit and 64-bit versions 2.4.12.

The following describes how to install Windows and Linux.


2. Install Windows

Step 1: Download MongoDB

Url: http://www.mongodb.org/dr//fastdl.mongodb.org/win32/mongodb-win32-i386-2.6.6.zip/download

Note: We recommend that you do not use a 32-bit windows xp 32-bit environment for installation and download.

Step 2: Set the MongoDB program storage directory

Create a new folder named mongodb under the C: \ disk directory, decompress MongoDB to the directory, and then create the subdirectories data and logs. The data directory is used to store mongodb data. The logs directory stores mongodb logs.

Step 3: Configure Environment Variables

This step is mainly used to facilitate the use of command lines. You can add C: \ mongodb \ bin to the path of the system environment variable.

Step 4: Start the MongoDB service.

Open the cmd console and perform the following operations to start the MongoDB service.



Note: The default listening port of the MongoDB server is 27017.

Step 5: Install the windows Service

Perform the following operations:


In this case, you can view the MongoDB service in the windows service, as shown in:



Enter the following command in the cmd console to start the MongoDB service, or directly start the MongoDB service in windows.


Step 6: Verify the Client Connection

Open a new cmd console and enter mongo. If the following prompt is displayed, you can start your MongoDB journey.



Step 7: View MongoDB logs

Check the c: \ mongodb \ logs \ mongodb. log File to view the running status of MongoDB or to troubleshoot the problem.

This completes the MongoDB installation demonstration on the Windows platform.

3. Installation of Linux

3.1 installation instructions

System Environment: CentOS-6.4 64-bit

Software: mongodb-linux-x86_64-2.6.6.tgz

: Http://www.mongodb.org/

Upload location:/usr/src/

Installation Directory:/usr/local/mongodb

Data Location:/var/mongodb/data

Log location:/var/mongodb/logs


3.2 check whether mongodb has been installed

[Root @ localhost src] # rpm-qa | grep mongodb

[Root @ localhost src] # service mongodb status

Mongodb: unrecognized service


3.3 install mongodb

[Root @ localhost ~] # Cd/usr/src

[Root @ localhost src] # groupadd mongodb

[Root @ localhost src] # useradd mongodb-g mongodb

[Root @ localhost src] # tar-zxvf mongodb-linux-x86_64-2.6.6.tgz

[Root @ localhost src] # mv mongodb-linux-x86_64-2.6.6/usr/local/mongodb

[Root @ localhost src] # cd/usr/local/

[Root @ localhost local] # chown-R mongodb: mongodb

[Root @ localhost local] # cd mongodb/

[Root @ localhost mongodb] # mkdir/var/mongodb

[Root @ localhost mongodb] # mkdir/var/mongodb/data

[Root @ localhost mongodb] # mkdir/var/mongodb/logs

[Root @ localhost mongodb] # service iptables stop // disable the firewall in the lab environment

Iptables: Flushing firewall rules: [OK]

Iptables: Setting chains to policy ACCEPT: filter [OK]

Iptables: Unloading modules: [OK]


3.4 Configuration

Add CentOS boot item

[Root @ localhost mongodb] # vi +/etc/rc. d/rc. local

Append the mongodb startup command script to the file:

/Usr/local/mongodb/bin/mongod -- dbpath =/var/mongodb/data -- logpath/var/mongodb/logs/log. log-fork


3.5 start MongoDB

[Root @ localhost bin] #./mongod -- dbpath =/var/mongodb/data/-- logpath/var/mongodb/logs/log. log-fork


3.6 test MongoDB

[Root @ localhost bin] #./mongo

MongoDB shell version: 2.6.6

Connecting to: test

Welcome to the MongoDB shell.

For interactive help, type "help ".

For more comprehensive documentation, see

Http://docs.mongodb.org/

Questions? Try the support group

Http://groups.google.com/group/mongodb-user

>

View database list

> Show dbs

Admin (empty)

Local 0.078 GB

Switch Database

> Use admin

Switched to db admin

Add User

> Db. addUser ("xuzheng", "123456", true );

WARNING: The 'adduser' shell helper is DEPRECATED. Please use 'createuser' instead

Successfully added user: {"user": "xuzheng", "roles": ["readAnyDatabase"]}

Display status

> Db. stats ();

{

"Db": "admin ",

"Collections": 4,

"Objects": 11,

"AvgObjSize": 82.9090909090909,

"DataSize": 912,

"StorageSize": 32768,

"NumExtents": 4,

"Indexes": 3,

"IndexSize": 24528,

"FileSize": 67108864,

"NsSizeMB": 16,

"DataFileVersion ":{

"Major": 4,

"Minor": 5

},

"ExtentFreeList ":{

"Num": 0,

"TotalSize": 0

},

"OK": 1

}


Display current version:

> Db. version ();

2.6.6

Obtain the currently used database:

> Db. getMongo ();

Connection to 127.0.0.1

Simple data insertion:

> Db. user. insert ({"name": "xuzheng", age: 20 });

WriteResult ({"nInserted": 1 })

View data:

> Db. user. find ();

{"_ Id": ObjectId ("549d085621fc93b35ccba9a0"), "name": "xuzheng", "age": 20}

Note: The above is only a simple demonstration of the most common basic operations of MongoDB. By default, the database listening port of MongoDB is 27017. If you want to remotely connect to a non-service, use -- port and -- host, as shown in:


Of course, the premise is that the mongodb client tool must be installed on a remote machine, that is, the mongo service.

So far, MongoDB has been installed on Windows and Linux platforms. Due to the inconsistent system environments, some problems may occur during installation on other platforms, you can find a solution on the Internet.








Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.