小白教程
所有教程
关于
Search
172.71.254.9
172.71.254.9
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Linux 平台安装 MongoDB”的源代码
本页内容
上一节:
Windows_平台安装_MongoDB
下一节:
Mac_OSX_平台安装_MongoDB
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:Linux MongoDB}}[[Category:MongoDB 教程|5]] = Linux平台安装MongoDB = MongoDB 提供了 linux 各个发行版本 64 位的安装包,你可以在官网下载安装包。 安装前我们需要安装各个 Linux 平台依赖包。 '''Red Hat/CentOS:''' <sample title="" desc="" lang="mongodb" hererun="1"> sudo yum install libcurl openssl </sample> '''Ubuntu 18.04 LTS ("Bionic")/Debian 10 "Buster":''' <sample title="" desc="" lang="mongodb" hererun="1"> sudo apt-get install libcurl4 openssl </sample> '''Ubuntu 16.04 LTS ("Xenial")/Debian 9 "Stretch":''' <sample title="" desc="" lang="mongodb" hererun="1"> sudo apt-get install libcurl3 openssl </sample> MongoDB 源码下载地址:https://www.mongodb.com/download-center#community 这里我们选择 tgz 下载,下载完安装包,并解压 '''tgz'''(以下演示的是 64 位 Linux上的安装) 。 <sample title="" desc="" lang="mongodb" hererun="1"> 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 # 将解压包拷贝到指定目录 </sample> MongoDB 的可执行文件位于 bin 目录下,所以可以将其添加到 '''PATH''' 路径中: <sample title="" desc="" lang="mongodb" hererun="1"> export PATH=<mongodb-install-directory>/bin:$PATH </sample> '''<mongodb-install-directory>''' 为你 MongoDB 的安装路径。如本文的 '''/usr/local/mongodb4''' 。 <sample title="" desc="" lang="mongodb" hererun="1"> export PATH=/usr/local/mongodb4/bin:$PATH </sample> == 创建数据库目录 == 默认情况下 MongoDB 启动后会初始化以下两个目录: * 数据存储目录:/var/lib/mongodb * 日志文件目录:/var/log/mongodb 我们在启动前可以先创建这两个目录并设置当前用户有读写权限: <sample title="" desc="" lang="mongodb" hererun="1"> sudo mkdir -p /var/lib/mongo sudo mkdir -p /var/log/mongodb sudo chown `whoami` /var/lib/mongo # 设置权限 sudo chown `whoami` /var/log/mongodb # 设置权限 </sample> 接下来启动 Mongodb 服务: <sample title="" desc="" lang="mongodb" hererun="1"> mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork </sample> 打开 /var/log/mongodb/mongod.log 文件看到以下信息,说明启动成功。 <sample title="" desc="" lang="mongodb" hererun="1"> # 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 </sample> == MongoDB 后台管理 Shell == 如果你需要进入 mongodb 后台管理,你需要先打开 mongodb 装目录的下的 bin 目录,然后执行 mongo 命令文件。 MongoDB Shell 是 MongoDB 自带的交互式 Javascript shell,用来对 MongoDB 进行操作和管理的交互式环境。 当你进入 mongoDB 后台后,它默认会链接到 test 文档(数据库): <sample title="" desc="" lang="mongodb" hererun="1"> $ cd /usr/local/mongodb4/bin $ ./mongo MongoDB shell version v4.2.8 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("2cfdafc4-dd56-4cfc-933a-187b887119b3") } MongoDB server version: 4.2.8 Welcome to the MongoDB shell. …… </sample> 由于它是一个JavaScript shell,您可以运行一些简单的算术运算: <sample title="" desc="" lang="mongodb" hererun="1"> > 2+2 4 > 3+6 9 </sample> 现在让我们插入一些简单的数据,并对插入的数据进行检索: <sample title="" desc="" lang="mongodb" hererun="1"> > db.xiaobai.insert({x:10}) WriteResult({ "nInserted" : 1 }) > db.xiaobai.find() { "_id" : ObjectId("5f069bdb4e02f8baf90f1184"), "x" : 10 } > </sample> 第一个命令将数字 10 插入到 xiaobai 集合的 x 字段中。 如果要停止 mongodb 可以使用以下命令: <sample title="" desc="" lang="mongodb" hererun="1"> mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --shutdown </sample> 也可以在 mongo 的命令出口中实现: <sample title="" desc="" lang="mongodb" hererun="1"> > use admin switched to db admin > db.shutdownServer() </sample> 更多安装方法可以参考官网:https://docs.mongodb.com/manual/administration/install-on-linux/
返回至“
Linux 平台安装 MongoDB
”。
上一节:
Windows_平台安装_MongoDB
下一节:
Mac_OSX_平台安装_MongoDB