MongoDB安装与基础使用

前期调研发现,如果对未来项目存储数据的架构无法预测,最好使用MongoDB数据库,非常适合半结构化数据。目前面对自己打算建立一个个人博客,并不知道未来数据存储是何种类型何种结构,因此先从部署MongoDB开始

安装

下载与安装:

MongoDB 提供了 linux 各发行版本 64 位的安装包,你可以在官网下载安装包。 https://www.mongodb.com/download-center/enterprise
在这里插入图片描述
在该页面中选取社区版,在选择适合自己的系统版本,点击Download,在下面出现下载连接,然后在系统中命令行执行如下操作。

# 下载
$ wget -c https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.1.tgz
# 解压
$ tar -zxvf mongodb-linux-x86_64-rhel70-4.2.1.tgz
# 设置环境变量
$ sudo cp bin/* /usr/bin/
# 添加环境便可配置 ~/.bash_profile 文件
# 文件中添加 PATH=<你解压的路径>/bin:$PATH

配置

配置数据库目录和log日志目录

注意:/data/db 是 MongoDB 默认的启动的数据库路径(–dbpath)

$ mkdir -p /data/software/MongoDB/db
$ mkdir -p /data/software/MongoDB/log
# 给予目录可写权限
$ chmod a+w /data/software/MongoDB/db
$ chmod a+w /data/software/MongoDB/log
运行 MongoDB 服务
$ mongod --dbpath /data/software/MongoDB/db --logpath /data/software/MongoDB/log/mongodb.log --fork

about to fork child process, waiting until server is ready for connections.
forked process: 9752
child process started successfully, parent exiting

正常启动查看释放服务运行成功

$ mongo

MongoDB shell version v4.2.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("8f4d7ca3-362d-412a-8147-4f1ad9a08126") }
MongoDB server version: 4.2.1
Server has startup warnings:
2019-11-13T22:59:48.584+0800 I  STORAGE  [initandlisten]
... ...

基础使用

shell后台运行

如果你需要进入MongoDB后台管理,直接执行mongo命令。
MongoDB Shell是MongoDB自带的交互式Javascript shell,用来对MongoDB进行操作和管理的交互式环境。
由于它是一个JavaScript shell,您可以运行一些简单尝试:

> 1+2+3
6
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

创建数据库

use DATABASE_NAME
如果数据库不存在,则创建数据库,否则切换到指定数据库。

> use mylog
switched to db mylog
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
>

使用show dbs 命名可显示数据库list,而刚才所创建的“mylog”数据库并没有显现,需要向其中插入数据。

> db.mylog.insert({"myname":"xxx", "age":22})
WriteResult({ "nInserted" : 1 })
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
mylog   0.000GB
> db.mylog.find()
{ "_id" : ObjectId("5dcc203551ca4bcb1d7c68ec"), "myname" : "xxx", "age" : 22 }

使用客户端连接MongoDB

在正式处理和操作数据库时,一般不会使用后台命令行执行操作,在此我们使用 Robo 3T 客户端连接数据库执行数据库的操作。

连接

正常启动MongoDB服务,服务默认的IP是机器的内网IP(好像应该是这样),外部是无法访问的,

$ netstat -tunlp | grep mongod

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      4653/mongod

我们需要设置成ip的集合0.0.0.0,此时我们需要关闭服务,使用–bind_ip_all参数重启,只有IP地址就变成0.0.0.0

$ mongod --dbpath /data/software/MongoDB/db --logpath /data/software/MongoDB/log/mongodb.log --fork --bind_ip_all

about to fork child process, waiting until server is ready for connections.
forked process: 4861
ERROR: child process failed, exited with error number 48
To see additional information in this output, start without the "--fork" option.

$ netstat -tunlp | grep mongod

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      5145/mongod            LISTEN      4653/mongod

在客户端填写你服务器的公网地址后,尝试连接,出现Error。
在这里插入图片描述
在Authentication栏中取消Perform authentication 即可。
在这里插入图片描述

使用

一般都是对表对操作

  • 插入 insert
  • 更新 update
  • 删除 remove
  • 查询 find
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值