centos7下安装MongoDB

1、下载MongoDB

curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-5.0.0.tgz

2、解压

tar -zxvf linux/mongodb-linux-x86_64-rhel70-5.0.0.tgz
mv linux/mongodb-linux-x86_64-rhel70-5.0.0 mongodb

3、创建数据库保存文件夹和日志文件夹

[root@mongodb] mkdir data
[root@mongodb] mkdir logs
[root@mongodb] chmod 777 data
[root@mongodb] chmod 777 logs # 分配权限
[root@mongodb] cd logs
[root@logs] touch mongodb.log #创建日志文件

4、配置环境变量

export PATH=$PATH:/usr/local/mongodb/bin

5、添加配置文件

[root@mongodb] vi mongodb.conf

配置文件内容

port=27017 #端口
dbpath= /usr/local/mongodb/data #数据库存文件存放目录
logpath= /usr/local/mongodb/logs/mongodb.log #日志文件存放路径
logappend=true #使用追加的方式写日志
fork=true #以守护进程的方式运行,创建服务器进程
maxConns=100 #最大同时连接数
noauth=true #不启用验证
journal=true #每次写入会记录一条操作日志(通过journal可以重新构造出写入的数据)。
#即使宕机,启动时wiredtiger会先将数据恢复到最近一次的checkpoint点,然后重放后续的journal日志来恢复。
storageEngine=wiredTiger  #存储引擎,有mmapv1、wiretiger、mongorocks
bind_ip = 0.0.0.0  #设置成全部ip可以访问,这样就可以在windows中去连虚拟机的MongoDB,也可以设置成某个网段或者某个ip1234567891011

6、启动

[root@localhost bin]# ./mongod --config /usr/local/mongodb/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 1446
child process started successfully, parent exiting

7、创建用户、分配权限、创建数据库

登录

[root@localhost bin]# ./mongo
MongoDB shell version v5.0.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ff24c050-0937-46d0-a9eb-e3d462d0bee8") }
MongoDB server version: 5.0.0
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
We recommend you begin using "mongosh".
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
---
The server generated these startup warnings when booting: 
        2021-07-19T13:30:51.081-04:00: You are running this process as the root user, which is not recommended
        2021-07-19T13:30:51.081-04:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2021-07-19T13:30:51.081-04:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
        2021-07-19T13:30:51.081-04:00: Soft rlimits for open file descriptors too low
        2021-07-19T13:30:51.081-04:00:         currentValue: 1024
        2021-07-19T13:30:51.081-04:00:         recommendedMinimum: 64000
---
---
        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()
---
> 

使用账号登录

mongo --host 127.0.0.1 -u "accesslog" --authenticationDatabase "accesslog" -p'accesslog'

登录某个IP

mongo --host 127.0.0.1

创建用户、分配权限

use test
db.createUser(
  {
    user: "test",
    pwd: "test123",
    roles: [ { role: "readWrite", db: "test" },
             { role: "read", db: "reporting" } ]
  }
)

认证

db.auth('username','password')

删除用户

db.dropUser('username')

更新用户

db.updateUser('username)

查看用户

db.getUser('accesslog')

mongodb内置角色

read    提供读取所有非系统的集合(数据库)   
readWrite   提供读写所有非系统的集合(数据库)和读取所有角色的所有权限   
dbAdmin 提供执行管理任务的功能,例如与架构相关的任务,索引编制,收集统计信息。此角色不授予用户和角色管理权限。   
dbOwner 提供对数据库执行任何管理操作的功能。此角色组合了readWrite,dbAdmin和userAdmin角色授予的权限。
userAdmin   提供在当前数据库上创建和修改角色和用户的功能。由于userAdmin角色允许用户向任何用户(包括他们自己)授予任何权限,因此该角色还间接提供对数据库的超级用户访问权限,或者,如果作用于管理数据库,则提供对群集的访问权限。   
clusterAdmin    提供最佳的集群管理访问。此角色组合了clusterManager,clusterMonitor和hostManager角色授予的权限。此外,该角色还提供了dropDatabase操作。   
readAnyDatabase 仅在admin 数据库中使用,提供所有数据库的读权限。  
readWriteAnyDatabase    尽在admin 数据库中使用,提供所有数据库的读写权限   
userAdminAnyDatabase    尽在admin 数据库中使用,提供与userAdmin相同的用户管理操作访问权限,允许用户向任何用户(包括他们自己)授予任何权限,因此该角色还间接提供超级用户访问权限。   
dbAdminAnyDatabase  仅在admin 数据库中使用,提供与dbAdmin相同的数据库管理操作访问权限,该角色还在整个群集上提供listDatabases操作。  
root    尽在admin 数据库中使用,提供超级权限   

8、mongodb命令

show dbs #查看数据库  
show users #查看当前数据库用户和权限  
db.dropUser('username') #删除用户  
db.updateUser('username) #更新用户  
use accesslog #创建数据库

查看用户例子:

> show users 
# 结果
{
    "_id" : "accesslog.accesslog",
    "userId" : UUID("768d8f6e-b7ee-4694-85ac-dc86581ad460"),
    "user" : "accesslog",
    "db" : "accesslog",
    "roles" : [
        {
            "role" : "readWrite",
            "db" : "accesslog"
        }
    ],
    "mechanisms" : [
        "SCRAM-SHA-1",
        "SCRAM-SHA-256"
    ]
}

查询

use accesslog # 切换数据库  
db.gatewaylog.find() #查询  
db.gatewaylog.find().pretty() #查询后格式化结果
db.gatewaylog.findOne()

提示:accesslog是数据库,gatewaylog是数据库表

db.gatewaylog.remove() #删除
db.gatewaylog.insert() #插入
> use accesslog
switched to db accesslog
> db.gatewaylog.find()
{ "_id" : ObjectId("60f786dbe8de9e4420750b65"), "targetServer" : "CompositeDiscoveryClient_account-service", "requestPath" : "/account-service/account/javadaily", "requestMethod" : "GET", "schema" : "http", "requestBody" : "", "responseData" : "{\"msg\":\"操作成功\",\"code\":200,\"data\":{\"id\":1,\"accountCode\":\"javadaily\",\"accountName\":\"JAVA日知录\",\"amount\":10000.00},\"timestamp\":1626834651502}", "requestTime" : ISODate("2021-07-21T02:30:51.452Z"), "responseTime" : ISODate("2021-07-21T02:30:51.505Z"), "executeTime" : NumberLong(53), "_class" : "com.hzhh123.cloudgateway.domain.GatewayLog" }
> db.gatewaylog.find().pretty()
{
    "_id" : ObjectId("60f786dbe8de9e4420750b65"),
    "targetServer" : "CompositeDiscoveryClient_account-service",
    "requestPath" : "/account-service/account/javadaily",
    "requestMethod" : "GET",
    "schema" : "http",
    "requestBody" : "",
    "responseData" : "{\"msg\":\"操作成功\",\"code\":200,\"data\":{\"id\":1,\"accountCode\":\"javadaily\",\"accountName\":\"JAVA日知录\",\"amount\":10000.00},\"timestamp\":1626834651502}",
    "requestTime" : ISODate("2021-07-21T02:30:51.452Z"),
    "responseTime" : ISODate("2021-07-21T02:30:51.505Z"),
    "executeTime" : NumberLong(53),
    "_class" : "com.hzhh123.cloudgateway.domain.GatewayLog"
}
> db.gatewaylog.findOne()
{
    "_id" : ObjectId("60f786dbe8de9e4420750b65"),
    "targetServer" : "CompositeDiscoveryClient_account-service",
    "requestPath" : "/account-service/account/javadaily",
    "requestMethod" : "GET",
    "schema" : "http",
    "requestBody" : "",
    "responseData" : "{\"msg\":\"操作成功\",\"code\":200,\"data\":{\"id\":1,\"accountCode\":\"javadaily\",\"accountName\":\"JAVA日知录\",\"amount\":10000.00},\"timestamp\":1626834651502}",
    "requestTime" : ISODate("2021-07-21T02:30:51.452Z"),
    "responseTime" : ISODate("2021-07-21T02:30:51.505Z"),
    "executeTime" : NumberLong(53),
    "_class" : "com.hzhh123.cloudgateway.domain.GatewayLog"
}

use accesslog;
db.GatewayLog.insert({
id:ObjectId(),
targetServer:'CompositeDiscoveryClient_account-service',
requestPath:'/account-service/account/javadaily',
requestMethod:'GET',
schema:'http',
requestBody:'',
responseData: {"msg":"操作成功","code":200,"data":{"id":1,"accountCode":"javadaily","accountName":"JAVA日知录","amount":10000.00},"timestamp":1626829723283}
,
ip:'127.0.0.1',
requestTime:ISODate("2014-04-12T21:49:17Z"),
responseTime:ISODate("2014-04-12T21:49:17Z"),
executeTime:871
});


更多mongodb操作命令参考https://www.runoob.com/mongodb/mongodb-delete-collection.html

9、连接工具Navicat 12 for MongoDB

参考http://www.ddooo.com/softdown/130308.htm

10、mongodb停止服务命令

[root@localhost] ps -ef|grep mongo
oot       2447      1  0 13:30 ?        00:02:12 ./mongod --config /usr/local/mongodb/mongodb.conf
root       3236   2851  0 17:50 pts/0    00:00:00 grep --color=auto mongo

[root@localhost] kill -9 2447

你可能感兴趣的:(centos7下安装MongoDB)