Linux下MongoDB的安装和配置教程

MongoDB安装

选择使用Yum安装

1、制作 repo 文件

  cat << EOF > /etc/yum.repos.d/mongodb-org-4.2.repo  [mongodb-org-4.2]  name=MongoDB Repository  baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/x86_64/  gpgcheck=1  enabled=1  gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc  EOF  12345678

baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/4.2/x86_64/安装失败,尝试把地址写死为7,安装基于centos7的版本。可以成功安装
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/

2、使用yum 命令安装

yum install -y mongodb-org

3、启动mongodb

安装完启动服务则可以使用

启动、停止、重启命令如下:

  service mongod start  service mongod stop  service mongod restart

4、开放mongodb的远程连接

mongodb的配置文件是 /etc/mongod.conf

如果要开放远程访问需要修改该文件的 bindIp值为: 0.0.0.0 ,否则通过其它电脑是连接不到的

vim /etc/mongod.conf

文件修改后要执行 restart 使配置生效

service mongod restart

如果仍不能远程连接,查看防火墙状态,如果防火墙开启,关闭防火墙或让防火墙放开 27017 端口(该端口是mongodb的默认端口,可通过配置文件修改mongodb的端口)
查看防火墙状态

firewall-cmd --state  

关闭防火墙状态

systemctl stop firewalld.service

防火墙放开 27017 端口

firewall-cmd --permanent --zone=public --add-port=27017/tcp
firewall-cmd --reload

测试是否可以远程连接

http://服务器ip:27017/

阿里云服务器则需要添加端口得安全组

5、创建用户和密码

1.进入mongo shell

[root@iZ2ze1wbnx7ym2bkq1xtk5Z conf.d]# mongo
MongoDB shell version v4.2.8
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("73551ca3-8d61-4ce2-a5d1-c0563f9828d4") }
MongoDB server version: 4.2.8
Server has startup warnings:
2020-07-01T15:24:12.665+0800 I  CONTROL  [initandlisten]
2020-07-01T15:24:12.665+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-07-01T15:24:12.665+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-07-01T15:24:12.665+0800 I  CONTROL  [initandlisten]
2020-07-01T15:24:12.665+0800 I  CONTROL  [initandlisten]
2020-07-01T15:24:12.665+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-07-01T15:24:12.665+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-07-01T15:24:12.665+0800 I  CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

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()
---

>

2.切换到admin数据库

admin这个库是mongodb自动带的,专门管理用户和权限的,创建超级用户,这个用户可以管理所有用户的增删改以及权限控制

> use admin
switched to db admin

3.添加账户

创建一个超级管理员权限(拥有userAdminAnyDatabasereadWriteAnyDatabase两个权限)的用户。用户名和密码随便写,但是角色必须是这两个

  db.createUser(   {   user: "alenghan", pwd: "123456", roles: [  {   role: "userAdminAnyDatabase", db: "admin"   },  "readWriteAnyDatabase"   ]   }  )

注:``db.createUser()`的具体使用方法:

创建完成就可以使用命令链接

mongo --port 27017 -u "alenghan" --authenticationDatabase "admin" -p 123456

4.修改mongo.conf文件

停止mongodb服务(service mongod stop),修改配置文件(/etc/mongod.conf

# mongod.conf
# for documentation of all options, see:
#

# where to write logging data.
systemLog: #系统日志
  destination: file #日志输出目的地
  logAppend: true # 如果为true,当mongod/mongos重启后,将在现有日志的尾部继续添加日志。否则,将会备份当前日志文件,然后创建一个新的日志文件;默认为false。
  path: /var/log/mongodb/mongod.log #日志路径

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo # mongod进程存储数据目录,此配置仅对mongod进程有效
  journal:
    enabled: true #是否开启journal日志持久存储,journal日志用来数据恢复,是mongod最基础的特性,通常用于故障恢复。64位系统默认为true,32位默认为false,建议开启,仅对mongod进程有效。
#  engine: #存储引擎类型,mongodb 3.0之后支持“mmapv1”、“wiredTiger”两种引擎,默认值为“mmapv1”;官方宣称wiredTiger引擎更加优秀。
#  wiredTiger: #对wiredTiger引擎配置生效

# how the process runs
processManagement:
  fork: true  # fork and run in background 运行在后台
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile PID文件路径
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017 #端口
  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. 绑定外网op 多个用逗号分隔,如果开放全部外网访问, 输入0.0.0.0
  # maxIncomingConnections: 65536  #进程允许的最大连接数 默认值为65536
  # wireObjectCheck: true #当客户端写入数据时 检测数据的有效性(BSON) 默认值为true
 
#security: #安全有关的配置
  #authorization: enabled #disabled或者enabled,仅对mongod有效;表示是否开启用户访问控制(Access Control),即客户端可以通过用户名和密码认证的方式访问系统的数据,默认为“disabled”,即客户端不需要密码即可访问数据库数据。(限定客户端与mongod、mongos的认证)
  #javascriptEnabled: true #true或者false,默认为true,仅对mongod有效;表示是否关闭server端的javascript功能,就是是否允许mongod上执行javascript脚本,如果为false,那么mapreduce、group命令等将无法使用,因为它们需要在mongod上执行javascript脚本方法。如果你的应用中没有mapreduce等操作的需求,为了安全起见,可以关闭javascript。
 
#operationProfiling: #性能分析器
  #slowOpThresholdMs: 100 #数据库profiler判定一个操作是“慢查询”的时间阀值,单位毫秒;
  #mode: off #数据库profiler级别,操作的性能信息将会被写入日志文件中,
  # 可选值:1)off:关闭profiling
  #       2)slowOp:on,只包含慢操作日志
  #       3)all:on,记录所有操作
  # 数据库profiling会影响性能,建议只在性能调试阶段开启。此参数仅对mongod有效。
 
#replication: #主从复制 主备模式 这个是大点,需要单独讲
  #oplogSizeMB:10240 #replication操作日志的最大尺寸,单位:MB。

#sharding: #sharding架构 集群中使用,暂时没有接触

总结

到此这篇关于Linux下MongoDB的安装和配置的文章就介绍到这了,更多相关Linux下MongoDB的安装和配置内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

参与评论