在Linux系统中安装MongoDB,首先需要下载MongoDB的安装包,可以通过访问MongoDB官方网站()下载对应版本的安装包,本文以下载MongoDB Community Server 4.4.3版本为例进行介绍。
1. 下载MongoDB安装包
访问MongoDB官方网站,点击“Downloads”选项卡,选择“Community Server”,然后点击“Download”按钮,在弹出的页面中,选择适合您的操作系统的版本,在本例中,我们选择“Linux”下的“x86_64-redhat-70-ssl”版本。
2. 上传安装包到Linux服务器
将下载好的安装包上传到Linux服务器上,可以使用SCP命令或者FTP工具进行上传,使用SCP命令上传安装包:
scp mongodb-linux-x86_64-4.4.3.tgz username@your_server_ip:/home/username
3. 解压安装包
在Linux服务器上,使用以下命令解压安装包:
tar -zxvf mongodb-linux-x86_64-4.4.3.tgz
4. 创建数据和日志目录
为了方便管理,我们需要为MongoDB创建一个数据和日志目录,使用以下命令创建目录:
mkdir -p /data/db /var/log/mongodb
5. 修改配置文件
在解压后的文件夹中,找到`mongod.conf`文件,使用文本编辑器打开并修改以下配置:
# 设置数据库存储路径 storage: dbPath: /data/db # 设置日志文件路径 systemLog: destination: file path: /var/log/mongodb/mongod.log logAppend: true
6. 创建系统服务
为了让MongoDB在系统启动时自动运行,我们需要创建一个系统服务,创建一个名为`mongod`的文件:
sudo touch /etc/systemd/system/mongod.service
使用文本编辑器打开该文件,并添加以下内容:
```ini
[Unit]
Description=MongoDB Database Server
After=network.target
Documentation=-on-linux/#run-mongod-as-a-service-unit
Wants=network-online.target
AssertFileIsExecutable=/usr/bin/mongod
AssertPathIsDirectory=/data/db
AssertFileIsReadable=/etc/mongod.conf
AssertFileIsWritable=/data/db/transactions-tmpdir
AssertFileIsExecutable=/usr/bin/mongod --config /etc/mongod.conf
[Service]
Type=forking
PIDFile=/var/run/mongodb/mongod.pid
ExecStart=/usr/bin/mongod --config /etc/mongod.conf --fork --logpath /var/log/mongodb/mongod.log --pidfile /var/run/mongodb/mongod.pid --quiet --replSet MaintainerMode --bind_ip_all --oplogSizeMB 128 --smallfiles --noprealloc --journalCommitIntervalMs 5000 --syncPeriodSecs 0 --shutdownTimeoutMinutes 90 --dbpath /data/db --auth &
User=mongodb
Group=nogroup
UMask=00077 # Only umask permissions r relevant for non-root users, so this is the recommended umask value for non-root users. If you're running as root, umask doesn't matter and you can set it to whatever value you like.Restart=always
RestartSec=10s # time to wait before restarting when process exits on non-zero status code or if the config files have been modified since the last restart (in seconds) [START = 10s] [MIN = 30s] [MAX = 3600s] [GRACE = 10m] [DEFAULT = 30s]KillMode=mixed # Indicates how processes are terminated when system resources are low or if an error occurs while the process is running. The available options are "graceful", "fast", and "mixed" (default). KillMode=mixed will attempt to kill all processes in a graceful manner first, but if that fails, it will resort to killing them quickly with SIGKILL if necessary.Environment=MONGO_INITDB_ROOT_USERNAME=root # Set this variable to specify the name of the initial root user created when the database is first installed and started up without any authentication enabled. This user has full access to the database and should only be used for initial setup purposes or testing purposes during development.Environment=MONGO_INITDB_ROOT_PASSWORD=example # Set this variable to specify the password for the initial root user created when the database is first installed and started up without any authentication enabled.Environment=MONGO_INITDB_DATABASE=test # Set this variable to specify the name of the database to create when the database is first installed and started up without any authentication enabled.Environment=MONGO_INITDB_AUTH=false # Set this variable to specify whether or not authentication should be enabled when the database is first installed and started up without any authentication enabled.Environment=MONGO_INITDB_READ_PRIVILEGES=user # Set this variable to specify which user(s) have read access when the database is first installed and started up without any authentication enabled.Environment=MONGO_INITDB_WRITE_PRIVILEGES=user # Set this variable to specify which user(s) have write access when the database is first installed and started up without any authentication enabled.[Install]WantedBy=multi-user.service # After=network.target means that network interfaces are up before starting this service, but we want to start this service after network interfaces are up so that we can connect to other servers in the cluster if needed. # Note that we use WantedBy=multi-user.service instead of WantedBy=graphical.target because we don't want this service to start automatically when the system boots up in graphical mode (i.e. single-user mode). We only want it to start automatically when the system boots up in multi-user mode (i.e. server mode). Save and close the file.
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/66382.html