首页
Javascript
Html
Css
Node.js
Electron
移动开发
小程序
工具类
服务端
浏览器相关
前端收藏
其他
关于

linux下通过yum安装mongodb

2020年05月31日 发布 阅读(277) 作者:Jerman

假设我要安装mongodb 4.2版本

1、在目录/etc/yum.repos.d下创建repo仓库

  1. cd /etc/yum.repos.d
  2. touch mongodb-org-4.2.repo

编辑mongodb-org-4.2.repo

  1. vim mongodb-org-4.2.repo

输入以下内容:

  1. [mongodb-org-4.2.repo]
  2. name=MongoDB Repository
  3. baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/x86_64/
  4. gpgcheck=1
  5. enabled=1
  6. gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

上面的baseurl,可访问https://repo.mongodb.org/yum/redhat/7/mongodb-org/
上面的gpgkey,可访问 https://www.mongodb.org/static/pgp/

执行安装

  1. yum install -y mongodb-org
  1. [root@123456 yum.repos.d]# yum install -y mongodb-org
  2. Loaded plugins: fastestmirror
  3. Loading mirror speeds from cached hostfile
  4. base | 3.6 kB 00:00:00
  5. epel | 4.7 kB 00:00:00
  6. extras | 2.9 kB 00:00:00
  7. mongodb-org-4.2 | 2.5 kB 00:00:00
  8. updates | 2.9 kB 00:00:00
  9. (1/8): base/7/x86_64/group_gz | 153 kB 00:00:00
  10. (2/8): epel/x86_64/group_gz | 95 kB 00:00:00
  11. (3/8): epel/x86_64/updateinfo | 1.0 MB 00:00:00
  12. (4/8): extras/7/x86_64/primary_db | 194 kB 00:00:00
  13. (5/8): updates/7/x86_64/primary_db | 1.3 MB 00:00:00
  14. (6/8): epel/x86_64/primary_db | 6.8 MB 00:00:00
  15. (7/8): base/7/x86_64/primary_db | 6.1 MB 00:00:00
  16. (8/8): mongodb-org-4.2/primary_db | 34 kB 00:00:02
  17. Resolving Dependencies
  18. --> Running transaction check
  19. ---> Package mongodb-org.x86_64 0:4.2.7-1.el7 will be installed
  20. --> Processing Dependency: mongodb-org-mongos = 4.2.7 for package: mongodb-org-4.2.7-1.el7.x86_64
  21. --> Processing Dependency: mongodb-org-shell = 4.2.7 for package: mongodb-org-4.2.7-1.el7.x86_64
  22. --> Processing Dependency: mongodb-org-server = 4.2.7 for package: mongodb-org-4.2.7-1.el7.x86_64
  23. --> Processing Dependency: mongodb-org-tools = 4.2.7 for package: mongodb-org-4.2.7-1.el7.x86_64
  24. --> Running transaction check
  25. ---> Package mongodb-org-mongos.x86_64 0:4.2.7-1.el7 will be installed
  26. ---> Package mongodb-org-server.x86_64 0:4.2.7-1.el7 will be installed
  27. ---> Package mongodb-org-shell.x86_64 0:4.2.7-1.el7 will be installed
  28. ---> Package mongodb-org-tools.x86_64 0:4.2.7-1.el7 will be installed
  29. --> Finished Dependency Resolution
  30. Dependencies Resolved
  31. =====================================================================================================================================================================================================================================
  32. Package Arch Version Repository Size
  33. =====================================================================================================================================================================================================================================
  34. Installing:
  35. mongodb-org x86_64 4.2.7-1.el7 mongodb-org-4.2 5.8 k
  36. Installing for dependencies:
  37. mongodb-org-mongos x86_64 4.2.7-1.el7 mongodb-org-4.2 15 M
  38. mongodb-org-server x86_64 4.2.7-1.el7 mongodb-org-4.2 25 M
  39. mongodb-org-shell x86_64 4.2.7-1.el7 mongodb-org-4.2 17 M
  40. mongodb-org-tools x86_64 4.2.7-1.el7 mongodb-org-4.2 62 M
  41. Transaction Summary
  42. =====================================================================================================================================================================================================================================
  43. Install 1 Package (+4 Dependent packages)
  44. Total download size: 119 M
  45. Installed size: 283 M
  46. Downloading packages:
  47. warning: /var/cache/yum/x86_64/7/mongodb-org-4.2/packages/mongodb-org-4.2.7-1.el7.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID 058f8b6b: NOKEY ] 0.0 B/s | 0 B --:--:-- ETA
  48. Public key for mongodb-org-4.2.7-1.el7.x86_64.rpm is not installed
  49. (1/5): mongodb-org-4.2.7-1.el7.x86_64.rpm | 5.8 kB 00:00:01
  50. (2/5): mongodb-org-server-4.2.7-1.el7.x86_64.rpm | 25 MB 00:00:05
  51. (3/5): mongodb-org-shell-4.2.7-1.el7.x86_64.rpm | 17 MB 00:00:03
  52. (4/5): mongodb-org-tools-4.2.7-1.el7.x86_64.rpm | 62 MB 00:00:09
  53. (5/5): mongodb-org-mongos-4.2.7-1.el7.x86_64.rpm | 15 MB 00:00:44
  54. -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  55. Total 2.6 MB/s | 119 MB 00:00:45
  56. Retrieving key from https://www.mongodb.org/static/pgp/server-4.2.asc
  57. Importing GPG key 0x058F8B6B:
  58. Userid : "MongoDB 4.2 Release Signing Key <packaging@mongodb.com>"
  59. Fingerprint: e162 f504 a20c df15 827f 718d 4b7c 549a 058f 8b6b
  60. From : https://www.mongodb.org/static/pgp/server-4.2.asc
  61. Running transaction check
  62. Running transaction test
  63. Transaction test succeeded
  64. Running transaction
  65. Installing : mongodb-org-shell-4.2.7-1.el7.x86_64 1/5
  66. Installing : mongodb-org-tools-4.2.7-1.el7.x86_64 2/5
  67. Installing : mongodb-org-mongos-4.2.7-1.el7.x86_64 3/5
  68. Installing : mongodb-org-server-4.2.7-1.el7.x86_64 4/5
  69. Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.
  70. Installing : mongodb-org-4.2.7-1.el7.x86_64 5/5
  71. Verifying : mongodb-org-4.2.7-1.el7.x86_64 1/5
  72. Verifying : mongodb-org-server-4.2.7-1.el7.x86_64 2/5
  73. Verifying : mongodb-org-mongos-4.2.7-1.el7.x86_64 3/5
  74. Verifying : mongodb-org-tools-4.2.7-1.el7.x86_64 4/5
  75. Verifying : mongodb-org-shell-4.2.7-1.el7.x86_64 5/5
  76. Installed:
  77. mongodb-org.x86_64 0:4.2.7-1.el7
  78. Dependency Installed:
  79. mongodb-org-mongos.x86_64 0:4.2.7-1.el7 mongodb-org-server.x86_64 0:4.2.7-1.el7 mongodb-org-shell.x86_64 0:4.2.7-1.el7 mongodb-org-tools.x86_64 0:4.2.7-1.el7
  80. Complete!
  81. [root@123456 yum.repos.d]#

验证安装是否成功

  1. [root@123456 yum.repos.d]# rpm -qa |grep mongodb
  2. mongodb-org-mongos-4.2.7-1.el7.x86_64
  3. mongodb-org-tools-4.2.7-1.el7.x86_64
  4. mongodb-org-shell-4.2.7-1.el7.x86_64
  5. mongodb-org-4.2.7-1.el7.x86_64
  6. mongodb-org-server-4.2.7-1.el7.x86_64
  1. [root@123456 yum.repos.d]# rpm -ql mongodb-org-server
  2. /etc/mongod.conf
  3. /lib/systemd/system/mongod.service
  4. /usr/bin/mongod
  5. /usr/share/doc/mongodb-org-server-4.2.7
  6. /usr/share/doc/mongodb-org-server-4.2.7/LICENSE-Community.txt
  7. /usr/share/doc/mongodb-org-server-4.2.7/MPL-2
  8. /usr/share/doc/mongodb-org-server-4.2.7/README
  9. /usr/share/doc/mongodb-org-server-4.2.7/THIRD-PARTY-NOTICES
  10. /usr/share/man/man1/mongod.1
  11. /var/lib/mongo
  12. /var/log/mongodb
  13. /var/log/mongodb/mongod.log
  14. /var/run/mongodb

mongodb配置文件

从上面的代码可以看出,mongodb配置文件在/etc/mongod.conf

  1. [root@123456 yum.repos.d]# vim /etc/mongod.conf
  2. # mongod.conf
  3. # for documentation of all options, see:
  4. # http://docs.mongodb.org/manual/reference/configuration-options/
  5. # where to write logging data.
  6. systemLog:
  7. destination: file
  8. logAppend: true
  9. path: /var/log/mongodb/mongod.log
  10. # Where and how to store data.
  11. storage:
  12. dbPath: /var/lib/mongo
  13. journal:
  14. enabled: true
  15. # engine:
  16. # wiredTiger:
  17. # how the process runs
  18. processManagement:
  19. fork: true # fork and run in background
  20. pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
  21. timeZoneInfo: /usr/share/zoneinfo
  22. # network interfaces
  23. net:
  24. port: 27017
  25. bindIp: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
  26. #security:
  27. #operationProfiling:
  28. #replication:
  29. #sharding:
  30. ## Enterprise-Only Options
  31. #auditLog:
  32. #snmp:
  33. "/etc/mongod.conf" 44L, 830C

启动Mongodb

全局执行

  1. service mongod start
  1. [root@123456 yum.repos.d]# service mongod start
  2. Redirecting to /bin/systemctl start mongod.service
  3. [root@123456 yum.repos.d]# ps -ef | grep mongod
  4. root 4236 3730 0 11:17 pts/1 00:00:00 vim mongodb-org-4.2.1.repo
  5. mongod 5381 1 5 11:38 ? 00:00:00 /usr/bin/mongod -f /etc/mongod.conf
  6. root 5427 4567 0 11:38 pts/2 00:00:00 grep --color=auto mongod
  7. [root@123456 yum.repos.d]# netstat -nltp|grep mongo
  8. tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 5381/mongod

关闭mongodb

  1. service mongod stop

执行下面查询 ,已经查询不到mongod的服务

  1. [root@123456 yum.repos.d]# ps -ef | grep mongod
  2. root 4236 3730 0 11:17 pts/1 00:00:00 vim mongodb-org-4.2.1.repo
  3. root 5612 4567 0 11:41 pts/2 00:00:00 grep --color=auto mongod
  4. [root@123456 yum.repos.d]# netstat -nltp|grep mongo
  5. [root@123456 yum.repos.d]#

了解一下mongodb角色类型

  • 数据库用户角色(Database User Roles):
    read:授予User只读数据的权限
    readWrite:授予User读写数据的权限

  • 数据库管理角色(Database Administration Roles):
    dbAdmin:在当前dB中执行管理操作
    dbOwner:在当前DB中执行任意操作
    userAdmin:在当前DB中管理User

  • 备份和还原角色(Backup and Restoration Roles):
    backup
    restore

  • 跨库角色(All-Database Roles):
    readAnyDatabase:授予在所有数据库上读取数据的权限
    readWriteAnyDatabase:授予在所有数据库上读写数据的权限
    userAdminAnyDatabase:授予在所有数据库上管理User的权限
    dbAdminAnyDatabase:授予管理所有数据库的权限

  • 集群管理角色(Cluster Administration Roles):
    clusterAdmin:授予管理集群的最高权限
    clusterManager:授予管理和监控集群的权限,A user with this role can access the config and local databases, which are used in sharding and replication, respectively.
    clusterMonitor:授予监控集群的权限,对监控工具具有readonly的权限
    hostManager:管理Server

创建mongodb管理员用户

先启动mongod服务(service mongod start),再执行mongo命令进行mongodb命令行

  1. mongo
  2. db.createUser({user: "admin", pwd: "123456",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})
  1. [root@123456 ~]# mongo
  2. MongoDB shell version v4.2.7
  3. connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
  4. Implicit session: session { "id" : UUID("c6adb47d-4b62-4758-951f-0bacdd0fd0a9") }
  5. MongoDB server version: 4.2.7
  6. Server has startup warnings:
  7. 2020-05-31T11:43:46.555+0800 I STORAGE [initandlisten]
  8. 2020-05-31T11:43:46.555+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
  9. 2020-05-31T11:43:46.555+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
  10. 2020-05-31T11:43:47.753+0800 I CONTROL [initandlisten]
  11. 2020-05-31T11:43:47.753+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
  12. 2020-05-31T11:43:47.753+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
  13. 2020-05-31T11:43:47.753+0800 I CONTROL [initandlisten]
  14. 2020-05-31T11:43:47.753+0800 I CONTROL [initandlisten]
  15. 2020-05-31T11:43:47.753+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
  16. 2020-05-31T11:43:47.753+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
  17. 2020-05-31T11:43:47.753+0800 I CONTROL [initandlisten]
  18. 2020-05-31T11:43:47.753+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
  19. 2020-05-31T11:43:47.753+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
  20. 2020-05-31T11:43:47.753+0800 I CONTROL [initandlisten]
  21. ---
  22. Enable MongoDB's free cloud-based monitoring service, which will then receive and display
  23. metrics about your deployment (disk utilization, CPU, operation statistics, etc).
  24. The monitoring data will be available on a MongoDB website with a unique URL accessible to you
  25. and anyone you share the URL with. MongoDB may use this information to make product
  26. improvements and to suggest MongoDB products and deployment options to you.
  27. To enable free monitoring, run the following command: db.enableFreeMonitoring()
  28. To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
  29. ---
  30. > db.createUser({user: "admin", pwd: "123456",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})
  31. Successfully added user: {
  32. "user" : "admin",
  33. "roles" : [
  34. {
  35. "role" : "userAdminAnyDatabase",
  36. "db" : "admin"
  37. }
  38. ]
  39. }

创建mongodb普通数据库用户

跟创建管理员用户差不多,只是角色可能不一样,可以根据不同的db,设置不同的角色

  1. use test
  2. db.createUser( { user: "root", pwd: "root", roles: [ { role: "readWrite", db: "test" }, { role: "read", db: "test2" } ] } )
  1. use test
  2. db.createUser( { user: "root", pwd: "123456", roles: [{ role: "readWrite", db: "test" }]} )

查询有哪些数据库

  1. show dbs

查找所有用户

  1. use admin
  2. db.system.users.find()

删除用户

  1. db.dropUser('admin')

忘记了管理员用户怎么办?

关闭配置文件的强制认证,两行都要注释

  1. #security:
  2. # authorization: enabled

重新启动mongod服务

  1. service mongod restart

修改密码

  1. db.changeUserPassword('root','123456')

如果开启了security选项,就先要使用管理员登录:

  1. db.auth('admin','123456')
版权声明:本站文章除特别声明外,均采用署名-非商业性使用-禁止演绎 4.0 国际 许可协议,如需转载,请注明出处
  • redis启动多个实例,配置多个端口

    redis配置多个端口,redis启动多个实例

    发布:2020-08-11 阅读(169)

  • LINUX挂载硬盘(不分区)的方法

    linux如何挂载新买的硬盘?

    发布:2020-06-21 阅读(309)

  • vim编辑nginx conf文件时,设置高亮/显示行号

    vim编辑nginx conf文件时,设置高亮/显示行号

    发布:2020-06-19 阅读(270)

  • vi/vim打开文件提示Found a swap file by the name

    vi/vim打开文件提示Found a swap file by the name

    发布:2020-06-04 阅读(208)

  • C++ compiler too old, need g++ 4.9.4 or clang++ 3.4.2 (CXX=g++)

    linux报错:C++ compiler too old, need g++ 4.9.4 or clang++ 3.4.2 (CXX=g++)

    发布:2020-06-01 阅读(345)

  • linux下安装nodejs

    linux下如何安装nodejs

    发布:2020-06-01 阅读(324)

  • CentOS7 linux下yum安装redis以及使用

    linux下安装 redis,以及相关使用

    发布:2020-06-01 阅读(245)

  • linux下通过yum安装mongodb

    linux下通过yum安装mongodb

    发布:2020-05-31 阅读(277)

  • Linux rpm命令详解

    RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序

    发布:2019-08-02 阅读(567)

  • Linux中apt与apt-get命令的区别与解释

    Ubuntu 16.04 发布时,一个引人注目的新特性便是 apt 命令的引入。其实早在 2014 年,apt 命令就已经发布了第一个稳定版,只是直到 2016 年的 Ubuntu 16.04 系统发布时才开始引人关注。随着 apt install package 命令的使用频率和普遍性逐步超过 apt-get install package,越来越多的其它 Linux 发行版也开始遵循 Ubuntu 的脚步,开始鼓励用户使用 apt 而不是 apt-get。

    发布:2019-03-04 阅读(792)

  • centos7设置、查看、删除环境变量的方法

    centos查看环境变量与设置环境变量在使用过程中很常见,本文整理了一些常用的与环境变量相关的命令,感兴趣的朋友可以参考下希望对你有所帮助

    发布:2018-12-27 阅读(2333)

  • linux CentOS 安装oh my Zsh

    linux cenos下安装oh my zsh,oh my zsh 用于管理zsh配置,使shell界面更美观。 Zsh 是一款功能强大终端(shell)软件,既可以作为一个交互式终端,也可以作为一个脚本解释器。它在兼容 Bash 的同时 (默认不兼容,除非设置成 emulate sh) 还有提供了很多改进。

    发布:2018-12-20 阅读(2803)

  • linux云服务通过Nginx绑定多个域名

    通过Nginx反向代理,在一个云服务器上绑定多个域名,不同的域名访问不同的目录,端口

    发布:2018-10-11 阅读(1115)

  • CentOS 7上安装Nginx

    如何在Linux(CentOS 7)上安装Nginx,作为反向代理服务器。

    发布:2018-10-11 阅读(1046)

  • tail命令详情

    命令从指定点开始将 File 参数指定的文件写到标准输出。如果没有指定文件,那么会使用标准输入。Number 变量指定将多少单元写到标准输出。Number 变量的值可以是正的或负的整数。如果值的前面有 +(加号),从文件开头指定的单元数开始将文件写到标准输出。如果值的前面有 -(减号),那么从文件末尾指定的单元数开始将文件写到标准输出。如果值前面没有 +(加号)或 -(减号),那么从文件末尾指定的单元号开始读取文件

    发布:2017-05-23 阅读(1109)