0.conn.ym-ubu24.9.mongo
1.绿色安装
  1. MongoDB 6.0.27依赖OpenSSL 1.1.1库

    wget http://security.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2.24_amd64.deb
    sudo dpkg -i libssl1.1_1.1.1f-1ubuntu2.24_amd64.deb
  2. mongodb

    cd /data/apk
    wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2004-6.0.27.tgz
    sudo tar -zxvf mongodb-linux-x86_64-ubuntu2004-6.0.27.tgz
    sudo cp mongodb-linux-x86_64-ubuntu2004-6.0.27/bin* /usr/local/bin/
    rm mongodb-linux-x86_64-ubuntu2004-6.0.27 -rf
    
    sudo mkdir -p /data/svr/mongodb/27117/data   #数据
    sudo mkdir -p /data/svr/mongodb/27117/log    #日志
    sudo mkdir -p /data/svr/mongodb/27117/key    #加入集群的认证key
    
    sudo useradd --system --no-create-home --shell /usr/sbin/nologin mongodb
    sudo chown -R mongodb:mongodb /data/svr/mongodb
2.开机启动
  1. vim /data/svr/mongodb/27117/mongod.conf

    # mongod.conf
    
    # for documentation of all options, see:
    #   http://docs.mongodb.org/manual/reference/configuration-options/
    
    # Where and how to store data.
    storage:
      dbPath: /data/svr/mongodb/27117/data
      journal:
        enabled: true
    #  engine:
    #  wiredTiger:
    #    engineConfig:
    #       cacheSizeGB: 4
    # where to write logging data.
    systemLog:
      destination: file
      logAppend: true
      path: /data/svr/mongodb/27117/log/mongod.log
    
    # network interfaces
    net:
      port: 27117
      bindIp: 0.0.0.0
    
    # how the process runs  #时区
    processManagement:
      timeZoneInfo: /usr/share/zoneinfo
    
    #security:              #节点加入集群的认证方式
    security:
      authorization: enabled
    #-  clusterAuthMode: keyFile
    #-  keyFile: /data/svr/mongodb/27117/key/mongoms.key
    
    #operationProfiling:
    
    #replication:           #实例归属于哪个副本集
    #-replication:
    #-  replSetName: msa
    #sharding:
    
    ## Enterprise-Only Options:
    
    #auditLog:
    
    #snmp:
  2. vim /etc/systemd/system/mongod.service

    [Unit]
    Description=MongoDB Database Server
    Documentation=https://docs.mongodb.org/manual
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    User=mongodb
    Group=mongodb
    EnvironmentFile=-/etc/default/mongod
    Environment="MONGODB_CONFIG_OVERRIDE_NOFORK=1"
    ExecStart=/usr/local/bin/mongod --config /data/svr/mongodb/27117/mongod.conf
    RuntimeDirectory=mongodb
    # file size
    LimitFSIZE=infinity
    # cpu time
    LimitCPU=infinity
    # virtual memory size
    LimitAS=infinity
    # open files
    LimitNOFILE=64000
    # processes/threads
    LimitNPROC=64000
    # locked memory
    LimitMEMLOCK=infinity
    # total threads (user+kernel)
    TasksMax=infinity
    TasksAccounting=false
    
    # Recommended limits for mongod as specified in
    # https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings
    
    [Install]
    WantedBy=multi-user.target
  3. 设置开机启动

    sudo systemctl daemon-reload
    sudo systemctl enable mongod
    sudo systemctl start mongod
3.创建用户
  1. 创建root用户

    mongosh --port 27117                #没有创建用户可以这样访问
    use admin
    db.createUser(
        {
            user:"root",
            pwd:"123321",
            roles:[
                {
                    role:"root",
                    db:"admin"
                }
            ]
        }
    )
  2. 创建读写用户

    mongosh --port 27117 --username root --password  123321
    db.createUser(
        {
            user:"svr",
            pwd:"123321",
            roles:[
                {
                    role:"readWriteAnyDatabase",
                    db:"admin"
                }
            ]
        }
    )
    use admin
    show users                                  //查看用户(在admin库注册的用户)
    db.updateUser("svr", { pwd: "pw-new" })     //修改密码
  3. 创建只读用户

    mongosh --port 27117 --username root --password  123321
    db.createUser(
        {
            user:"reader",
            pwd:"123321",
            roles:[
                {
                    role:"readAnyDatabase",
                    db:"admin"
                }
            ]
        }
    )
  4. roles

用户类型 推荐role 创建在哪个库 用途
超级管理员 root admin DBA/运维紧急操作
开发账号 readWriteAnyDatabase admin 开发环境方便使用
数据分析 readAnyDatabase admin 统计平台
监控账号 clusterMonitor admin prometheus、集群状态监控
业务服务器 readWrite 业务库 游戏服、服务端程序连接
查询账号 read 业务库 运营查询、客服查询
备份账号 backup admin 自动备份
文档更新时间: 2026-08-20 11:18   作者:morninglu