MongoDB一步一步來(lái)

阿里云ECS centos 7.4

使用yum安裝MongoDB

新建一個(gè)文件/etc/yum.repos.d/mongodb-org-4.0.repo

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

編輯文件內(nèi)容

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

粘貼以下內(nèi)容

[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保存退出

開(kāi)始安裝

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

耐心等待安裝完成
啟動(dòng)MongoDB服務(wù)

[root@localhost ~]# service mongod start

停止MongoDB服務(wù)

[root@localhost ~]# service mongod stop

重啟MongoDB服務(wù)

[root@localhost ~]# service mongod restart

添加開(kāi)機(jī)自動(dòng)啟動(dòng)

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

關(guān)閉 CentOS 7 的 Transparent Huge Pages(THP)

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

這兩個(gè)問(wèn)題是CentOS7特有的,因?yàn)閺腃entOS7版本開(kāi)始會(huì)默認(rèn)啟用Transparent Huge Pages(THP)
Transparent Huge Pages(THP)本意是用來(lái)提升內(nèi)存性能,但某些數(shù)據(jù)庫(kù)廠商還是建議直接關(guān)閉THP(比如說(shuō)Oracle、MariaDB、MongoDB等),否則可能會(huì)導(dǎo)致性能出現(xiàn)下降。

查看THP狀態(tài)

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

修改系統(tǒng)配置

[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保存退出
修改文件權(quán)限

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

重啟虛擬機(jī)

[root@localhost ~]# reboot

再次查看 THP 狀態(tài)

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

已經(jīng)改為了禁用 THP

配置 MongoDB 的身份驗(yàn)證

WARNING: Access control is not enabled for the database

報(bào)這個(gè)錯(cuò)是因?yàn)镸ongoDB需要有一個(gè)安全庫(kù)來(lái)開(kāi)啟數(shù)據(jù)庫(kù)訪問(wèn)控制

在MongoDB部署上啟用訪問(wèn)控制會(huì)強(qiáng)制執(zhí)行身份驗(yàn)證,要求用戶(hù)識(shí)別自己。當(dāng)訪問(wèn)啟用了訪問(wèn)控制的MongoDB部署時(shí),用戶(hù)只能執(zhí)行由其角色確定的操作。

在admin數(shù)據(jù)庫(kù)中創(chuàng)建root 用戶(hù)

> 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

添加數(shù)據(jù)庫(kù)

在 mongo shell 下

> use test

為新數(shù)據(jù)庫(kù)添加用戶(hù)

> db.createUser({user:"test",pwd:"123456",roles:[{role:"readWrite",db:"test"}]})
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容