MongoDB的介绍我就不说了。直接开始环境的搭建和连接。在这个之前,向大家介绍几个关于MongoDB的网站。
1. https://www.mongodb.com/
MongoDB的官网。
2. https://www.mongoing.com
MongoDB的中文网站
3. http://docs.mongoing.com/manual-zh/
中文文档
4. http://github.com/mongodb
github上mongodb的托管地址,各个版本都有。
5. jira.mongodb.org/browser
MongoDB bug反馈的地方
自己本来是打算在linux系统上搭建的,但是环境的搭建总是这么不顺利。最开始是在github下载了源码,然后去编译,但是不知道为什么源码总是编译失败。然后放弃源码编译,去官网下载了二进制压缩包。下载完成后启动Mongodb服务器端是还是出现了错误,多次尝试解决未果,就只能先暂时放下然后转战windows系统进行学习实践。不过在后面有时间的话一定会在linux环境中成功搭建。所以下面的实战均是在windows系统下的练习。
1.下载mongodb。
①进入官网https://www.mongodb.com/ ,点击右上角DownLoad下载按钮。
②选择你要下载的系统和版本,之后点击DownLoad。
③之后会跳转到注册页面,可以忽略,因为过一会就会开始下载。
可忽略注册步骤
可看到注册页面上方的提示信息。
开始下载
2.下载完成后双击安装。
①默认安装在C盘以MongoDB为文件名的目录中。
②目录下的文件
其中bin就是最重要的一个文件。bin下面的文件如下(没有全部介绍,现在开始学习只会用到mongo.exe和mongod.exe):
3.创建目录。
①在E盘创建如下MongoDB-FirstTest文件夹。
②在MongoDB-FirstTest文件夹下创建相应的目录结构。
③找到之前安装在C盘的MongoDB文件目录,拷贝bin目录下的mongo.exe和mongod.exe到E:\MongoDB-FirstTest\bin
④在E:\MongoDB-FirstTest\conf文件夹下创建配置文件
写入mongod.conf的文件内容如下:
③data文件夹和log文件夹只创建即可 ,启动服务器后会自动生成一系列数据。
4. 启动mongodb服务器。
①打开window命令行工具进入E:\MongoDB-FirstTest\bin
②启动mongodb服务器,启动时使用-f指定我们刚才写的配置文件目录。
如果我们不配置日志文件,则启动之后的信息就会在输入启动命令之后输出在控制台上。因为我们配置了日志文件,所以启动信息会记录在日志文件中。
5.查看启动服务器后生成的日志文件和数据文件。
(1)日志文件
①在第四步启动服务器之后,E:\MongoDB-FirstTest\conf目录下会生成mongod.log文件
②文件内容如下(可以看看红色加粗部分的内容):
2016-08-12T13:30:12.103+0800 I CONTROL [main] Hotfix KB2731284 or later update is not installed, will zero-out data files
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] MongoDB starting : pid=2452 port=12345 dbpath=E:\MongoDB-FirstTest\data 64-bit host=hjj-PC
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] db version v3.2.8
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] git version: ed70e33130c977bda0024c125b56d159573dbaf0
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1p 9 Jul 2015
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] allocator: tcmalloc
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] modules: none
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] build environment:
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] distmod: 2008plus-ssl
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] distarch: x86_64
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] target_arch: x86_64
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] options: { config: "E:\MongoDB-FirstTest\conf\mongod.conf", net: { port: 12345 }, storage: { dbPath: "E:\MongoDB-FirstTest\data" }, systemLog: { destination: "file", path: "E:\MongoDB-FirstTest\log\mongod.log" } }
2016-08-12T13:30:12.113+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2016-08-12T13:30:15.061+0800 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-08-12T13:30:15.062+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory 'E:/MongoDB-FirstTest/data/diagnostic.data'
2016-08-12T13:30:15.795+0800 I NETWORK [initandlisten] waiting for connections on port 12345
(2)数据文件
在第四步启动服务器之后,E:\MongoDB-FirstTest\data目录下会生成如下文件
7.连接mongodb服务器。
①打开window命令行工具进入E:\MongoDB-FirstTest\bin
②连接服务器 mongo 127.0.0.1:12345
红色部分为连接命令
白色部分为连接成功后的提示
③连接成功后在去查看日志文件会发现最后一行内容
2016-08-12T13:33:54.647+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58238 #1 (1 connection now open)
8.关闭客户端连接
ctrl+c 即可关闭客户端连接。
再去查看日志文件(红色部分)
2016-08-12T13:30:12.103+0800 I CONTROL [main] Hotfix KB2731284 or later update is not installed, will zero-out data files
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] MongoDB starting : pid=2452 port=12345 dbpath=E:\MongoDB-FirstTest\data 64-bit host=hjj-PC
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] db version v3.2.8
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] git version: ed70e33130c977bda0024c125b56d159573dbaf0
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1p 9 Jul 2015
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] allocator: tcmalloc
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] modules: none
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] build environment:
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] distmod: 2008plus-ssl
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] distarch: x86_64
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] target_arch: x86_64
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] options: { config: "E:\MongoDB-FirstTest\conf\mongod.conf", net: { port: 12345 }, storage: { dbPath: "E:\MongoDB-FirstTest\data" }, systemLog: { destination: "file", path: "E:\MongoDB-FirstTest\log\mongod.log" } }
2016-08-12T13:30:12.113+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2016-08-12T13:30:15.061+0800 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-08-12T13:30:15.062+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory 'E:/MongoDB-FirstTest/data/diagnostic.data'
2016-08-12T13:30:15.795+0800 I NETWORK [initandlisten] waiting for connections on port 12345
2016-08-12T13:33:54.647+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58238 #1 (1 connection now open)
2016-08-12T14:27:48.123+0800 I NETWORK [conn1] end connection 127.0.0.1:58238 (0 connections now open)
9.关闭服务器
ctrl+c 即可关闭服务器。
再去查看日志文件(红色部分)
2016-08-12T13:30:12.103+0800 I CONTROL [main] Hotfix KB2731284 or later update is not installed, will zero-out data files
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] MongoDB starting : pid=2452 port=12345 dbpath=E:\MongoDB-FirstTest\data 64-bit host=hjj-PC
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] db version v3.2.8
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] git version: ed70e33130c977bda0024c125b56d159573dbaf0
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1p 9 Jul 2015
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] allocator: tcmalloc
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] modules: none
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] build environment:
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] distmod: 2008plus-ssl
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] distarch: x86_64
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] target_arch: x86_64
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] options: { config: "E:\MongoDB-FirstTest\conf\mongod.conf", net: { port: 12345 }, storage: { dbPath: "E:\MongoDB-FirstTest\data" }, systemLog: { destination: "file", path: "E:\MongoDB-FirstTest\log\mongod.log" } }
2016-08-12T13:30:12.113+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2016-08-12T13:30:15.061+0800 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-08-12T13:30:15.062+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory 'E:/MongoDB-FirstTest/data/diagnostic.data'
2016-08-12T13:30:15.795+0800 I NETWORK [initandlisten] waiting for connections on port 12345
2016-08-12T13:33:54.647+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58238 #1 (1 connection now open)
2016-08-12T14:27:48.123+0800 I NETWORK [conn1] end connection 127.0.0.1:58238 (0 connections now open)
2016-08-12T14:30:03.952+0800 I CONTROL [thread1] Ctrl-C signal
2016-08-12T14:30:03.953+0800 I CONTROL [consoleTerminate] got CTRL_C_EVENT, will terminate after current cmd ends
2016-08-12T14:30:03.953+0800 I FTDC [consoleTerminate] Shutting down full-time diagnostic data capture
2016-08-12T14:30:04.087+0800 I CONTROL [consoleTerminate] now exiting
2016-08-12T14:30:04.095+0800 I NETWORK [consoleTerminate] shutdown: going to close listening sockets...
2016-08-12T14:30:04.095+0800 I NETWORK [consoleTerminate] closing listening socket: 388
2016-08-12T14:30:04.096+0800 I NETWORK [consoleTerminate] shutdown: going to flush diaglog...
2016-08-12T14:30:04.096+0800 I NETWORK [consoleTerminate] shutdown: going to close sockets...
2016-08-12T14:30:04.107+0800 I STORAGE [consoleTerminate] WiredTigerKVEngine shutting down
2016-08-12T14:30:06.037+0800 I STORAGE [consoleTerminate] shutdown: removing fs lock...
2016-08-12T14:30:06.038+0800 I CONTROL [consoleTerminate] dbexit: rc: 12