MongoDB 数据库部署和应用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB 数据库部署和应用相关的知识,希望对你有一定的参考价值。
推荐步骤:
➢ 在 Centos01 上部署 MongoDB 服务器客户端登录验证
➢ 在 Centos01 的 MongoDB 配置文件通过控制文件控制 MongoDB 服务,配置 MongoDB 身份验证
➢ 在 Centos01 的 MongoDB 服务器配置身份验证管理和修改配置文件支持验证
➢ 在 Centos01 管理 MongoDB 管理数据,集合批量数据管理
实验步骤:
一、在 Centos01 上安装 mongoDB 数据库管理 mongoDB 服务
1、安装 MongoDB
1)创建管理 MongoDB 组和用户
[root@centos01 ~]# groupadd mongod
[root@centos01 ~]# useradd -M -s /sbin/nologin -g mongod mongod
2)解压指定 MongoDB 安装位置[root@centos01 ~]# mv /usr/src/mon
godb-linux-x86_64-rhel70-4.2.24/
/usr/local/mongodb
3)优化命令
[root@centos01 ~]# echo "PATH=$PATH:/usr/local/mongodb/bin/" >> /etc/profile
[root@centos01 ~]# source /etc/profile
2、配置 mongodb 配置文件日志文件初始化 mongoDB
1)创建 MongoDB 数据库的配置文件
[root@centos01 ~]# mkdir /usr/local/mongodb/conf
[root@centos01 ~]# mkdir /usr/local/mongodb/log
[root@centos01 ~]# mkdir /usr/local/mongodb/data
[root@centos01 ~]# chown -R mongod:mongod /usr/local/mongodb/
2)初始化 MongoDB 数据库启动服务
[root@centos01 ~]# mongod --dbpath=/usr/local/mongodb/data/ --
logpath=/usr/local/mongodb/log/mongodb.log --port=27017 --logappend --fork
about to fork child process, waiting until server is ready for connections.
forked process: 1885
child process started successfully, parent exiting
3)查看服务运行状态
[root@centos01 ~]# netstat -anptu | grep mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1885/mongod
3、登录 mongoDB 测试 mongoDB
1)登录 MongoDB
[root@centos01 ~]# mongoTo permanently disable this reminder, run the following command:
db.disableFreeMonitoring()
---
>
2)查看 MongoDB 数据库
> show databases;
admin 0.000GB
config 0.000GB
local 0.000GB
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
3)切换数据库查看表
> use admin
switched to db admin
> show tables
system.version
> exit
bye二、生成 MongoDB 配置文件通过控制文件控制 MongoDB 服务,配置
MongoDB 身份验证
1、配置生成 MongoDB 配置文件
1)生成 MongoDB 配置文件
[root@centos01 ~]# vim /usr/local/mongodb/conf/mongo.conf
systemLog:
destination: file
path: "/usr/local/mongodb/log/mongodb.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/usr/local/mongodb/data/"
processManagement:
fork: true
net:
port: 27017
bindIp: 192.168.100.10,127.0.0.1
2)修改配置文件所有者
[root@centos01 ~]# chown mongod:mongod /usr/local/mongodb/conf/mongo.conf
3)停止服务
[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf --shutdown
killing process with pid: 1885
2、通过配置文件启动停止 mongoDB
1)通过配置文件启动 mongoDB
[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3663
child process started successfully, parent exiting
2)查看服务运行状态
[root@centos01 ~]# netstat -anptu | grep mongod
tcp 0 0 192.168.100.10:27017 0.0.0.0:* LISTEN 3663/mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 3663/mongod
3)通过配置文件停止服务
[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf --shutdown
killing process with pid: 3663
3、登录 mongoDB 停止服务
1)启动 mongoDB 服务
[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3922
child process started successfully, parent exiting
2)登录 mongoDB 切换到 admin 数据库停止服务
[root@centos01 ~]# mongo
> use admin
switched to db admin
> db.shutdownServer()2023-03-16T00:23:10.633+0800 I NETWORK [js] DBClientConnection failed to receive
message from 127.0.0.1:27017 - HostUnreachable: Connection closed by peer
server should be down...
三、配置 mongoDB 身份验证管理和修改配置文件支持验证
1、MongDB 创建管理员和读取写入数据用户
1)创建管理员用户
[root@centos01 ~]# mongo
> use admin //切换到admin数据库
switched to db admin
> db.createUser( //db.createUser函数创建用户
...
... user: "root", //用户命令root
... pwd: "pwd@123", //授权密码
... roles: [ role: "root", db: "admin" ]
//授权角色
...
)
Successfully added user:
"user" : "root",
"roles" : [
"role" : "root",
"db" : "admin"
]
> exit
bye
2)使用 root 登录创建应用程序访问用户授权读取和写入权限
[root@centos01 ~]# mongo -uroot -ppwd@123 192.168.100.10/admin
> use admin
switched to db admin
> db.createUser(
...
... user: "bob",
... pwd: "pwd@123",
... roles: [ role: "readWrite", db: "bob" ]
...
... )
Successfully added user:
"user" : "bob",
"roles" : [
"role" : "readWrite",
"db" : "bob"
]
1)创建管理 MongoDB 组和用户
[root@centos01 ~]# groupadd mongod
[root@centos01 ~]# useradd -M -s /sbin/nologin -g mongod mongod
2)解压指定 MongoDB 安装位置
[root@centos01 ~]# mv /usr/src/mongodb-linux-x86_64-rhel70-4.2.24/
/usr/local/mongodb
3)优化命令
[root@centos01 ~]# echo "PATH=$PATH:/usr/local/mongodb/bin/" >> /etc/profile
[root@centos01 ~]# source /etc/profile
2、配置 mongodb 配置文件日志文件初始化 mongoDB
1)创建 MongoDB 数据库的配置文件
[root@centos01 ~]# mkdir /usr/local/mongodb/conf
[root@centos01 ~]# mkdir /usr/local/mongodb/log
[root@centos01 ~]# mkdir /usr/local/mongodb/data
[root@centos01 ~]# chown -R mongod:mongod /usr/local/mongodb/
2)初始化 MongoDB 数据库启动服务
[root@centos01 ~]# mongod --dbpath=/usr/local/mongodb/data/ --
logpath=/usr/local/mongodb/log/mongodb.log --port=27017 --logappend --fork
about to fork child process, waiting until server is ready for connections.
forked process: 1885
child process started successfully, parent exiting
3)查看服务运行状态
[root@centos01 ~]# netstat -anptu | grep mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1885/mongod
3、登录 mongoDB 测试 mongoDB
1)登录 MongoDB
[root@centos01 ~]# mongoTo permanently disable this reminder, run the following command:
db.disableFreeMonitoring()
---
>
2)查看 MongoDB 数据库
> show databases;
admin 0.000GB
config 0.000GB
local 0.000GB
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
3)切换数据库查看表
> use admin
switched to db admin
> show tables
system.version
> exit
bye二、生成 MongoDB 配置文件通过控制文件控制 MongoDB 服务,配置
MongoDB 身份验证
1、配置生成 MongoDB 配置文件
1)生成 MongoDB 配置文件
[root@centos01 ~]# vim /usr/local/mongodb/conf/mongo.conf
systemLog:
destination: file
path: "/usr/local/mongodb/log/mongodb.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/usr/local/mongodb/data/"
processManagement:
fork: true
net:
port: 27017
bindIp: 192.168.100.10,127.0.0.1
2)修改配置文件所有者
[root@centos01 ~]# chown mongod:mongod /usr/local/mongodb/conf/mongo.conf
3)停止服务
[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf --shutdown
killing process with pid: 1885
2、通过配置文件启动停止 mongoDB
1)通过配置文件启动 mongoDB
[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3663
child process started successfully, parent exiting
2)查看服务运行状态
[root@centos01 ~]# netstat -anptu | grep mongod
tcp 0 0 192.168.100.10:27017 0.0.0.0:* LISTEN 3663/mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 3663/mongod
3)通过配置文件停止服务
[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf --shutdown
killing process with pid: 3663
3、登录 mongoDB 停止服务
1)启动 mongoDB 服务
[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3922
child process started successfully, parent exiting
2)登录 mongoDB 切换到 admin 数据库停止服务
[root@centos01 ~]# mongo
> use admin
switched to db admin
> db.shutdownServer()2023-03-16T00:23:10.633+0800 I NETWORK [js] DBClientConnection failed to receive
message from 127.0.0.1:27017 - HostUnreachable: Connection closed by peer
server should be down...
三、配置 mongoDB 身份验证管理和修改配置文件支持验证
1、MongDB 创建管理员和读取写入数据用户
1)创建管理员用户
[root@centos01 ~]# mongo
> use admin //切换到admin数据库
switched to db admin
> db.createUser( //db.createUser函数创建用户
...
... user: "root", //用户命令root
... pwd: "pwd@123", //授权密码
... roles: [ role: "root", db: "admin" ]
//授权角色
...
)
Successfully added user:
"user" : "root",
"roles" : [
"role" : "root",
"db" : "admin"
]
> exit
bye
2)使用 root 登录创建应用程序访问用户授权读取和写入权限
[root@centos01 ~]# mongo -uroot -ppwd@123 192.168.100.10/admin
> use admin
switched to db admin
> db.createUser(
...
... user: "bob",
... pwd: "pwd@123",
... roles: [ role: "readWrite", db: "bob" ]
...
... )
Successfully added user:
"user" : "bob",
"roles" : [
"role" : "readWrite",
"db" : "bob"
]
exitbye
1)创建管理 MongoDB 组和用户
[root@centos01 ~]# groupadd mongod
[root@centos01 ~]# useradd -M -s /sbin/nologin -g mongod mongod
2)解压指定 MongoDB 安装位置
[root@centos01 ~]# mv /usr/src/mongodb-linux-x86_64-rhel70-4.2.24/
/usr/local/mongodb
3)优化命令
[root@centos01 ~]# echo "PATH=$PATH:/usr/local/mongodb/bin/" >> /etc/profile
[root@centos01 ~]# source /etc/profile
2、配置 mongodb 配置文件日志文件初始化 mongoDB
1)创建 MongoDB 数据库的配置文件
[root@centos01 ~]# mkdir /usr/local/mongodb/conf
[root@centos01 ~]# mkdir /usr/local/mongodb/log
[root@centos01 ~]# mkdir /usr/local/mongodb/data
[root@centos01 ~]# chown -R mongod:mongod /usr/local/mongodb/
2)初始化 MongoDB 数据库启动服务
[root@centos01 ~]# mongod --dbpath=/usr/local/mongodb/data/ --
logpath=/usr/local/mongodb/log/mongodb.log --port=27017 --logappend --fork
about to fork child process, waiting until server is ready for connections.
forked process: 1885
child process started successfully, parent exiting
3)查看服务运行状态
[root@centos01 ~]# netstat -anptu | grep mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1885/mongod
3、登录 mongoDB 测试 mongoDB
1)登录 MongoDB
[root@centos01 ~]# mongoTo permanently disable this reminder, run the following command:
db.disableFreeMonitoring()
---
>
2)查看 MongoDB 数据库
> show databases;
admin 0.000GB
config 0.000GB
local 0.000GB
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
3)切换数据库查看表
> use admin
switched to db admin
> show tables
system.version
> exit
bye
二、生成 MongoDB 配置文件通过控制文件控制 MongoDB 服务,配置
MongoDB 身份验证
1、配置生成 MongoDB 配置文件
1)生成 MongoDB 配置文件
[root@centos01 ~]# vim /usr/local/mongodb/conf/mongo.conf
systemLog:
destination: file
path: "/usr/local/mongodb/log/mongodb.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/usr/local/mongodb/data/"
processManagement:
fork: true
net:
port: 27017
bindIp: 192.168.100.10,127.0.0.1
2)修改配置文件所有者
[root@centos01 ~]# chown mongod:mongod /usr/local/mongodb/conf/mongo.conf
3)停止服务
[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf --shutdown
killing process with pid: 1885
2、通过配置文件启动停止 mongoDB
1)通过配置文件启动 mongoDB
[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3663
child process started successfully, parent exiting
2)查看服务运行状态
[root@centos01 ~]# netstat -anptu | grep mongod
tcp 0 0 192.168.100.10:27017 0.0.0.0:* LISTEN 3663/mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 3663/mongod
3)通过配置文件停止服务
[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf --shutdown
killing process with pid: 3663
3、登录 mongoDB 停止服务
1)启动 mongoDB 服务
[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3922
child process started successfully, parent exiting
2)登录 mongoDB 切换到 admin 数据库停止服务
[root@centos01 ~]# mongo
> use admin
switched to db admin
> db.shutdownServer()2023-03-16T00:23:10.633+0800 I NETWORK [js] DBClientConnection failed to receive
message from 127.0.0.1:27017 - HostUnreachable: Connection closed by peer
server should be down...
三、配置 mongoDB 身份验证管理和修改配置文件支持验证
1、MongDB 创建管理员和读取写入数据用户
1)创建管理员用户
[root@centos01 ~]# mongo
> use admin //切换到admin数据库
switched to db admin
> db.createUser( //db.createUser函数创建用户
...
... user: "root", //用户命令root
... pwd: "pwd@123", //授权密码
... roles: [ role: "root", db: "admin" ]
//授权角色
...
)
Successfully added user:
"user" : "root",
"roles" : [
"role" : "root",
"db" : "admin"
]
> exit
bye
2)使用 root 登录创建应用程序访问用户授权读取和写入权限
[root@centos01 ~]# mongo -uroot -ppwd@123 192.168.100.10/admin
> use admin
switched to db admin
> db.createUser(
...
... user: "bob",
... pwd: "pwd@123",
... roles: [ role: "readWrite", db: "bob" ]
...
... )
Successfully added user:
"user" : "bob",
"roles" : [
"role" : "readWrite",
"db" : "bob"
]
> exix
bye
3)使用普通用户登录 mongoDB
[root@centos01 ~]# mongo -ubob -ppwd@123 192.168.100.10/admin
2、修改 mongoDB 主配置文件支持验证
1)修改 mongoDB 主配置文件
[root@centos01 ~]# vim /usr/local/mongodb/conf/mongo.conf
systemLog:
destination: file
path: "/usr/local/mongodb/log/mongodb.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/usr/local/mongodb/data/"
processManagement:
fork: true
net:
port: 27017
bindIp: 192.168.100.10,127.0.0.1
security:
authorization: enabled
2)停止服务启动加载身份验证
[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf --shutdown
killing process with pid: 4337
[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.confabout to fork child process, waiting until server is ready for connections.
forked process: 6113
child process started successfully, parent exiting
3、配置客户端登录进行身份验证
1)通过 IP 地址或者数据库验证登录
[root@centos01 ~]# mongo -uroot -ppwd@123 admin
[root@centos01 ~]# mongo -uroot -ppwd@123 192.168.100.10/admin
2)登录进行验证
[root@centos01 ~]# mongo
> use admin
switched to db admin
> db.auth(root,pwd@123)
1
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
3)查看创建授权的用户
> use admin //切换到admin数据库
switched to db admin
> db.system.users.find() //查看users表中创建的用户信息
"_id" : "admin.root", "userId" : UUID("bbbb8384-552d-43b7-bdbd-849c811a7fba"),
"user" : "root", "db" : "admin", "credentials" : "SCRAM-SHA-1" :
"iterationCount" : 10000, "salt" : "OWAQ5bhvBM5xsUgG57vrRg==", "storedKey" :
"VhzOEnNHc1EneG2JwkIb/hgVzqc=", "serverKey" : "N95vRRKEPtbpy0M0PtUH4WbXidc=" , "SCRAM-SHA-256" : "iterationCount" : 15000, "salt" :
"srIf+zffJydaFBlm7NoOtJCCb+VF4/28B3Vigw==", "storedKey" :
"ZQXhhxJ2NpaNuMw2CvfXic+3KcTEE150Mk/fJ55WDM4=", "serverKey" :
"kCjYi9BLpDyL+kDRoQzNsKgsOZtJvQjXT9dW+/cWN4k=" , "roles" : [ "role" : "root",
"db" : "admin" ]
"_id" : "admin.bob", "userId" : UUID("23c4a9a8-8765-440e-bc1c-9bd0892c6661"),
"user" : "bob", "db" : "admin", "credentials" : "SCRAM-SHA-1" :
"iterationCount" : 10000, "salt" : "GOIEIxWUxjvXqNYlYdaplQ==", "storedKey" :
"6HBrMz8gUVURJWsV3TKb3ITB4pk=", "serverKey" : "rh7mibsdVUfcoQtbaV2PImqPlUM=" ,
"SCRAM-SHA-256" : "iterationCount" : 15000, "salt" :
"uTrQFcgpS/yxvc6yUmJ9U58Jwv51IalUOo223w==", "storedKey" :
"8q+7cXeEkL4ZyEMokSDzwsvay6vqjl9nxLy6zu3P27A=", "serverKey" :
"u24JQWS0TXn1Fmj0bnjrc8OUzzCdGICI3xN+EX7X00A=" , "roles" : [ "role" :
"readWrite", "db" : "bob" ]
> exit
bye
4)查看用户信息
> db.system.users.find().pretty()
"_id" : "admin.root",
"userId" : UUID("bbbb8384-552d-43b7-bdbd-849c811a7fba"),
"user" : "root",
"db" : "admin",
"credentials" :
"SCRAM-SHA-1" : "iterationCount" : 10000,
"salt" : "OWAQ5bhvBM5xsUgG57vrRg==",
"storedKey" : "VhzOEnNHc1EneG2JwkIb/hgVzqc=",
"serverKey" : "N95vRRKEPtbpy0M0PtUH4WbXidc="
,
"SCRAM-SHA-256" :
"iterationCount" : 15000,
"salt" : "srIf+zffJydaFBlm7NoOtJCCb+VF4/28B3Vigw==",
"storedKey" : "ZQXhhxJ2NpaNuMw2CvfXic+3KcTEE150Mk/fJ55WDM4=",
"serverKey" : "kCjYi9BLpDyL+kDRoQzNsKgsOZtJvQjXT9dW+/cWN4k="
,
"roles" : [
"role" : "root",
"db" : "admin"
]
"_id" : "admin.bob",
"userId" : UUID("23c4a9a8-8765-440e-bc1c-9bd0892c6661"),
"user" : "bob",
"db" : "admin","credentials" :
"SCRAM-SHA-1" :
"iterationCount" : 10000,
"salt" : "GOIEIxWUxjvXqNYlYdaplQ==",
"storedKey" : "6HBrMz8gUVURJWsV3TKb3ITB4pk=",
"serverKey" : "rh7mibsdVUfcoQtbaV2PImqPlUM="
,
"SCRAM-SHA-256" :
"iterationCount" : 15000,
"salt" : "uTrQFcgpS/yxvc6yUmJ9U58Jwv51IalUOo223w==",
"storedKey" : "8q+7cXeEkL4ZyEMokSDzwsvay6vqjl9nxLy6zu3P27A=",
"serverKey" : "u24JQWS0TXn1Fmj0bnjrc8OUzzCdGICI3xN+EX7X00A="
,
"roles" : [
"role" : "readWrite",
"db" : "bob"
]
四、配置 mongoDB 基本管理
1、mongoDB 数据库基本管理
1)查看 mongoDB 数据库版本
[root@centos01 ~]# mongo -uroot -ppwd@123 192.168.100.10/admin
> db.version()
4.2.24
2)显示当前数据库
> db
admin
3)查看当前数据库状态
> db.stats()
"db" : "admin",
"collections" : 2,
"views" : 0,
"objects" : 4,
"avgObjSize" : 287.25,
"dataSize" : 1149,
"storageSize" : 73728,
"numExtents" : 0,
"indexes" : 3,
"indexSize" : 110592,
"scaleFactor" : 1,
"fsUsedSize" : 4763291648,
"fsTotalSize" : 81353875456,"ok" : 1
4)查看数据库连接请求
> db.getMongo()
connection to 192.168.100.10:27017
5)切换到指定数据库
> use benet
switched to db benet
6)benet 数据库创建表 student 表插,id 插入 1
> db.student.insert(id:1)
WriteResult( "nInserted" : 1 )
7)查看创建数据库
> show dbs
admin 0.000GB
benet 0.000GB
config 0.000GB
local 0.000G
8)删除当前数据库
> db.dropDatabase()
"dropped" : "benet", "ok" : 1
2、集合的基本管理
1)创建 a 表插入数据
> db.a.insert(id:1)
WriteResult( "nInserted" : 1 )
> db.b.insert(id:2)WriteResult( "nInserted" : 1 )
2)查看表
> show tables;
a
b
3)查询表中数据
> db.a.find()
"_id" : ObjectId("641200d9810124bd98a26a78"), "id" : 1
4)创建集合名字 c 查看创建集合
> db.createCollection(c);
"ok" : 1
> show tables;
a
b
c
5)删除集合
> db.c.drop()
true
> show tables;
a
b
3、插入数据和批量插入数据
1)accp 库创建集合 t1 插入数据
> use accp
switched to db accp> db.t1.insert(id:110,name:"bob")
WriteResult( "nInserted" : 1 )
> db.t1.find()
"_id" : ObjectId("64120367810124bd98a26a7a"), "id" : 110, "name" : "bob"
> db.t1.insert(姓名:"张三",年龄:18,学历:"大专")
WriteResult( "nInserted" : 1 )
> db.t1.find()
"_id" : ObjectId("64120367810124bd98a26a7a"), "id" : 110, "name" : "bob"
"_id" : ObjectId("641203c3810124bd98a26a7b"), "姓名" : "张三", "年龄" : 18, "学历
" : "大专"
2)插入多个连续连数据
> db.t1.insertMany([
... name:"bob",
... name:"tom",
... name:"alice"
... ])
"acknowledged" : true,
"insertedIds" : [
ObjectId("641205bd810124bd98a26a7c"),
ObjectId("641205bd810124bd98a26a7d"),
ObjectId("641205bd810124bd98a26a7e")
]
> db.t1.find() "_id" : ObjectId("64120367810124bd98a26a7a"), "id" : 110, "name" : "bob"
"_id" : ObjectId("641203c3810124bd98a26a7b"), "姓名" : "张三", "年龄" : 18, "学历
" : "大专"
"_id" : ObjectId("641205bd810124bd98a26a7c"), "name" : "bob"
"_id" : ObjectId("641205bd810124bd98a26a7d"), "name" : "tom"
"_id" : ObjectId("641205bd810124bd98a26a7e"), "name" : "alice"
3)批量化插入数据
> for(i=0;i<100;i++) db.t1.insert("编号":i,"名字":"test","年龄":18,"date":new
Date());
WriteResult( "nInserted" : 1 )
4)查询编号为 50 的记录
> db.t1.find(编号:50)
"_id" : ObjectId("6412071c810124bd98a26ab1"), "编号" : 50, "名字" : "test", "年龄
" : 18, "date" : ISODate("2023-03-15T17:57:48.513Z")
5)清空 t1 表中数据
> db.t1.remove();
mongodb 部署安装(Linux 官方版本)
文档目的
在Linux系统中安装Mongodb应用程序,并设置基本的安全权限,保证数据库的安全
基础知识
MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。
MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。
常用命令
命令 | 用法 |
用配置文件启动mongo | mongod --config /etc/mongod.conf --port 27017 --dbpath /data/db1 |
重启mongo | service mongod restart |
将mongo加入开机启动 | chkconfig mongod on |
创建数据库 | use database_name |
查看数据库 | show dbs |
插入数据 | db.database_name.insert({“name”:”名字”}) |
删除数据库 | db.dropDatabase() |
删除集合 | db.collection.drop() |
插入文档 | db.COLLECTION_NAME.insert(document) |
查看已插入文档 | db.col.find() |
更新文档 | db.collection.update() |
db.collection.save() | |
删除文档 | db.collection.remove() |
mongodb监控 | mongostat |
mongotop |
所需软件
MongoDB server version: 3.4.1
系统环境
操作系统:centos 7 (3.10.0-327.36.3.el7.x86_64)
操作步骤
1.配置yum仓库
vim /etc/yum.repos.d/mongodb-org-3.4.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
2.安装mongodb应用
sudo yum install -y mongodb-org
3.用无访问权限控制方式开启mongo
mongod --port 27017 --dbpath /data/db1 &
注:如果没有/data/db1 文件夹,则新建一个 mkdir -p /data/db1
4.设置管理员账号密码
登入mongo
mongo --port 27017
创建管理员用户
use admin
db.createUser(
{
user: "myUserAdmin",
pwd: "abc123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
5.配置mongo配置文件开启强制访问权限控制功能
vim /etc/mongod.conf
修改安全选项
security:
authorization: enabled
6.应用配置文件开启mongo
mongod --config /etc/mongod.conf --port 27017 --dbpath /data/db1
注:也可以用以下命令强制访问权限控制开启功能开启mongo
mongod --auth --port 27017 --dbpath /data/db1
注:如果有开启SELinux,需要配置以下语句
semanage port -a -t mongod_port_t -p tcp 27017
7.创建一般用户账户
mongo
use test
db.createUser(
{
user: "myTester",
pwd: "xyz123",
roles: [ { role: "readWrite", db: "test" },
{ role: "read", db: "reporting" } ]
}
)
常见问题
参考文献
https://docs.mongodb.com/manual/tutorial/enable-authentication/
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/
本文出自 “11591346” 博客,请务必保留此出处http://11601346.blog.51cto.com/11591346/1892046
以上是关于MongoDB 数据库部署和应用的主要内容,如果未能解决你的问题,请参考以下文章