Mongodb 探索

简介

MongoDB是一个跨平台,面向文档的数据库,提供高性能,高可用性和易于扩展。MongoDB是工作在集合和文档上一种概念。

特点

  • 支持特别查询在MongoDB中,可以通过字段,范围查询进行搜索,并且还支持正则表达式搜索。
  • 索引可以索引文档中的任何字段。
  • 复制MongoDB支持主从复制。主机可以执行读写操作,从机从主机复制数据,只能用于读取或备份(不写入)
  • 复制数据MongoDB可以在多台服务器上运行。 复制数据以保持系统正常运行,并在硬件故障的情况下保持其运行状态。
  • 负载均衡由于数据放在碎片中,因此具有自动负载平衡配置。
  • 支持映射缩减和聚合工具
  • 使用JavaScript而不是Procedure
  • 它是一个用C++编写的无模式数据库
  • 提供高性能
  • 轻松存储任何大小的文件,而不会使您的堆栈复杂化
  • 在故障的情况下易于管理
  • 它还支持:
    • 具有动态模式的JSON数据模型
    • 自动分片用于水平可扩展性
    • 内置复制高可用性

优点

  • MongoDB 的架构较少。它是一个文档数据库,它的一个集合持有不同的文档。
  • 从一个到另一个的文档的数量,内容和大小可能有差异。
  • MongoDB 中单个对象的结构很清淅。
  • MongoDB 中没有复杂的连接。
  • MongoDB 提供深度查询的功能,因为它支持对文档的强大的动态查询。
  • MongoDB 很容易扩展。
  • 它使用内部存储器来存储工作集,这是其快速访问的原因。

场景

  • 大而复杂的数据
  • 移动和社会基础设施数据
  • 内容管理和交付
  • 用户数据管理
  • 数据中心

安装

1
2
3
4
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz    # 下载
tar -zxvf mongodb-linux-x86_64-3.0.6.tgz # 解压

mv mongodb-linux-x86_64-3.0.6/ /usr/local/mongodb # 将解压包拷贝到指定目录

MongoDB 的可执行文件位于 bin 目录下,所以可以将其添加到 PATH 路径中:

1
export PATH=<mongodb-install-directory>/bin:$PATH

为你 MongoDB 的安装路径。如本文的 /usr/local/mongodb 。

创建数据库目录

MongoDB的数据存储在data目录的db目录下,但是这个目录在安装过程不会自动创建,所以你需要手动创建data目录,并在data目录中创建db目录。

以下实例中我们将data目录创建于根目录下(/)。

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

1
mkdir -p /data/db

运行

可以在命令行中执行mongo安装目录中的bin目录执行mongod命令来启动mongdb服务。如果你的数据库目录不是/data/db,可以通过 –dbpath 来指定。

1
$ ./mongod

启动参数

  • –dbpath 指定数据目录,默认是/data/db,每个mongod进程都需要独立的数据目录
  • –port 指定服务器监听端口,默认27017
  • –fork 以守护进程的方式运行服务
  • –logpath 指定输出日志的路径,而不是输出到命令行
  • –config 指定配置文件

停止

  • 查看进程,使用kill命令
  • 在客户端进去,使用db.shutdownServer()

使用

1
2
show dbs;
#查看数据库
1
2
use wesure;
#切换或者创建(如果没有该库的话)
1
2
db.getName();
#db; db和getName方法是一样的效果,都可以查询当前使用的数据库
1
2
db.stats();
#查看数据库状态

用户相关

1、添加一个用户

1
2
3
db.addUser("name");

db.addUser("userName", "pwd123", true); 添加用户、设置密码、是否只读

2、数据库认证、安全模式

1
db.auth("userName", "123123");

3、显示当前所有用户

1
show users;

4、删除用户

1
db.removeUser("userName");

其他

  • 查询之前的错误信息
    1
    db.getPrevError();
  • 清除错误记录
    1
    db.resetError();

克隆

使用copyDatabase命令实现数据库复制,可以在几秒内创建数据库副本。

1
db.copyDatabase(fromdb, todb, fromhost, username, password, mechanism)¶
1
2
3
4
5
6
7
[root@gz-tencent mongo]# mongo localhost:27018
> db.copyDatabase("backup","backup","xxx.x.x.x:27017");
WARNING: db.copyDatabase is deprecated. See http://dochub.mongodb.org/core/copydb-clone-deprecation
{
"note" : "Support for the copydb command has been deprecated. See http://dochub.mongodb.org/core/copydb-clone-deprecation",
"ok" : 1
}

注意,执行后不会立刻展示正确的容量大小,取决于数据库实际的大小和网速。另外4.0以后的版本以及不能使用该命令了。

IMPORTANT
Starting in version 4.2, MongoDB removes the copydb command. The deprecated db.copyDatabase(), which wraps the copydb command, can only be run against MongoDB 4.0 or earlier versions. For behavior and examples, refer to the 4.0 or earlier version of the manual.
For an alternative in version 4.2+, see Copy/Clone a Database.

4.2 copy/clone

As an alternative, users can use mongodump and mongorestore (with the mongorestore options –nsFrom and –nsTo).

For example, to copy the test database from a local instance running on the default port 27017 to the examples database on the same instance, you can:

  1. Use mongodump to dump the test database to an archive mongodump-test-db:
1
mongodump --archive="mongodump-test-db" --db=test
  1. Use mongorestore with –nsFrom and –nsTo to restore (with database name change) from the archive:
1
mongorestore --archive="mongodump-test-db" --nsFrom='test.*' --nsTo='examples.*'

简单来说分为两步,首先dump导出,然后restore恢复,也可以指定源空间和目标空间。

namespace

The canonical name for a collection or index in MongoDB. The namespace is a combination of the database name and the name of the collection or index, like so: [database-name].[collection-or-index-name]. All documents belong to a namespace. See Namespaces.

规范的命名是集合或者索引在MongoDB中。命名空间是由数据库名和集合或者索引共同组成。

database

A physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.

一种存放集合的物理容器。每个数据库在文件系统都有自己的文件。单个MongoDB服务可以有多个数据库。

journal

A sequential, binary transaction log used to bring the database into a valid state in the event of a hard shutdown. Journaling writes data first to the journal and then to the core data files. MongoDB enables journaling by default for 64-bit builds of MongoDB version 2.0 and newer. Journal files are pre-allocated and exist as files in the data directory. See Journaling.

一种连续的,二进制不间断的日志用来数据库有效状态记录事件在硬件宕机的时候。

数据文件类型

  1. journal 日志文件
  2. namespace 表名文件
  3. data 数据及索引文件

参考

  1. https://docs.mongodb.com/manual/introduction/