zoukankan      html  css  js  c++  java
  • centos7安装mongodb

    Install MongoDB Community Edition on Red Hat or CentOS — MongoDB Manual

    官网的安装说明地址在这,但是有个问题,用5.0版本的源文件,报错,怎么都起不来,mongodb本身也没有打印任何日志,所以就直接运行mongod命令结果发现这个包都有毛病,没法运行

    Illegal instruction
    

    所以果断的切换到了4.4的源,安装4.4的版本一次性成功。

    首先就是先将源添加到系统编辑文件/etc/yum.repos.d/mongodb-org-4.4.repo

    [mongodb-org-4.4]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
    gpgcheck=1
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
    

    然后安装mongodb

    sudo yum install -y mongodb-org
    

    启动mongodb

    systemctl start mongod
    

    mongodb的配置文件路径为/etc/mongod.conf, 内容为

    # mongod.conf
    
    # for documentation of all options, see:
    #   http://docs.mongodb.org/manual/reference/configuration-options/
    
    # where to write logging data.
    systemLog:
      destination: file
      logAppend: true
      path: /var/log/mongodb/mongod.log
    
    # Where and how to store data.
    storage:
      dbPath: /var/lib/mongo
      journal:
        enabled: true
    #  engine:
    #  wiredTiger:
    
    # how the process runs
    processManagement:
      fork: true  # fork and run in background
      pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
      timeZoneInfo: /usr/share/zoneinfo
    
    # network interfaces
    net:
      port: 27017
      bindIp: 127.0.0.1  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
    
    
    #security:
    
    #operationProfiling:
    
    #replication:
    
    #sharding:
    
    ## Enterprise-Only Options
    
    #auditLog:
    
    #snmp:
    

    默认是只允许本机访问的,我们可以设置bindIp,用,分割,允许多个ip访问,如果更加直接一点可以设置bindIpAll 的值为true,允许所有的ip, 这两个参数是互斥的,生产还是限制一下ip安全。

    修改配置需要重启mongodb

    systemctl restart mongod
    

    注意:centos7 默认情况下是开启防火墙的,远程访问会让防火墙拦截

    firewall-cmd --state 查看状态

    我们可以关闭防火墙,或者开启端口,这里直接关闭防火墙

    systemctl stop firewalld.service

    设置开机不自启动

    systemctl disable firewalld.service

    mongodb管理工具可以使用

    Releases · Studio3T/robomongo (github.com)

    官方文档中的一些小细节

    目录创建和权限设置

    仔细翻翻文档下面有,文件目录的创建还有将文件目录用户所属设置为mongod, 实际操作中没有关心这一步可以正常启动。

    image.png

    我们可以通过命令systemctl status mongod 查看到service文件的目录

    image.png

    查看这个service文件可以看出,这里面写明了需要用mongod用户运行mongodb

    [Unit]
    Description=MongoDB Database Server
    Documentation=https://docs.mongodb.org/manual
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    User=mongod
    Group=mongod
    Environment="OPTIONS=-f /etc/mongod.conf"
    EnvironmentFile=-/etc/sysconfig/mongod
    ExecStart=/usr/bin/mongod $OPTIONS
    ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
    ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb
    ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb
    PermissionsStartOnly=true
    PIDFile=/var/run/mongodb/mongod.pid
    Type=forking
    # 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
    

    然后用命令查看了一下要求创建的目录发现已经存在而且就是属于这个用户的,所以我们就不用自己设置了

    image.png

    ulimit

    image.png

    这里主要介绍的是linux默认资源限制可能会让我们的数据库产生问题,默认linux是可以限制像进程可以打开多少文件、地址空间等限制,而在linux一切皆文件,创建tcp连接也会创建文件,所以你测试没有问题,生产环境可能会产生问题,所以这里mongodb官方给出了一些设置建议,如果你使用的是软件源安装的,我们是使用的systemd管理的进程,service文件中有如下内容

    # 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
    

    mongodb帮我们做了设置。

    在centos7中Systemd 中的 /etc/security/limits.conf 文件的配置作用域缩小了,只适用于通过PAM认证登录用户的资源限制,对Systemd的service的资源限制不生效。

  • 相关阅读:
    公用导航栏的根据url控制选中导航js
    页面切换出动晃动解决
    redis五大数据类型
    redis简介
    Linux安装redis
    各种锁的理解
    原子引用
    理解CAS
    彻底玩转单例模式
    Volatile
  • 原文地址:https://www.cnblogs.com/xuzhen97/p/15335456.html
Copyright © 2011-2022 走看看