第一部分基础篇第二章安装MongoDB
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 ]
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 >
> show dbs admin (empty) local 0.078GB
切换数据库
> use admin switched to db admin
> db.addUser("xuzheng","123456",true); WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead Successfully added user: { "user" : "xuzheng", "roles" : [ "readAnyDatabase" ] }
> 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 }
显示当前版本:
> 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平台的安装完成,由于不同的系统环境不一致,所以在其他平台中安装会出现一些问题,都可以从网上找到解决的办法,在此不依依演示。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



.NET 4.0 is used to create a variety of applications and it provides application developers with rich features including: object-oriented programming, flexibility, powerful architecture, cloud computing integration, performance optimization, extensive libraries, security, Scalability, data access, and mobile development support.

In a serverless architecture, Java functions can be integrated with the database to access and manipulate data in the database. Key steps include: creating Java functions, configuring environment variables, deploying functions, and testing functions. By following these steps, developers can build complex applications that seamlessly access data stored in databases.

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

PiNetwork is about to launch PiBank, a revolutionary mobile banking platform! PiNetwork today released a major update on Elmahrosa (Face) PIMISRBank, referred to as PiBank, which perfectly integrates traditional banking services with PiNetwork cryptocurrency functions to realize the atomic exchange of fiat currencies and cryptocurrencies (supports the swap between fiat currencies such as the US dollar, euro, and Indonesian rupiah with cryptocurrencies such as PiCoin, USDT, and USDC). What is the charm of PiBank? Let's find out! PiBank's main functions: One-stop management of bank accounts and cryptocurrency assets. Support real-time transactions and adopt biospecies

The way to download Microsoft PowerToys is: 1. Open PowerShell and run wingetinstallMicrosoft.PowerToys, 2. or visit the GitHub page to download the installation package. PowerToys is a set of tools to improve Windows user productivity. It includes features such as FancyZones and PowerRename, which can be installed through winget or graphical interface.

Sorting index is a type of MongoDB index that allows sorting documents in a collection by specific fields. Creating a sort index allows you to quickly sort query results without additional sorting operations. Advantages include quick sorting, override queries, and on-demand sorting. The syntax is db.collection.createIndex({ field: <sort order> }), where <sort order> is 1 (ascending order) or -1 (descending order). You can also create multi-field sorting indexes that sort multiple fields.
