Linux下MongoDB安装

本文详细介绍了MongoDB在Linux上的安装步骤,包括下载、解压、创建数据和日志目录,配置mongod.conf文件,解决启动时的错误问题,并最终成功启动MongoDB服务。此外,还展示了通过客户端连接MongoDB并查看数据库的操作过程。
摘要由CSDN通过智能技术生成

勿以浮沙筑高台


MongoDB安装

  • 下载MongoDB

    wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.2.tgz
    
  • 解压MongoDB

    tar xf mongodb-linux-x86_64-rhel70-4.4.2.tgz -C /usr/local
    
  • 到安装文件下的bin文件,创建3个文件夹,data log conf

    cd /usr/local/mongodb/
    mkdir data log conf
    
  • 新建配置文件 mongod.conf,需要配置日志文件路径,数据文件路径

    cd /conf
    vim 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: /usr/local/mongodb/log/mongod.log
    # Where and how to store data.
    # 数据文件路径配置.
    storage:
    dbPath: /usr/local/mongodb/data
    journal:
      enabled: true
    # engine:
    # wiredTiger:
    # how the process runs
    processManagement:
    # fork=true代表后台启动
    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改为任意IP访问
    bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, 
    alternatively, use the net.bindIpAll setting.
    ### 添加授权
    # security:
    #   authorization: enable
    
  • 进入我们的bin文件夹,mongo是客户端,mongod是服务端,mogos是路由

    [root@work1 bin]# cd /usr/local/mongodb/bin/
    [root@work1 bin]# ll
    总用量 195360
    -rwxr-xr-x. 1 root root     7684 1117 2020 install_compass
    -rwxr-xr-x. 1 root root 53415728 1116 2020 mongo     #mongo是客户端
    -rwxr-xr-x. 1 root root 82209144 1116 2020 mongod    #mongod是服务端
    -rwxr-xr-x. 1 root root 64413232 1116 2020 mongos    #mogos是路由
    #启动服务
    #不管输出什么只要,又东西输出就代表可以
    [root@work1 bin]# ./mongod   
    
  • 运行报错,查看日志文件是没有进程文件目录

    	[root@work1 bin]# ./mongod -f ../conf/mongod.conf
    about to fork child process, waiting until server is ready for connections.
    forked process: 2373
    ERROR: child process failed, exited with 1
    To see additional information in this output, start without the "--fork" option.
    [root@work1 bin]# cd ../log
    [root@work1 log]# ll
    总用量 4
    -rw-------. 1 root root 890 622 21:40 mongod.log
    [root@work1 log]# cat mongod.log
    {"t":{"$date":"2021-06-22T21:40:34.789+08:00"},"s":"I",  "c":"CONTROL",  "id":23285,   "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
    {"t":{"$date":"2021-06-22T21:40:35.377+08:00"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
    {"t":{"$date":"2021-06-22T21:40:35.378+08:00"},"s":"I",  "c":"NETWORK",  "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
    {"t":{"$date":"2021-06-22T21:40:35.379+08:00"},"s":"I",  "c":"CONTROL",  "id":23330,   "ctx":"main","msg":"ERROR: Cannot write pid file to {path_string}: {errAndStr_second}","attr":{"path_string":"/var/run/mongodb/mongod.pid","errAndStr_second":"No such file or directory"}}
    
  • 创建进程文件路径

    [root@work1 log]# mkdir -p /var/run/mongodb/
    
  • 运行成功

    [root@work1 bin]# ./mongod -f ../conf/mongod.conf
    about to fork child process, waiting until server is ready for connections.
    forked process: 2381
    child process started successfully, parent exiting
    
  • 客户端链接

    ./mongo
    

    show databases出现数据,代表链接上了

    [root@work1 bin]# ./mongo
    MongoDB shell version v4.4.2
    connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
    Implicit session: session { "id" : UUID("487166a5-a544-4a4b-9b10-e1dab6b287b0") }
    MongoDB server version: 4.4.2
    Welcome to the MongoDB shell.
    For interactive help, type "help".
    For more comprehensive documentation, see
            https://docs.mongodb.com/
    Questions? Try the MongoDB Developer Community Forums
            https://community.mongodb.com
    ---
    The server generated these startup warnings when booting:
            2021-06-22T21:43:34.402+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
            2021-06-22T21:43:34.402+08:00: You are running this process as the root user, which is not recommended
            2021-06-22T21:43:34.403+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
            2021-06-22T21:43:34.403+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
            2021-06-22T21:43:34.403+08:00: Soft rlimits too low
            2021-06-22T21:43:34.403+08:00:         currentValue: 1024
            2021-06-22T21:43:34.403+08: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()
    ---
    > shouw databases
    uncaught exception: SyntaxError: unexpected token: identifier :
    @(shell):1:6
    > shouw datebases
    uncaught exception: SyntaxError: unexpected token: identifier :
    @(shell):1:6
    > show databases
    admin   0.000GB
    config  0.000GB
    local   0.000GB
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值