Home Database Mysql Tutorial 第一部分基础篇第二章安装MongoDB

第一部分基础篇第二章安装MongoDB

Jun 07, 2016 pm 04:13 PM
mongo mongodb download Base Install

1、下载 MongoDB的官方下载站是http://www.mongodb.org/downloads 可以在上面下载最新的安装程序,在下载页面可以看到,它对操作系统支持很全面,如OS X、Linux、Windows、Solaris都支持,而且都有各自的32位和64位版本2.4.12版本。 下面将分别介绍Windows和

 

1、下载

MongoDB的官方下载站是http://www.mongodb.org/downloads 可以在上面下载最新的安装程序,在下载页面可以看到,它对操作系统支持很全面,如OS X、Linux、Windows、Solaris都支持,而且都有各自的32位和64位版本2.4.12版本。

下面将分别介绍Windows和Linux版本的安装方法。

2、Windows平台的安装

步骤一:下载MongoDB

url下载地址:http://www.mongodb.org/dr//fastdl.mongodb.org/win32/mongodb-win32-i386-2.6.6.zip/download

说明:在此演示安装下载使用windows xp 32位环境,建议不使用32位的环境。

步骤二:设置MongoDB程序存放目录

在C:\盘目录下新建名为mongodb文件夹,将MongoDB解压到至该目录下,然后在该目录下新建子目录data和logs目录。data目录用于存放mongodb的数据,logs目录用户存放mongodb的日志。

步骤三:配置环境变量

该步骤主要是为了命令行的方便使用,可以把C:\mongodb\bin加到系统环境变量的path中。

步骤四:启动MongoDB服务。

打开cmd控制台,执行如下操作即可启动MongoDB服务

\\

\

说明:MongoDB服务端的默认监听端口是27017

步骤五:安装windows服务

执行如下操作:

\\

此时可以查看windows的服务中出现了MongoDB的服务,如下图所示:

\
\

在cmd控制台中输入如下命令启动MongoDB服务,或者在windows服务中直接启动

\

步骤六:客户端连接验证

新打开一个cmd控制台,输入mongo,如果出现下面提示,那么就可以开始MongoDB之旅了。

\

\

步骤七:查看MongoDB日志

查看c:\mongodb\logs\mongodb.log文件,即可对MongoDB的运行情况进行查看或者排错了。

这样就完成了Windows平台的MongoDB安装演示。

3、Linux平台的安装

3.1、安装说明

系统环境:CentOS-6.4 64位

安装软件:mongodb-linux-x86_64-2.6.6.tgz

下载地址:http://www.mongodb.org/

上传位置:/usr/src/

安装目录:/usr/local/mongodb

数据位置:/var/mongodb/data

日志位置:/var/mongodb/logs

3.2、检查是否安装过mongodb

 

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

[root@localhost src]# service mongodb status

mongodb: unrecognized service


3.3、安装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 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//实【本文来自鸿网互联 (http://www.68idc.cn)】验环境中关闭防火墙

iptables: Flushing firewall rules: [ OK ]

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

iptables: Unloading modules: [ OK ]
Copy after login

3.4、配置

添加CentOS开机启动项

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

将mongodb启动命令脚本追加到文件中:

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

3.5、启动MongoDB

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


3.6、测试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

>
Copy after login
查看数据库列表

> show dbs

admin (empty)

local 0.078GB
Copy after login

切换数据库

> use admin

switched to db admin
Copy after login
添加用户

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

WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead

Successfully added user: { "user" : "xuzheng", "roles" : [ "readAnyDatabase" ] }
Copy after login
显示状态

> 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

}
Copy after login

显示当前版本:

 

> db.version();

2.6.6

获取当前使用数据库:

 

> db.getMongo();

connection to 127.0.0.1

简单插入数据:

 

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

WriteResult({ "nInserted" : 1 })

查看数据:

 

> db.user.find();

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

说明:以上仅仅只是简单演示下MongoDB最常用的基本操作,MongoDB默认情况下数据库监听端口为27017,如果要远程连接一个非服务,使用--port和--host来操作,如下图所示:

\\

当然前提是远程的机器上必须安装有mongodb的客户端工具也就是mongo服务。

至此,MongoDB在Windows平台和Linux平台的安装完成,由于不同的系统环境不一致,所以在其他平台中安装会出现一些问题,都可以从网上找到解决的办法,在此不依依演示。


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1279
29
C# Tutorial
1257
24
Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Apr 18, 2025 am 11:48 AM

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

How to choose a database for GitLab on CentOS How to choose a database for GitLab on CentOS Apr 14, 2025 pm 04:48 PM

GitLab Database Deployment Guide on CentOS System Selecting the right database is a key step in successfully deploying GitLab. GitLab is compatible with a variety of databases, including MySQL, PostgreSQL, and MongoDB. This article will explain in detail how to select and configure these databases. Database selection recommendation MySQL: a widely used relational database management system (RDBMS), with stable performance and suitable for most GitLab deployment scenarios. PostgreSQL: Powerful open source RDBMS, supports complex queries and advanced features, suitable for handling large data sets. MongoDB: Popular NoSQL database, good at handling sea

What is the CentOS MongoDB backup strategy? What is the CentOS MongoDB backup strategy? Apr 14, 2025 pm 04:51 PM

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

How to set up users in mongodb How to set up users in mongodb Apr 12, 2025 am 08:51 AM

To set up a MongoDB user, follow these steps: 1. Connect to the server and create an administrator user. 2. Create a database to grant users access. 3. Use the createUser command to create a user and specify their role and database access rights. 4. Use the getUsers command to check the created user. 5. Optionally set other permissions or grant users permissions to a specific collection.

How to encrypt data in Debian MongoDB How to encrypt data in Debian MongoDB Apr 12, 2025 pm 08:03 PM

Encrypting MongoDB database on a Debian system requires following the following steps: Step 1: Install MongoDB First, make sure your Debian system has MongoDB installed. If not, please refer to the official MongoDB document for installation: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/Step 2: Generate the encryption key file Create a file containing the encryption key and set the correct permissions: ddif=/dev/urandomof=/etc/mongodb-keyfilebs=512

MongoDB vs. Oracle: Choosing the Right Database for Your Needs MongoDB vs. Oracle: Choosing the Right Database for Your Needs Apr 22, 2025 am 12:10 AM

MongoDB is suitable for unstructured data and high scalability requirements, while Oracle is suitable for scenarios that require strict data consistency. 1.MongoDB flexibly stores data in different structures, suitable for social media and the Internet of Things. 2. Oracle structured data model ensures data integrity and is suitable for financial transactions. 3.MongoDB scales horizontally through shards, and Oracle scales vertically through RAC. 4.MongoDB has low maintenance costs, while Oracle has high maintenance costs but is fully supported.

What are the tools to connect to mongodb What are the tools to connect to mongodb Apr 12, 2025 am 06:51 AM

The main tools for connecting to MongoDB are: 1. MongoDB Shell, suitable for quickly viewing data and performing simple operations; 2. Programming language drivers (such as PyMongo, MongoDB Java Driver, MongoDB Node.js Driver), suitable for application development, but you need to master the usage methods; 3. GUI tools (such as Robo 3T, Compass) provide a graphical interface for beginners and quick data viewing. When selecting tools, you need to consider application scenarios and technology stacks, and pay attention to connection string configuration, permission management and performance optimization, such as using connection pools and indexes.

How to start mongodb How to start mongodb Apr 12, 2025 am 08:39 AM

To start the MongoDB server: On a Unix system, run the mongod command. On Windows, run the mongod.exe command. Optional: Set the configuration using the --dbpath, --port, --auth, or --replSet options. Use the mongo command to verify that the connection is successful.

See all articles