Window平台安装 MongoDB
Window平台安装 MongoDB
1、MongoDB 下载
MongoDB提供了可用于32位和64位系统的预编译二进制包,你可以从MongoDB官网下载安装,MongoDB预编译二进制包下载地址:http://www.mongodb.org/downloads
下载安装包之后直接打开运行,修改安装目录到自己想放的位置,本次安装包是放在G盘

一直执行Next下一步,当进入这个界面的时候,下面的√一定要去掉,不然会安装的特别的慢,如果不去掉,可能要等几个小时以上

当所有的步骤值完成的时候,找到你的安装的目录,会有以下结果

2、环境变量配置
MongoDB安装完后,需要配置环境变量

在系统变量中的path变量里面添加MongoDB的安装目录


3、创建数据库目录
在启动mongodb服务之前需要必须创建数据库文件的存放文件夹,否则命令不会自动创建,而且不能启动成功
在你想要存放数据的地方,新建一个文件夹,如db,我们推荐的数据库目录设置是:
data/
conf -->配置文件目录
mongod.conf -->配置文件
db -->数据库目录
log -->日志文件目录
mongodb.log -->日志记录文件
4、启动数据库
1.打开cmd命令行护着用Windows+R键打开,输入cmd
2.进入G盘,也就是你安装的目录,G:\mongodb\bin;(一定要先进入G盘,让cd到你的目录 cd mongodb, 然后在cd bin这个目录下)
3.输入如下命令启动MongoDB,mongod --dbpath G:\mongodb\data\db, 这个是你安装的目录 ,然后回车
G:\>mongodb/bin
G:\mongodb\bin>mongod --dbpath=/data/db
输出以下信息:
2020-02-25T17:11:01.839+0800 I CONTROL [initandlisten] MongoDB starting : pid=1504 port=27017 dbpath=/data/db 64-bit host=Haier-PC
2020-02-25T17:11:01.842+0800 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2020-02-25T17:11:01.844+0800 I CONTROL [initandlisten] db version v4.0.1-47-gd57ef6a
2020-02-25T17:11:01.844+0800 I CONTROL [initandlisten] git version: d57ef6a0c5a41729977f8d535a4c8de6d0cff8ba
2020-02-25T17:11:01.844+0800 I CONTROL [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') BOOST_LIB_VERSION=1_49
2020-02-25T17:11:01.844+0800 I CONTROL [initandlisten] allocator: tcmalloc
2020-02-25T17:11:01.844+0800 I CONTROL [initandlisten] options: { storage: { dbPath: "/data/db" } }
2020-02-25T17:11:01.860+0800 I JOURNAL [initandlisten] journal dir=/data/db\journal
2020-02-25T17:11:01.862+0800 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
2020-02-25T17:11:01.971+0800 I JOURNAL [durability] Durability thread started
2020-02-25T17:11:01.972+0800 I JOURNAL [journal writer] Journal writer thread started
2020-02-25T17:11:02.186+0800 I INDEX [initandlisten] allocating new ns file /data/db\local.ns, filling with zeroes...
2020-02-25T17:11:02.553+0800 I STORAGE [FileAllocator] allocating new datafile /data/db\local.0, filling with zeroes...
2020-02-25T17:11:02.554+0800 I STORAGE [FileAllocator] creating directory /data/db\_tmp
2020-02-25T17:11:02.600+0800 I STORAGE [FileAllocator] done allocating datafile /data/db\local.0, size: 64MB, took 0.012 secs
2020-02-25T17:11:02.654+0800 I NETWORK [initandlisten] waiting for connections on port 27017
最后一行显示"waiting for connections on port 27017",表明mongodb服务器已经启动,正在27017窗口等待连接。
在浏览器输入http://localhost:27017,如果在浏览器中出现下面一段英文说明成功了

5、将MongoDB服务器作为Windows服务运行
像上面那样启动mongodb,发现没办法输入命令行了,这是可以采用打开多个窗口来连接,但这样就太麻烦了,解决办法就是将MongoDB服务器作为Windows服务运行。
输入以下命令:
G:\mongodb\bin>mongod --dbpath "G:\mongodb\data\db" --logpath "G:\mongodb\log\mongodb.log" --serviceName "mongodb" --serviceDisplayName "mongodb" --install
看到了如下输出:
2020-02-25T15:32:46.339+0800 I CONTROL log file "G:\mongodb\log\mongodb.log" exists; moved to "G:\mongodb\log\mongodb.log.2020-02-25T15-32-46".
说明mongodb服务安装成功。启动mongodb服务:
F:\mongodb\bin>net start mongodb
MongoDB 服务已经启动成功。
在浏览器输入http://localhost:27017,如果在浏览器中出现下面一段英文说明成功了

我们可以通过在命令窗口输入services.msc这个命令,弹出一个服务窗口,可以看到MongoDB服务启动中

说明: 当我们通过输入net start MongoDB启动的时候给我报这个错误

解决办法:
在mongodn>data>db找到这个目录,删除mongodb.lock这个文件

删除这个文件之后,再次运行 net start MongoDB这个命令,服务已经能启动了,说明MongoDB已经配置好了
6、常用命令
在bin的目录下,输入mongo启动
- show dbs 显示当前所有的数据,可以看到,当前没有数据内容

- use students 创建数据库名字, students就是数据库名字

- db 显示当前数据库

-
show collections 显示数据库中所有的集合
-
db.[collection].insert(doc) 往数据库插入文档
db.students.insert({id:"001",name:"JIM",age:"18"});这个就是我们插入的数据

- db.students.find()通过数据这个命令,查询students这个数据库当前集合中所有的文档,也可以条件查询

文章转载自博客园https://www.cnblogs.com/zhoulifeng/p/9429597.html
文章中的内容全部经过作者本人测试