如何在 Linux 平台安装 MongoDB
MongoDB 提供了 linux 各个发行版本 64 位的安装包,你可以在官网下载安装包。
YUM 安装-最简单的安装方式
很多平台都内置了MongoDB的yum源,可以直接通过yum命令安装,yum安装是小白的福音,你不用考虑任何兼容问题、安装包问题、配置问题,只通过一条命令就可以完成整个安装过程,属于Linux平台下的“一键安装”,大家赶紧体验一下吧!
[root@centos ~]# yum -y install mongodb-org
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
Determining fastest mirrors
* remi-php74: mirrors.tuna.tsinghua.edu.cn
* remi-safe: mirrors.tuna.tsinghua.edu.cn
docker-ce-stable | 3.5 kB 00:00
epel | 4.7 kB 00:00
extras | 2.9 kB 00:00
mariadb | 3.4 kB 00:00
mngodb-org | 2.5 kB 00:00
os | 3.6 kB 00:00
remi-php74 | 3.0 kB 00:00
remi-safe | 3.0 kB 00:00
updates | 2.9 kB 00:00
(1/6): remi-safe/primary_db | 2.1 MB 00:00
(2/6): remi-php74/primary_db | 251 kB 00:00
(3/6): epel/7/x86_64/updateinfo | 1.0 MB 00:00
(4/6): epel/7/x86_64/primary_db | 7.0 MB 00:01
(5/6): updates/7/x86_64/primary_db | 12 MB 00:01
(6/6): docker-ce-stable/7/x86_64/primary_db | 67 kB 00:02
Resolving Dependencies
--> Running transaction check
---> Package mongodb-org.x86_64 0:4.0.27-1.el7 will be installed
--> Processing Dependency: mongodb-org-shell = 4.0.27 for package: mongodb-org-4 .0.27-1.el7.x86_64
--> Processing Dependency: mongodb-org-server = 4.0.27 for package: mongodb-org- 4.0.27-1.el7.x86_64
--> Processing Dependency: mongodb-org-mongos = 4.0.27 for package: mongodb-org- 4.0.27-1.el7.x86_64
--> Processing Dependency: mongodb-org-tools = 4.0.27 for package: mongodb-org-4 .0.27-1.el7.x86_64
--> Running transaction check
---> Package mongodb-org-mongos.x86_64 0:4.0.27-1.el7 will be installed
---> Package mongodb-org-server.x86_64 0:4.0.27-1.el7 will be installed
---> Package mongodb-org-shell.x86_64 0:4.0.27-1.el7 will be installed
---> Package mongodb-org-tools.x86_64 0:4.0.27-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
mongodb-org x86_64 4.0.27-1.el7 mngodb-org 6.2 k
Installing for dependencies:
mongodb-org-mongos x86_64 4.0.27-1.el7 mngodb-org 9.6 M
mongodb-org-server x86_64 4.0.27-1.el7 mngodb-org 17 M
mongodb-org-shell x86_64 4.0.27-1.el7 mngodb-org 10 M
mongodb-org-tools x86_64 4.0.27-1.el7 mngodb-org 41 M
Transaction Summary
================================================================================
Install 1 Package (+4 Dependent packages)
Total download size: 78 M
Installed size: 259 M
Downloading packages:
(1/5): mongodb-org-4.0.27-1.el7.x86_64.rpm | 6.2 kB 00:00
(2/5): mongodb-org-mongos-4.0.27-1.el7.x86_64.rpm | 9.6 MB 00:00
(3/5): mongodb-org-shell-4.0.27-1.el7.x86_64.rpm | 10 MB 00:00
(4/5): mongodb-org-server-4.0.27-1.el7.x86_64.rpm | 17 MB 00:01
(5/5): mongodb-org-tools-4.0.27-1.el7.x86_64.rpm | 41 MB 00:02
--------------------------------------------------------------------------------
Total 18 MB/s | 78 MB 00:04
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mongodb-org-mongos-4.0.27-1.el7.x86_64 1/5
Installing : mongodb-org-tools-4.0.27-1.el7.x86_64 2/5
Installing : mongodb-org-shell-4.0.27-1.el7.x86_64 3/5
Installing : mongodb-org-server-4.0.27-1.el7.x86_64 4/5
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.
Installing : mongodb-org-4.0.27-1.el7.x86_64 5/5
Verifying : mongodb-org-server-4.0.27-1.el7.x86_64 1/5
Verifying : mongodb-org-4.0.27-1.el7.x86_64 2/5
Verifying : mongodb-org-shell-4.0.27-1.el7.x86_64 3/5
Verifying : mongodb-org-tools-4.0.27-1.el7.x86_64 4/5
Verifying : mongodb-org-mongos-4.0.27-1.el7.x86_64 5/5
Installed:
mongodb-org.x86_64 0:4.0.27-1.el7
Dependency Installed:
mongodb-org-mongos.x86_64 0:4.0.27-1.el7
mongodb-org-server.x86_64 0:4.0.27-1.el7
mongodb-org-shell.x86_64 0:4.0.27-1.el7
mongodb-org-tools.x86_64 0:4.0.27-1.el7
Complete!
默认数据位置
/var/lib/mongo
默认日志位置
/var/log/mongodb
配置文件位置
vim /etc/mongod.conf
修改监听ip和端口
net:
port: 3717
bindIp: 0.0.0.0
压缩包安装
安装前我们需要安装各个 Linux 平台依赖包。
Red Hat/CentOS:
sudo yum install libcurl openssl
Ubuntu 18.04 LTS (“Bionic”)/Debian 10 “Buster”:
sudo apt-get install libcurl4 openssl
Ubuntu 16.04 LTS (“Xenial”)/Debian 9 “Stretch”:
sudo apt-get install libcurl3 openssl
MongoDB 源码下载地址:https://www.mongodb.com/download-center#community
这里我们选择 tgz 下载,下载完安装包,并解压 tgz(以下演示的是 64 位 Linux上的安装) 。
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1604-4.2.8.tgz # 下载
tar -zxvf mongodb-linux-x86_64-ubuntu1604-4.2.8.tgz # 解压
mv mongodb-src-r4.2.8 /usr/local/mongodb4 # 将解压包拷贝到指定目录
MongoDB 的可执行文件位于 bin 目录下,所以可以将其添加到 PATH 路径中:
export PATH=<mongodb-install-directory>/bin:$PATH
<mongodb-install-directory> 为你 MongoDB 的安装路径。如本文的 /usr/local/mongodb4 。
export PATH=/usr/local/mongodb4/bin:$PATH
创建数据库目录
默认情况下 MongoDB 启动后会初始化以下两个目录:
- 数据存储目录:/var/lib/mongodb
- 日志文件目录:/var/log/mongodb
我们在启动前可以先创建这两个目录并设置当前用户有读写权限:
sudo mkdir -p /var/lib/mongo
sudo mkdir -p /var/log/mongodb
sudo chown `whoami` /var/lib/mongo # 设置权限
sudo chown `whoami` /var/log/mongodb # 设置权限
接下来启动 Mongodb 服务:
mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork
下面是mongod启动的常用参数详细说明:
参数 | 说明 | 取值示例 |
dbpath | mongodb数据文件存储路径 | /data/mongodb |
logpath | mongod的日志路径 | /var/log/mongodb/mongodb.log |
logappend | 日志使用追加代替覆盖 | true |
bind_ip | 绑定的IP | 10.10.10.10 |
port | 绑定的端口 | 27107 |
journal | write操作首先写入“日记”,是一个数据安全的设置,具体参考官方文档。 | true |
打开 /var/log/mongodb/mongod.log 文件看到以下信息,说明启动成功。
# tail -10f /var/log/mongodb/mongod.log
2020-07-09T12:20:17.391+0800 I NETWORK [listener] Listening on /tmp/mongodb-27017.sock
2020-07-09T12:20:17.392+0800 I NETWORK [listener] Listening on 127.0.0.1
2020-07-09T12:20:17.392+0800 I NETWORK [listener] waiting for connections on port 27017
MongoDB 后台管理 Shell
如果你需要进入 mongodb 后台管理,你需要先打开 mongodb 装目录的下的 bin 目录,然后执行 mongo 命令文件。
MongoDB Shell 是 MongoDB 自带的交互式 Javascript shell,用来对 MongoDB 进行操作和管理的交互式环境。
当你进入 mongoDB 后台后,它默认会链接到 test 文档(数据库):
[root@centos ~]# mongo
MongoDB shell version v4.0.27
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("2551141b-6ee1-46ff-b36d-583efe3edd62") }
MongoDB server version: 4.0.27
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()
>
由于它是一个JavaScript shell,您可以运行一些简单的算术运算:
> 2+2
4
> 3+6
9
现在让我们插入一些简单的数据,并对插入的数据进行检索:
> db.runoon.insert({x:10})
WriteResult({ "nInserted" : 1 })
> db.runoon.find()
{ "_id" : ObjectId("5f069bdb4e02f8baf90f1184"), "x" : 10 }
>
第一个命令将数字 10 插入到 runoon 集合的 x 字段中。
mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --shutdown
也可以在 mongo 的命令出口中实现:
> use admin
switched to db admin
> db.shutdownServer()
更多安装方法可以参考官网:https://docs.mongodb.com/manual/administration/install-on-linux/