MongoDB一步一步来

阿里云ECS centos 7.4

使用yum安装MongoDB

新建一个文件/etc/yum.repos.d/mongodb-org-4.0.repo

[root@localhost ~]# touch /etc/yum.repos.d/mongodb-org-4.0.repo

编辑文件内容

[root@localhost ~]# vim /etc/yum.repos.d/mongodb-org-4.0.repo

粘贴以下内容

[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc

:wq保存退出

开始安装

[root@localhost ~]# sudo yum install -y mongodb-org

耐心等待安装完成
启动MongoDB服务

[root@localhost ~]# service mongod start

停止MongoDB服务

[root@localhost ~]# service mongod stop

重启MongoDB服务

[root@localhost ~]# service mongod restart

添加开机自动启动

[root@localhost ~]# sudo chkconfig mongod on

配置MongoDB

[root@localhost ~]# mongo
MongoDB shell version v4.0.3
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("****-****-****-****-****") }
MongoDB server version: 4.0.3
Server has startup warnings:
2018-10-19T13:39:33.345+0800 I STORAGE  [initandlisten]
2018-10-19T13:39:33.345+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-10-19T13:39:33.345+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-10-19T13:39:34.007+0800 I CONTROL  [initandlisten]
2018-10-19T13:39:34.007+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-10-19T13:39:34.007+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-10-19T13:39:34.007+0800 I CONTROL  [initandlisten]
2018-10-19T13:39:34.007+0800 I CONTROL  [initandlisten]
2018-10-19T13:39:34.007+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-10-19T13:39:34.007+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-10-19T13:39:34.007+0800 I CONTROL  [initandlisten]
2018-10-19T13:39:34.007+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-10-19T13:39:34.007+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-10-19T13:39:34.007+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()
---
>

关闭 CentOS 7 的 Transparent Huge Pages(THP)

WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'
WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'

这两个问题是CentOS7特有的,因为从CentOS7版本开始会默认启用Transparent Huge Pages(THP)
Transparent Huge Pages(THP)本意是用来提升内存性能,但某些数据库厂商还是建议直接关闭THP(比如说Oracle、MariaDB、MongoDB等),否则可能会导致性能出现下降。

查看THP状态

[root@localhost ~]#  cat /sys/kernel/mm/transparent_hugepage/defrag
[always] madvise never
[root@localhost ~]# cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never

修改系统配置

[root@localhost ~]# vim /etc/rc.d/rc.local

在后边添加

# for MongoDB , disable thp
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag

然后:wq保存退出
修改文件权限

[root@localhost ~]# chmod +x /etc/rc.d/rc.local

重启虚拟机

[root@localhost ~]# reboot

再次查看 THP 状态

[root@localhost ~]#  cat /sys/kernel/mm/transparent_hugepage/defrag
always madvise [never]
[root@localhost ~]# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]

已经改为了禁用 THP

配置 MongoDB 的身份验证

WARNING: Access control is not enabled for the database

报这个错是因为MongoDB需要有一个安全库来开启数据库访问控制

在MongoDB部署上启用访问控制会强制执行身份验证,要求用户识别自己。当访问启用了访问控制的MongoDB部署时,用户只能执行由其角色确定的操作。

在admin数据库中创建root 用户

> use admin
switched to db admin
> db.createUser({ user: "myUserAdmin",pwd: "abc123",roles: [{ role: "userAdminAnyDatabase", db: "admin"  }] })
Successfully added user: {
    "user" : "myUserAdmin",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}
> exit
bye

MongoDB 配置文件

[root@localhost ~]# vim /etc/mongod.conf
net:
  port: 27017
  bindIpAll: true

security:
  authorization: enabled

添加数据库

在 mongo shell 下

> use test

为新数据库添加用户

> db.createUser({user:"test",pwd:"123456",roles:[{role:"readWrite",db:"test"}]})
    原文作者:Quincy_X
    原文地址: https://www.jianshu.com/p/d130c19d91b4
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞