【数据库】Linux下MongoDB的安装和配置

MongoDB安装

选择使用Yum安装javascript

一、制做 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/html

二、使用yum 命令安装

yum install -y mongodb-org

三、启动mongodb

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

启动、中止、重启命令以下:web

service mongod start
service mongod stop
service mongod restart

四、开放mongodb的远程链接

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

若是要开放远程访问须要修改该文件的 bindIp值为: 0.0.0.0 ,不然经过其它电脑是链接不到的shell

vim /etc/mongod.conf

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

service mongod restart

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

firewall-cmd --state

关闭防火墙状态centos

systemctl stop firewalld.service

防火墙放开 27017 端口api

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

测试是否能够远程链接

http://服务器ip:27017/

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

五、建立用户和密码

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:
# http://docs.mongodb.org/manual/reference/configuration-options/

# 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架构 集群中使用,暂时没有接触
相关文章
相关标签/搜索