MongoDB 整理

11 篇文章 0 订阅

简介

MongoDB是一个面向文档的非关系型数据库(NoSQL),使用json格式存储,在Mongo DB中 每一条记录都是一个Document对象,所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作

  1. MongoDB是一个单独的服务器;
  2. MongoDB是基于文档,而不是基于表;
  3. MongoDB中较少的Schema;
  4. 你不需要学习另外的语言;
  5. MongoDB有良好的PHP支持。

MongoDB 区别于关系型数据库。

  • 优点:适用于三高的场景,对海量数据比较友好,没有join,没有事务,使用内存映射,速度快
  • 缺点:不支持事务。对于一些对事务有要求的场景,还是要使用关系型数据库

适用场景

存储大尺寸、低价值的数据
json及对象类型数据
网站缓存数据
评论、子评论类有明显从属关系数据
多服务器数据,其内置的MapReduce很容易现实全局遍历。

  • 适用选择
    适用选择
  • 具体场景:
    在这里插入图片描述

安装

Linux平台安装MongoDB

  1. Linux 平台依赖包
sudo yum install libcurl openssl
  1. 官网下载 解压 tgz
    mongodb4
    选择相应版本下载
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.4/x86_64/RPMS/mongodb-org-mongos-4.4.0-1.el7.x86_64.rpm
tar -zxvf mongodb-linux-x86_64-rhel70-4.4.0.tgz
mv mongodb-linux-x86_64-rhel70-4.4.0 /usr/local/mongodb4
  1. MongoDB 的可执行文件位于 bin 目录下,所以可以将其添加到 PATH 路径中
vim /etc/profile
#最后一行新增一行
export PATH=/usr/local/mongodb4/bin:$PATH
source /etc/profile
  1. 创建数据库目录
  • 数据存储目录:/var/lib/mongodb
  • 日志文件目录:/var/log/mongodb
sudo mkdir -p /var/lib/mongo
sudo mkdir -p /var/log/mongodb
sudo chown `whoami` /var/lib/mongo     # 设置权限
sudo chown `whoami` /var/log/mongodb   # 设置权限
  1. 两种方法启动
  • 直接启动
cd /usr/local/mongodb4/bin
mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork
  • 配置文件

新增mongodb.conf文件,内容

dbpath=/var/lib/mongo   #数据文件存放目录
logpath=/var/log/mongodb/mongodb.log  #日志文件存放目录
port=27017 #端口
bind_ip = 0.0.0.0 #外网访问
#auth=true
fork=true #以守护程序的方式启用,即在后台运行
#journal=false
#storageEngine=mmapv1

执行修复命令:./mongod -f mongodb.conf --repair

./mongod -f mongodb.conf --repair

配置文件启动

#bin目录下 
mongod -f mongodb.conf
#显示  启动成功
about to fork child process, waiting until server is ready for connections.
forked process: 25812
child process started successfully, parent exiting
  1. 测试是否启动成功:./mongo
#bin目录下
./mongo
#显示
MongoDB shell version v4.4.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f8de8d5f-22b9-4568-8d6b-3e062c325701") }
MongoDB server version: 4.4.0
---
The server generated these startup warnings when booting:
        2020-09-09T16:13:27.641+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2020-09-09T16:13:28.071+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2020-09-09T16:13:28.071+08:00: You are running this process as the root user, which is not recommended
        2020-09-09T16:13:28.071+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2020-09-09T16:13:28.071+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
>

Linux 上安装 MongoDB PHP 扩展

  1. 编译安装
$ wget http://pecl.php.net/get/mongodb-1.5.2.tgz
$ cd /mongodb-1.5.2
$ phpize
$ ./configure
$ make && make install
  1. 有信息提示,安装成功
Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20180731/

  1. 修改php.ini,添加mogodb配置
extension_dir=/usr/lib/php/extensions/debug-non-zts-20151012/
extension=mongodb.so
  1. php-m 查看mogodb 扩展
mongodb
mysqli
mysqlnd
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
redis
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
swoole
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlreader
xmlrpc
xmlwriter
xsl
yaconf
yaml
zip
zlib

[Zend Modules]

php think-orm 使用MongoDb

<?php
/**
 * Mongodb.php
 * User chenzhuo
 * Date 2020/9/9 4:41 下午
 * Description :
 */

namespace app\common\lib\nosql;
use think\facade\Db;
class Mongodb
{

    public $connection = null;

    /**
     * @var self
     */
    private static $self = null;


    /**
     * @return self
     */
    public static function getInstance()
    {
        if (self::$self == null) {
            self::$self = new self();
        }
        return self::$self;
    }
    public function __construct()
    {
        $this->connection = Db::connect("mongo");
    }

    // find查找
    public function find(string $table, string $id){
        $res = $this->connection->table($table)->find($id);
        return $res;
    }
    //field 全部查询
    public function select(string $table, string $field){

        $res = $this->connection->table($table)->field($field)->select();

        return $res;
    }
    //添加
    public function insert(string $table, array $data){

        $res = $this->connection->table($table)->insert($data);

        return $res;
    }
    //修改
    public function update(string $table, array $where, array $data){

        $res = $this->connection->table($table)->where($where)->update($data);

        return $res;
    }
    // 阻止外部clone
    private function __clone()
    {

    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
mongodb的磁盘回收可以通过备份恢复和删除数据来实现。备份恢复是一种常用的方法,可以使用mongodump命令将数据库备份到指定目录,然后使用mongorestore命令将备份数据恢复到数据库中。这样可以释放一部分磁盘空间。\[1\] 另外,需要注意的是,当删除数据时,mongodb并不会立即释放占用的磁盘空间给操作系统。即使使用drop collection命令删除集合,也不会释放给操作系统,除非使用drop database命令。在dbshell中使用db.serverStatus().mem命令可以看到磁盘空间占用显示已经释放,但是使用df命令查看操作系统的空闲磁盘空间时,可能并没有变化。这是因为mongodb会在数据文件中维护一个空记录列表,删除文档或集合后,mongodb会将空记录列表中的存储空间分配给新的文档。\[2\]\[3\] 因此,如果需要回收mongodb的磁盘空间,可以通过备份恢复和删除数据的方式来实现。 #### 引用[.reference_title] - *1* *2* [mongodb磁盘满故障](https://blog.csdn.net/sunny05296/article/details/106651111)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [mongodb磁盘碎片整理](https://blog.csdn.net/weixin_44375561/article/details/122728272)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值