初识MongDB
随时都要保持求知欲!
一、什么是MongDB
MongDB的官网
什么是MongoDB?
MongoDB是一个文档数据库,具有您想要的查询和索引所需的可扩展性和灵活性。
MongoDB 将数据存储在灵活的,类似JSON的文档中,这意味着字段可能因文档而异,数据结构可随时间变化
文档模型映射到应用程序代码中的对象,使数据易于使用
即席查询,索引和实时聚合提供了访问和分析数据的强大方法
MongoDB是一个分布式数据库,它的核心是高可用性,横向扩展和地理分布,并且易于使用
MongoDB是免费且开源的,在GNU Affero General Public License下发布
二、MongoDB的架构
MongoDB架构官方文档
讲的非常好,有兴趣的同学可以自行前往多了解一下。
三、安装MongoDB
官方不同操作系统安装网址
在上面的网址中,你可以根据你的系统的不同选择不同的方式安装。
四、Mac安装MongoDB
下面以Mac系统安装为例:
Mac系统安装教程
使用Homebrew安装MongoDB。
Homebrew installs binary packages based on published “formulae.” This section describes how to update brew to the latest packages and install MongoDB Community Edition. Homebrew requires some initial setup and configuration, which is beyond the scope of this document.
1.升级Homebrew’s
Update Homebrew’s package database.
In a system shell, issue the following command:
brew update
2.安装MongoDB
You can install MongoDB via brew with several different options. Use one of the following operations:
brew install mongodb
五、运行MongoDB
1.创建MongoDB的数据库文件夹
Before you start MongoDB for the first time, create the directory to which the mongod process will write data. By default, the mongod process uses the /data/db directory. If you create a directory other than this one, you must specify that directory in the dbpath option when starting the mongod process later in this procedure.
The following example command creates the default /data/db directory:
mkdir -p /data/db
2.给文件夹设置权限
Before running mongod for the first time, ensure that the user account running mongod has read and write permissions for the directory.(必须得有可读可写权限)
- 我们在这里给予所有的用户的可读可写权限
/data ⌚ 15:34:06
$ ll
total 0
drwxr-xr-x 2 root wheel 64B Jun 8 15:33 db
/data ⌚ 15:34:07
$ chmod 777 /data/db
chmod: Unable to change file mode on /data/db: Operation not permitted
/data ⌚ 15:35:48
$ sudo chmod 777 /data/db
/data ⌚ 15:35:57
$ ll
total 0
drwxrwxrwx 2 root wheel 64B Jun 8 15:33 db
/data ⌚ 15:35:59
$
3.运行MongoDB
To run MongoDB, run the mongod process at the system prompt. If necessary, specify the path of the mongod or the data directory. See the following examples.(两种启动方式:你如果使用的默认db的文件夹,就可以默认启动,还有一种需要结合db的位置启动)
- Run without specifying paths
If your system PATH variable includes the location of the mongod binary and if you use the default data directory (i.e., /data/db), simply enter mongod at the system prompt:(默认db位置,默认启动方式)
mongod
- Specify the path of the mongod
If your PATH does not include the location of the mongod binary, enter the full path to the mongod binary at the system prompt:(二进制文件启动)
<path to binary>/mongod
- Specify the path of the data directory
If you do not use the default data directory (i.e., /data/db), specify the path to the data directory using the –dbpath option:(使用自定义的文件夹的方式启动)
mongod --dbpath <path to data directory>
4.测试MongoDB是否安装成功
有以下的输出就说明启动成功了:
[initandlisten] waiting for connections on port 27017
5.开始使用MongoDB
Start a mongo shell on the same host machine as the mongod. Use the –host command line option to specify the localhost address and port that the mongod listens on:
方式一:
mongo
方式二
mongo --host 127.0.0.1:27017
六、后台启动OR杀死程序(以MongoDB为例)
1.后台启动MongoDB
nohup + 服务的启动项 + &
3913 是服务的启动端口
$ nohup mongod &
[1] 3913
appending output to nohup.out
2.连接MongoDB
$ mongo
MongoDB shell version v3.6.5
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.5
Server has startup warnings:
2018-06-08T16:01:00.163+0800 I CONTROL [initandlisten]
2018-06-08T16:01:00.163+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-06-08T16:01:00.163+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-06-08T16:01:00.163+0800 I CONTROL [initandlisten]
2018-06-08T16:01:00.163+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
3.查看nohup正在运行的后台程序
3913:服务的端口
mongod:服务
$ jobs -l
[1] + 3913 running nohup mongod
4.杀死这个端口
$ kill -9 3913
[1] + 3913 killed nohup mongod
~ ⌚ 16:07:33
$
好啦,现在MongoDB的安装是连接使用到此就结束的。