一、MongoDB简介
1.MongoDB是为快速开发互联网Web应用而设计的数据库系统。
2.MongoDB的设计目标是极简、灵活、作为Web应用栈的一部分。
3.MongoDB的数据模型是面向文档的,所谓文档是一种类似于JSON的结构,简单理解
MongoDB这个数据库中存的是各种各样的JSON。(BSON)
三个概念:
数据库(database):数据库是一个仓库,在仓库中可以存放集合。
集合(collection):集合类似于数组,在集合中可以存放文档。
文档(document):文档数据库中的最小单位,我们存储和操作的内容都是文档。
基本概念
1.文档(document):类似于JS中的对象,在MongoDB中每一条数据都是一个文档;
2.集合(collection):集合就是一组文档,也就是集合是用来存放文档的,集合中存储的文档可以是各种各样的,没有格式要求;
3.多个文档组成集合,多个集合组成数据库
二、软件安装过程:windows与linux
下载地址: https://www.mongodb.org/dl/win32/
注意:
1.MongoDB的版本偶数版本为稳定版,奇数版本为开发版。
2.MongoDB对于32位系统支持不佳,所以3.2版本以后没有再对32位系统的支持。
windows安装:
1.选择自定义安装方式,设置安装路径,然后下一步直至结束
2.需要配置path环境变量,按照配置java的方式一样即可
MONGODB_HOME = D:\java\MongoDB\Server\3.2 我这里配置了一个变量
%MONGODB_HOME%\bin;
3.在C盘根目录下创建data文件夹,在data下创建db文件夹 ,这个路径是默认路径,也可以在启动mongod时修改此路径
4.打开CMD命令行窗口,输入mongod
C:\Users\Administrator>mongod 32位的可能会比较麻烦,有可能报错
5.指定端口和路径
–dbpath:data文件夹的路径
–port 123:修改默认端口号
C:\Users\Administrator>mongod --dbpath C:\Users\Mrchen\Desktop\mongo\data\db --port 123
注意:打开的命令行窗口不能关闭,窗口关闭则服务停止
6.停止mongod服务: Ctrl + C
7.配置mongo的windows服务,不用一直开着命令窗口了。
-
在mongo安装目录的server下3.x目录下创建mongod.cfg文件,并添加如下内容
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
-
管理员模式打开控制台,并输入如下指令
mongo bin路径=安装路径从盘符开始到bin下
mongo路径 = 安装路径到server下3.x目录
sc.exe create MongoDB binPath= "\"mongo bin路径\mongod.exe\" --service -- config=\"mongo路径\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"
-
删除服务 sc delete MongoDB(服务名称)
删除后的服务如果机器没有重启需要去删除注册表,然后重新打开服务管理此时服务就会被清除,方可重新建立服务
linux环境安装
1.下载MongDB的linux版本
2.解压tar包
3.创建data/db 文件夹
4. 进入到解压后的MongoDB 的可执行文件位于 bin 目录下,所以可以将其添加到 PATH 路径中:
export PATH=<mongodb-install-directory>/bin:$PATH
<mongodb-install-directory> 为你 MongoDB 的安装路径。如本文的 /usr/local/mongodb
5.启动mongodb
5.1 如在 / 根目录下创建的 data/db文件夹可直接在bin下启动
./mongod 直接运行可执行文件启动mongodb
5.2 设置自定义 data/db目录
如设置目录: /opt/mogodb/data
启动方式也在bin下:
[root@instance-45oh6r0z bin]# ./mongod --dbpath /opt/mogodb/data
//以下是启动日志
2018-12-05T21:12:43.829+0800 I CONTROL [initandlisten] MongoDB starting : pid=115999 port=27017 dbpath=/opt/mogodb/data 64-bit host=instance-45oh6r0z
2018-12-05T21:12:43.829+0800 I CONTROL [initandlisten] db version v4.0.4
2018-12-05T21:12:43.829+0800 I CONTROL [initandlisten] git version: f288a3bdf201007f3693c58e140056adf8b04839
2018-12-05T21:12:43.829+0800 I CONTROL [initandlisten] allocator: tcmalloc
2018-12-05T21:12:43.829+0800 I CONTROL [initandlisten] modules: none
2018-12-05T21:12:43.829+0800 I CONTROL [initandlisten] build environment:
2018-12-05T21:12:43.829+0800 I CONTROL [initandlisten] distarch: x86_64
2018-12-05T21:12:43.829+0800 I CONTROL [initandlisten] target_arch: x86_64
2018-12-05T21:12:43.829+0800 I CONTROL [initandlisten] options: { storage: { dbPath: "/opt/mogodb/data" } }
2018-12-05T21:12:43.830+0800 I STORAGE [initandlisten] Detected data files in /opt/mogodb/data created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2018-12-05T21:12:43.830+0800 I STORAGE [initandlisten]
2018-12-05T21:12:43.830+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-12-05T21:12:43.830+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-12-05T21:12:43.830+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1454M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2018-12-05T21:12:45.094+0800 I STORAGE [initandlisten] WiredTiger message [1544015565:94589][115999:0x7f32ea4d0a40], txn-recover: Main recovery loop: starting at 1/32768 to 2/256
2018-12-05T21:12:45.215+0800 I STORAGE [initandlisten] WiredTiger message [1544015565:215921][115999:0x7f32ea4d0a40], txn-recover: Recovering log 1 through 2
2018-12-05T21:12:45.285+0800 I STORAGE [initandlisten] WiredTiger message [1544015565:285372][115999:0x7f32ea4d0a40], txn-recover: Recovering log 2 through 2
2018-12-05T21:12:45.340+0800 I STORAGE [initandlisten] WiredTiger message [1544015565:340728][115999:0x7f32ea4d0a40], txn-recover: Set global recovery timestamp: 0
2018-12-05T21:12:45.354+0800 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten]
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten]
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten]
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten]
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten]
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten]
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 15651 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2018-12-05T21:12:45.366+0800 I CONTROL [initandlisten]
2018-12-05T21:12:45.383+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/opt/mogodb/data/diagnostic.data'
2018-12-05T21:12:45.384+0800 I NETWORK [initandlisten] waiting for connections on port 27017
- 命令行启动客户端
1.直接在bin下启动
[root@instance-45oh6r0z bin]# ./mongo
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("95ccfb21-5960-4ed0-9fae-0ecbd234601c") }
MongoDB server version: 4.0.4
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
Server has startup warnings:
2018-12-05T21:03:43.065+0800 I STORAGE [initandlisten]
2018-12-05T21:03:43.065+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-12-05T21:03:43.065+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-12-05T21:03:43.795+0800 I CONTROL [initandlisten]
2018-12-05T21:03:43.795+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-12-05T21:03:43.795+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-12-05T21:03:43.795+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-12-05T21:03:43.795+0800 I CONTROL [initandlisten]
2018-12-05T21:03:43.795+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2018-12-05T21:03:43.795+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2018-12-05T21:03:43.795+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
2018-12-05T21:03:43.795+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2018-12-05T21:03:43.795+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2018-12-05T21:03:43.795+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2018-12-05T21:03:43.795+0800 I CONTROL [initandlisten]
2018-12-05T21:03:43.796+0800 I CONTROL [initandlisten]
2018-12-05T21:03:43.796+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-12-05T21:03:43.796+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-12-05T21:03:43.796+0800 I CONTROL [initandlisten]
2018-12-05T21:03:43.796+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-12-05T21:03:43.796+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-12-05T21:03:43.796+0800 I CONTROL [initandlisten]
2018-12-05T21:03:43.796+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 15651 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2018-12-05T21:03:43.796+0800 I CONTROL [initandlisten]
//2.以上是启动日志,启动成功则进入如下,显示所有数据库
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
//3. 使用test 数据库,如test数据库不存在也可以使用,当插入数据时自动创建
> use test
switched to db test
//4.显示当前数据库
> db
test
//5.插入一条数据
> db.test.insert({name:"张三",age:18})
WriteResult({ "nInserted" : 1 })
//6.查看数据
> db.test.find();
{ "_id" : ObjectId("5c07cd459058f73ce8af5573"), "name" : "张三", "age" : 18 }
>
安装过程还是比较简单:主要是windows注册进服务可能由于32位操作系原因会有问题;