MongoDB的安装(Linux环境)

火影  金牌会员 | 2024-8-24 00:15:53 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 544|帖子 544|积分 1632

登录到Linux服务器执行 lsb_release -a ,即可得知服务器的版本信息为:CentOS 7。(如需了解别的方式获取Linux系统信息可参考该链接:Linux服务器常用巡检下令)
  1. [root@linux100 ~]# hostnamectl
  2.    Static hostname: linux100
  3.          Icon name: computer-vm
  4.            Chassis: vm
  5.         Machine ID: fb9d8715fff144aca93accad42cec460
  6.            Boot ID: 49ec505cca3d4052a6115b36a670e7e6
  7.     Virtualization: vmware
  8.   Operating System: CentOS Linux 7 (Core)
  9.        CPE OS Name: cpe:/o:centos:centos:7
  10.             Kernel: Linux 3.10.0-1160.25.1.el7.x86_64
  11.       Architecture: x86-64
复制代码

需要在官网先确定系统对应的Mongo DB版本:平台支持的MongoDB版本比对

这里Linux服务器版本为CentOS 7 x86-64,则MongoDB选择7.0.8版本。

1 在官网下载安装包

MongoDB官网下载地址:MongoDB Enterprise Server Download | MongoDB


MongoDB从6.0.10及其之后版本中,bin目次下就没有mongo可执行文件,则需要单独安装官方客户端下令行工具MongoDB shell来毗连进行数据库操作,它不支持图形用户页面。 官网下载地址:https://www.mongodb.com/try/download/shell。


如需使用其他MongoDB客户端工具可看该文章:MongoDB的安装(window环境)_windows下重装mongodb-CSDN博客文章浏览阅读649次,点赞7次,收藏11次。MongoDB shell是官方客户端下令行工具,不支持图形用户页面。_windows下重装mongodb
https://blog.csdn.net/qq_39512532/article/details/135533903。
2 安装MongoDB

(1)将下载的两个压缩包上传到服务器的 /opt/software 路径下
  1. [root@linux100 ~]# cd /opt/software/
  2. [root@linux100 software]# ll
  3. 总用量 12922
  4. -rw-r--r--. 1 root   root   154767684 4月  24 04:22 mongodb-linux-x86_64-enterprise-rhel70-7.0.8.tgz
  5. -rw-r--r--. 1 root   root    80612324 4月  24 05:09 mongosh-2.2.5-linux-x64.tgz
复制代码
(2)解压文件到自界说目次 /opt/module/ 路径下并改名:
  1. # 文件解压缩
  2. [root@linux100 software]# tar -zxvf mongodb-linux-x86_64-enterprise-rhel70-7.0.8.tgz -C /opt/module
  3. [root@linux100 software]# tar -zxvf mongosh-2.2.5-linux-x64.tgz -C /opt/module
  4. # 修改名称
  5. [root@linux100 software]# cd /opt/module/
  6. [root@linux100 module]# mv mongodb-linux-x86_64-enterprise-rhel70-7.0.8 mongodb-7.0.8
  7. [root@linux100 module]# mv mongosh-2.2.5-linux-x64 mongosh-2.2.5
复制代码
  扩展:以下是两个文件bin目次下的文件列表
  
  

  
  
  MongoDB的bin目次下文件详解:
  
文件名称说明形貌
mongod服务器程序,用于启动 MongoDB 服务;
mongosMongoDB 分片服务,用于管理分片集群;
  (3)创建MongoDB的数据存储目次和日志存储目次
  1. # 在mongodb-7.0.8文件下创建数据存储目录和日志存储目录
  2. [root@linux100 module]# cd mongodb-7.0.8
  3. [root@linux100 mongodb-7.0.8]# mkdir -p data
  4. [root@linux100 mongodb-7.0.8]# mkdir -p log
  5. [root@linux100 mongodb-7.0.8]# touch log/mongodb.log
复制代码
(4)创建启动用的配置文件mongod.conf
  1. [root@linux100 mongodb-7.0.8]# mdkir config
  2. [root@linux100 mongodb-7.0.8]# vim config/mongod.conf
复制代码
mongod.conf配置以下内容:
   # 数据存储位置
dbpath=/opt/module/mongodb-7.0.8/data/
  # 日志存储位置
logpath=/opt/module/mongodb-7.0.8/log/mongodb.log
  # 运行时占用端口
port=27017
  # fork为true指明以配景方式启动
fork=true
  3 启动MongoDB及其客户端

  1. [root@linux100 mongodb-7.0.8]# cd bin
  2. # 启动MongoDB服务端
  3. [root@linux100 bin]# ./mongod -f /opt/module/mongodb-7.0.8/config/mongod.conf
  4. about to fork child process, waiting until server is ready for connections.
  5. forked process: 21418
  6. child process started successfully, parent exiting
  7. # 启动MongoDB客户端,默认连接本机的MongoDB服务(以默认端口连接),并进入到test数据库
  8. [root@linux100 bin]# cd /opt/module/mongosh-2.2.5/bin/
  9. [root@linux100 bin]# ./mongosh
  10. Current Mongosh Log ID: 66285b69ce6a67cac62202d7
  11. Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.5
  12. Using MongoDB:          7.0.8
  13. Using Mongosh:          2.2.5
  14. For mongosh info see: https://docs.mongodb.com/mongodb-shell/
  15. ------
  16.    The server generated these startup warnings when booting
  17.    2024-04-24T09:07:39.038+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
  18.    2024-04-24T09:07:39.038+08:00: You are running this process as the root user, which is not recommended
  19.    2024-04-24T09:07:39.038+08:00: This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip <address> to specify which IP addresses it should serve responses from, or with --bind_ip_all to bind to all interfaces. If this behavior is desired, start the server with --bind_ip 127.0.0.1 to disable this warning
  20.    2024-04-24T09:07:39.038+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' in this binary version
  21.    2024-04-24T09:07:39.038+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never' in this binary version
  22.    2024-04-24T09:07:39.038+08:00: Soft rlimits for open file descriptors too low
  23. ------
  24. Deprecation warnings:
  25.   - Using mongosh on the current operating system is deprecated, and support may be removed in a future release.
  26. See https://www.mongodb.com/docs/mongodb-shell/install/#supported-operating-systems for documentation on supported platforms.
  27. Enterprise test>
复制代码
  题目:
  如果Mongodb服务和客户端不在同一台服务器,毗连MongoDB服务毗连不上!!!
  在当地浏览器访问http://192.168.243.100:27017,访问失败!!!
  解决方案:
  (1)关闭MongoDB所在服务器防火墙,具体可参考下面链接:
  Linux服务器防火墙的操作
https://blog.csdn.net/qq_39512532/article/details/131229204(2)mongodb启动时需要绑定所在IP:
  1. [root@linux100 ~]# cd /opt/module/mongodb-7.0.8/bin
  2. # 启动MongoDB服务端,并绑定该服务ip
  3. [root@linux100 bin]# ./mongod -f /opt/module/mongodb-7.0.8/config/mongod.conf --bind_ip 192.168.243.100
  4. # 启动Mongodb客户端
  5. [root@linux100 bin]# ./mongosh mongodb://192.168.243.100:27017
复制代码
在浏览器中输入地址测试MongoDB是否启动成功:http://192.168.243.100:27017
  

  

4 配置登岸认证

(1)进入admin数据库
  1. Enterprise test> use admin
复制代码
(2)创建管理员用户,并设置密码与权限
  1. Enterprise admin> db.createUser({user:'admin', pwd: 'a123456',roles:[{role:'root',db:'admin'}]})
复制代码
(3)验证刚刚创建的账户是否成功:(1成功、0失败)
  1. Enterprise admin> db.auth('admin','a123456')
  2. { ok: 1 }
复制代码
(4)在/opt/module/mongodb-7.0.8/config/mongod.conf配置文件下面再新增以下内容:
   #认证模式(true代表开启认证登录,false代表未开启认证登录)
auth=true   
#远程毗连
bind_ip=0.0.0.0
  重启mongoDB服务以便配置生效。
  1. [root@linux100 bin] ./mongod -f /opt/module/mongodb-7.0.8/config/mongod.conf
复制代码
(5)客户端用账号密码登录
  1. [root@linux100 bin]# cd /opt/module/mongosh-2.2.5/bin/
  2. [root@linux100 bin]# ./mongosh 'mongodb://admin:a123456@192.168.243.100:27017'
复制代码
  扩展:
  

  • 删除用户角色:db.dropUser("admin")
  • 修改用户密码:db.changeUserPassword("admin","123")
  5 操作数据库

MongoDB数据库的详细操作步骤可参考该链接:
MongoDB的数据库操作
https://blog.csdn.net/qq_39512532/article/details/135533055
 6 关闭MongoDB

  1. # 使用lsof命令查看MongoDB是否运行,也可找到该端口上运行的进程ID
  2. [root@linux100 bin]# lsof -i :27017
  3. COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
  4. mongod  21418 root   15u  IPv4 505796      0t0  TCP localhost:27017 (LISTEN)
  5. # 关闭MongoDB服务
  6. [root@linux100 bin]# kill -9 21418
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

火影

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表