Introduction to the use of MongoDB

1. Introduction to MongoDB

MongoDB is written in C++ language and is a non-relational open source database system based on distributed file storage. Its advantage is that it can store massive data, has powerful query function, and is an independent collection-oriented document form.

  • Application platform: MongoDB supports system platforms Unix Linux Windowssuch as .

Differences between MySQL and MongoDB:

MySQL database data sheet data
MongDB database gather Documentation

1. MongoDB Features

Storage:

  • Set-oriented : Data is grouped and stored in a dataset, called a set.
  • Document-oriented : Documents stored in collections are stored as key-value pairs.
  • Efficient binary data storage : Using binary format storage, any type of data object can be saved.

Operability:

  • Fully indexed : You can index on arbitrary properties, including internal objects. to improve query speed.
  • Powerful aggregation tools : In addition to providing rich query functions, MongoDB also provides powerful aggregation tools, such count groupas .
  • Drivers that support Perl PHP Java C# JavaScript Ruby Python Cand languages.C++

Availability:

  • Support replication and data recovery : MongoDB supports master-slave replication mechanism, which can realize data backup, failure recovery, read expansion and other functions. The replica set-based replication mechanism provides automatic failure recovery to ensure that cluster data will not be lost.
  • Automatically handle sharding : MongoDB supports clusters to automatically shard data. To shard data, the cluster can be used to store more data, achieve greater load, and ensure load balancing of storage.

2. MongoDB applicable scenarios

  1. Website real-time data processing : It is very suitable for real-time insertion, update and query, and has the replication and high scalability required for website real-time data storage.
  2. Caching : It is suitable as a caching layer for information infrastructure. After the system restarts, the persistent cache layer built by it can avoid overloading the underlying data sources.
  3. High scalability scenarios : Ideal for databases consisting of dozens or hundreds of servers.

Scenarios that do not apply are as follows:

  • Systems requiring a highly transactional nature (e.g. banking and accounting systems)
  • Traditional business can only apply.
  • Complex cross-document (table) cascading queries.

3. MongoDB storage structure

Logical structure:

  • Documents document: Stored data.
  • Collection collection: consists of multiple documents, equivalent to a table, but the difference is that the collection has no fixed schema.
  • Database database: A database consisting of multiple collections.

Physical structure:

  • .wtFile: Each table or index corresponds to a namespace, the amount of data increases, the number of files increases, and the allocated and in-use disk space is stored.
  • Data file: An entity that stores data and uses a pre-allocated space mechanism.
  • Log files: system log files, journallog files (for MongoDB crash recovery protection), oplogreplication operation log files (equivalent to MySQL's BinLog file), slow query logs (statements whose query operations exceed the specified time)

The physical structure is where the data is actually stored.

4. MongoDB data types

insert image description here

2. Deploy MongoDB database application

CPU name operating system IP address Version
MongoDB CentOS 7.4 192.168.1.1 mongodb-linux-x86_64-rhel70-4.4.5.tgz

1. Prepare the system environment

[root@MongoDB ~]# ulimit -n 25000									# 同一时间最多开启的文件数
[root@MongoDB ~]# ulimit -u 25000									# 用户最多开启的程序数目
[root@MongoDB ~]# echo 0 > /proc/sys/vm/zone_reclaim_mode			# 设置内核参数. 当某个节点内存不足时可以借用其它节点的内存
[root@MongoDB ~]# sysctl -w vm.zone_reclaim_mode=0
vm.zone_reclaim_mode = 0
[root@MongoDB ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@MongoDB ~]# echo never > /sys/kernel/mm/transparent_hugepage/defrag

Because the version installed below is , and the tools in the directory 4.4.5MongoDB version are separated, it needs to be installed: Portal4/bin
insert image description here

[root@MongoDB ~]# tar xf mongodb-database-tools-rhel70-x86_64-100.3.1.tgz 

2. Install MongoDB

[root@MongoDB ~]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.5.tgz
[root@MongoDB ~]# tar xf mongodb-linux-x86_64-rhel70-4.4.5.tgz
[root@MongoDB ~]# mv mongodb-linux-x86_64-rhel70-4.4.5 /usr/local/mongodb
[root@MongoDB ~]# mv mongodb-database-tools-rhel70-x86_64-100.3.1/bin/* /usr/local/mongodb/bin/
[root@MongoDB ~]# echo "export PATH=/usr/local/mongodb/bin:\$PATH" >> /etc/profile
[root@MongoDB ~]# source /etc/profile

3. Create MongoDB storage directory and configuration file

[root@MongoDB ~]# mkdir /usr/local/mongodb/{
    
    data,logs,conf}
[root@MongoDB ~]# touch /usr/local/mongodb/logs/mongodb.log
[root@MongoDB ~]# chmod 777 /usr/local/mongodb/logs/mongodb.log 
[root@MongoDB ~]# cat <<END > /usr/local/mongodb/conf/mongodb.conf
bind_ip=192.168.1.1
port=27017
dbpath=/usr/local/mongodb/data/
logpath=/usr/local/mongodb/logs/mongodb.log
logappend=true																	# 日志以文件追加的方式写入
fork=true																		# 通过后台运行 MongoDB 服务
maxConns=5000																	# MongoDB 最大连接数
END

4. Write a service startup script

[root@MongoDB ~]# mongod -f /usr/local/mongodb/conf/mongodb.conf				# 启动 MongoDB 服务
[root@MongoDB ~]# mongo 192.168.1.1:27017										# 登录验证
[root@MongoDB ~]# mongod -f /usr/local/mongodb/conf/mongodb.conf --shutdown		# 关闭 MongoDB 服务
[root@MongoDB ~]# vim /etc/init.d/mongodb
#!/bin/bash
case "$1" in
start)
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf;;
stop)
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf --shutdown;;
restart)
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf --shutdown
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf;;
esac
[root@MongoDB ~]# chmod +x /etc/init.d/mongodb 									# 添加执行权限
[root@MongoDB ~]# /etc/init.d/mongodb start										# 启动

Login authentication

[root@MongoDB ~]# mongo 192.168.1.1:27017										# 登录 MongoDB 数据库 (默认在 test 库中)
> show databases					# 查看数据库中所有库
admin   0.000GB
config  0.000GB
local   0.000GB
> db.getName()						# 查看当前登录库
test

The default 4 libraries after MongoDB is installed:

  • admin: Stores the relevant information about the database account.
  • config: Used in a sharded cluster environment to store shard-related metadata information.
  • local: Used to store arbitrary collections limited to a single local server (because the library is not replicated to slave nodes, in short, there is no redundancy)
  • test: A test library created by MongoDB by default. When connecting to the MongoDB service, if you do not specify a specific database to connect to, the testlibrary .

Guess you like

Origin blog.csdn.net/weixin_46902396/article/details/117782901