MongoDB installation and common operation instructions

One, MongoDB overview

1.1: Introduction

  • MongoDB is a cross-platform, document-oriented database. Can achieve high performance, high availability, and can be easily extended. It is an open source database system based on distributed file storage. Under high load conditions, adding more nodes can ensure server performance.
  • MongoDB is also a product between relational and non-relational databases. It is the most versatile and most like relational database among non-relational databases. The main reason for not adopting the relational model is to obtain better scalability. MongoDB no longer has the concept of'row', and its operation mode is mainly based on two concepts: collection and document.

1.2: Features

  • Including collection-oriented storage, model freedom, rich query statements and multi-level indexes, replication set mechanism, easy horizontal expansion, pluggable storage engine, cross-platform multi-language support, etc.
  • Simple to install, provides document-oriented storage function, easier to operate
  • Provides replication, high availability, and automatic sharding functions. If the load increases (more storage space and stronger processing power are required), it can be distributed on other nodes in the computer network, which is called sharding
  • Support rich query expressions. The query command uses JSON format tags, which can easily query the embedded objects and arrays in the document
  • Support various programming languages: Puby, Python, Java, C++, PHP, C# and other languages

1.3: Applicable scenarios

  • MongoDB can provide scalable and high-performance data storage solutions for web applications. MongoDB is mainly used in website data, distributed scenarios, data caching, and JSON document format storage. It is suitable for Internet applications with large data volume, high concurrency, and multi-tasking. Its built-in horizontal expansion mechanism provides data processing capabilities ranging from several million to one billion, which can well meet the requirements of Web2.0 and mobile Internet applications. Claim

Two, MongoDB installation

  • Configure yum source warehouse
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# vim mongodb-org.repo
[mangodb-org]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1		'开启检查'
enabled=1		'开启'
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
[root@localhost yum.repos.d]# yum list	'重新加载一下'
  • Install MongoDB
[root@localhost yum.repos.d]# yum install mongodb-org -y	'安装mongodb工具包'
[root@localhost yum.repos.d]# vim /etc/mongod.conf 
net:
  port: 27017  
  bindIp: 0.0.0.0   '将自己的地址改成任意地址,允许别人访问'
[root@localhost yum.repos.d]# systemctl start mongod.service  '开启服务'
[root@localhost yum.repos.d]# netstat -ntap |grep mongod
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      12857mongod  
[root@localhost yum.repos.d]# iptables -F
[root@localhost yum.repos.d]# setenforce 0
[root@localhost yum.repos.d]# mongo
exit退出
'配置mongo2,创建第二个实例'
[root@localhost yum.repos.d]# cd /etc/
[root@localhost etc]# cp -p mongod.conf mongod2.conf 
[root@localhost etc]# vim mongod2.conf 
systemLog:
path: /data/mongodb/mongod2.log 	'日志文件地址'
storage:
  dbPath: /data/mongodb/mongo	'数据文件存储的目录'
net:
  port: 27018
[root@localhost etc]# mkdir -p /data/mongodb '创建目录'
[root@localhost etc]# cd /data/mongodb/
[root@localhost mongodb]# mkdir mongod
[root@localhost mongodb]# touch mongod2.log		'创建日志文件'
[root@localhost mongodb]# chmod 777 mongod2.log 	'增加权限'
[root@localhost mongodb]# ls
mongod  mongod2.log
[root@localhost mongodb]# mv mongod mongo
[root@localhost mongodb]# mongod -f /etc/mongod2.conf  '开启指定mongo2的文件去登录'
about to fork child process, waiting until server is ready for connections.
forked process: 13983
child process started successfully, parent exiting
netstat -ntap mongod
[root@localhost mongodb]# mongo --port 27018

Three: mongodb database operation

3.1: Basic operation

> show dbs	 '查看数据库'
admin   0.000GB
config  0.000GB
local   0.000GB
> db.version()	'查看版本'
3.6.20	

ues school;   '不存在会创建,不建立集合又会删除'
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
db.createCollection('info')  '创建集合,info表示集合'
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
school  0.000GB
> show collections
info
db.info.find()	'查找'
插入数据
db.info.insert({
    
    "id":1,"name":"zhangsan","score":99,"hobby":["game","talk"]})
WriteResult({
    
     "nInserted" : 1 })

> db.info.find()
{
    
     "_id" : ObjectId("5f5c21d3a50e49270a4c7d96"), "id" : 1, "name" : "zhangsan", "score" : 99, "hobby" : [ "game", "talk" ] }
> a=db.info.find()	并赋予别名a
> for(var i=1;i<=100;i++)db.info.insert({
    
    "id":i,"name":"jack"+i}) '字符串连接中间加+号,会将后面的自动转换成字符串'
> it '查看更多'

> a=db.info.findOne ({
    
    "id":10})  '查找指定的第十条记录并赋予别名'
{
    
    
	"_id" : ObjectId("5f5c239fa50e49270a4c7da0"),
	"id" : 10,
	"name" : "jack10"
}

  
> typeof(a.id)	'查看属性类型'
number
> typeof(a.name)
string

> db.info.update({
    
    "id":10},{
    
    $set:{
    
    "name":"tom"}})   '修改'
> a=db.info.findOne({
    
    "id":10})
{
    
     "_id" : ObjectId("5f5c239fa50e49270a4c7da0"), "id" : 10, "name" : "tom" }

show collections  '查看集合'
db.info.drop	'删除集合'

'删除数据库'
show dbs
use 数据库名
db.dropDatabase() 

'统计'  
db.info.count()
显示
100

3.2: Import and export data

  • export data
[root@localhost mongodb]# mongoexport -d school -c info -o /opt/school.json
2020-09-12T09:37:45.716+0800	connected to: localhost
2020-09-12T09:37:45.716+0800	exported 0 records
[root@localhost mongodb]# cd /opt/
[root@localhost opt]# ls
rh  school.json
-d:指定数据库
-c:指定集合
-o:指出
  • Import Data
[root@localhost mongodb]# mongoimport -d school -c test --file users.json
[root@localhost mongodb]# mongo
> use school
> show collections
info 
test

3.3: Conditional operation

[root@localhost opt]# mongoexport -d school -c info -q '{"id":{"$eq":10}}' -o /opt/top10.json
'导出第十行'

3.4: Backup and restore

[root@localhost opt]# mkdir /backup
[root@localhost opt]# mongodump -d school -o /opt/backup
[root@localhost opt]# ls
backup  info.json  rh  school.json  top10.json
[root@localhost opt]# cd backup/
[root@localhost backup]# ls
school
[root@localhost backup]# cd school/
[root@localhost school]# ls
test.bson  test.metadata.json

[root@localhost school]# mongorestore -d abc --dir=/opt/backup/school
[root@localhost school]# mongo
> show dbs;
abc     0.000GB

3.5: Copy the database

> use school
switched to db school
> for(var i=1;i<=100;i++)db.users.insert({
    
    "id":1,"name":"jack"+1})
WriteResult({
    
     "nInserted" : 1 })
> show tables;
> db.users.find()
> db.copyDatabase("school","share")
{
    
     "ok" : 1 }
> use share
> db.users.find()

3.6: Clone the collection

mongo --port 27018
> db.runCommand({
    
    "cloneCollection":"school.users","from":"20.0.0.51:27017"})

3.7: Create an administrative user

> use admin
switched to db admin
> db.createUser({
    
    "user":"root","pwd":"123","roles":["root"]})
Successfully added user: {
    
     "user" : "root", "roles" : [ "root" ] } 
roles表示角色,也就是权限
> db.auth("root","123"

3.8: Process Management

> db.currentOp()
'只要注意opid'
	"opid" : 5409,
> db.killOp(5409)
'并不是删除数据库,而是将当前进程初始化'

Guess you like

Origin blog.csdn.net/m0_47219942/article/details/108552702