在Linux下安装MongoDB和配置教程
MongoDB是一个开源的NoSQL数据库,它使用BSON(类似于JSON)格式存储数据,MongoDB适用于各种规模的应用程序,从简单的文档存储到复杂的实时分析,在本教程中,我们将介绍如何在Linux下安装MongoDB并进行基本配置。
准备工作
1、系统要求:确保您的Linux系统满足以下要求:
64位操作系统(例如Ubuntu 16.04或更高版本)
GNU C++编译器(g++)
Python 2.7或更高版本
MongoDB的官方安装包
2、下载MongoDB安装包:访问MongoDB官方网站(https://www.mongodb.com/try/download/community)下载适合您系统的MongoDB安装包。
安装MongoDB
1、解压缩安装包:将下载的MongoDB安装包解压到一个合适的目录,例如/opt
。
sudo tar zxvf mongodblinuxx86_644.4.3.tgz C /opt
2、创建数据和日志目录:为MongoDB创建一个数据和日志目录。
sudo mkdir p /data/db /data/log
3、修改配置文件:复制默认的配置文件并修改相关设置。
sudo cp /opt/mongodblinuxx86_644.4.3/mongod.conf /etc/mongod.conf sudo nano /etc/mongod.conf
在/etc/mongod.conf
文件中,找到以下行并进行相应的修改:
存储数据和日志的目录 dbpath = /data/db logpath = /data/log/mongodb.log
保存并退出编辑器。
启动MongoDB服务
1、创建系统服务:创建一个名为mongod
的系统服务。
sudo nano /etc/systemd/system/mongod.service
在文件中添加以下内容:
[Unit] Description=Highperformance, schemafree documentoriented database After=network.target [Service] User=mongodb Group=mongodb ExecStart=/opt/mongodblinuxx86_644.4.3/bin/mongod quiet config /etc/mongod.conf PIDFile=/var/run/mongodb/mongod.pid TimeoutStopSec=10min KillMode=mixed Restart=onfailure RestartSec=5s SendSIGKILL=no SendSIGHUP=yes SyslogIdentifier=mongodb SyslogFacility=local4 LogLevel=info StorageEngine=wiredTiger ProtectionLevel=readWriteOnce ReadPreference=primaryPreferred Journal=[{ enabled: true, mmapv1: { enabled: false } }, { enabled: false, mmapv1: { enabled: false } }] ExecStartPost=/usr/bin/ulimit n 1000000 && /usr/bin/chown R mongodb:mongodb /data/db && /usr/bin/chown R mongodb:mongodb /data/log && /usr/bin/chown R mongodb:mongodb /var/run/mongodb && /usr/bin/chown R mongodb:mongodb /opt/mongodblinuxx86_644.4.3 /opt/mongodblinuxx86_644.4.3/bin/* /opt/mongodblinuxx86_644.4.3/*.jar /opt/mongodblinuxx86_644.4.3/*.so && /usr/bin/find /data/db type f exec chmod 770 {} \; && /usr/bin/find /data/log type f exec chmod 770 {} \; && /usr/bin/find /var/run/mongodb type f exec chmod 770 {} \; && /usr/bin/find /opt/mongodblinuxx86_644.4.3 type f exec chmod 770 {} \; && /usr/bin/find /opt/mongodblinuxx86_644.4.3 type d exec chmod 770 {} \; && echo "security" >> /etc/mongod.conf && echo "authorization: enabled" >> /etc/mongod.conf && echo "bindIp: 127.0.0.1" >> /etc/mongod.conf && echo "port: 27017" >> /etc/mongod.conf && echo "replication: { replSetName: \"rs0\" }">> /etc/mongod.conf && echo "storage: { dbPath: \"$HOME/\", journal: { enabled: true } }" >> /etc/mongod.conf && echo "systemLog: { destination: file, path: \"$HOME/\", logAppend: true, quiet: true, metaData: false }">> /etc/mongod.conf && echo "net: { bindIp: \"127.0.0.1\", port: 27017, http: { enabled: true, version: \"1.1\" } }" >> /etc/mongod.conf && echo "processManagement: { fork: true, pidFilePath: \"$HOME/\", logFilePath: \"$HOME/\", logVerbosity: 0 }">> /etc/mongod.conf && echo "setParameter: { enableLocalhostAuthBypass: false }">> /etc/mongod.conf && echo "auditLog: { destination: file, path: "$HOME/\", format: json, auditIntervalMillis: 1000, mode: always }">> /etc/mongod.conf && echo "storageEngine: wiredTiger">> /etc/mongod.conf && echo "wiredTiger: { engineConfig: { cacheSizeGB: 2 } }">> /etc/mongod.conf && echo "setParameter: { failIndexKeyTooLong: false }">> /etc/mongod.conf && echo "setParameter: { internalQueryMaxLimitBytes: 10485760 }">> /etc/mongod.conf && echo "setParameter: { maxTimeDifferenceBetweenTransactionsInSeconds: 120 }">> /etc/mongod.conf && echo "setParameter: { minWiredTigerFreeListPercentage: 5 }">> /etc/mongod.conf && echo "setParameter: { numInitialSyncAttempts: 1 }">> /etc/mongod.conf && echo "setParameter: { readPreference: primaryPreferred }">> /etc/mongod.conf && echo "setParameter: { safePeriodMinutes: 0 }">> /etc/mongod.conf && echo "setParameter: { storageEngineAllowDiskUseAboveMemoryFraction: true }">> /etc/mongod.conf && echo "setParameter: { ttlMonitorEnabled: true }">> /etc/mongod.conf && echo "setParameter: { writeConcernMajorityJournalDefaultDecorationsEnabled: true }">> /etc/mongod.conf && echo "setParameter: { writeConcernWalDefaultCommitTagSizeBytes: 536870912 }">> /etc/mongod.conf && echo "setParameter: { writeConcernWalDefaultNumberOfTagsToKeepPerDocumentType}">> /etc/mongod.furthermore
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/508499.html