前言,以下是本人安裝mongodb4.0.5版本的操作 centos7.2系統(tǒng)
1、下載安裝包
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/RPMS/mongodb-org-server-4.0.5-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/RPMS/mongodb-org-shell-4.0.5-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/RPMS/mongodb-org-tools-4.0.5-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/RPMS/mongodb-org-mongos-4.0.5-1.el7.x86_64.rpm
2、安裝程序
安裝mongodb 服務(wù)端
rpm -ivh mongodb-org-server-4.0.5-1.el7.x86_64.rpm
如果還需要用命令行連接 mongoDB,則需要安裝 shell 程序包
rpm -ivh mongodb-org-shell-4.0.5-1.el7.x86_64.rpm
如果需要一些附加工具,例如數(shù)據(jù)導(dǎo)入導(dǎo)出旧蛾,則需要安裝 tool 程序包
rpm -ivh mongodb-org-tools-4.0.5-1.el7.x86_64.rpm
如果需要集群則需要安裝mongos
rpm -ivh mongodb-org-mongos-4.0.5-1.el7.x86_64.rpm
3、配置文件
安裝好了以后蠕嫁,會(huì)生成一個(gè) /etc/mongod.conf 的配置文件蚜点,配置了 mogoDB 默認(rèn)的配置。默認(rèn)的配置文件如下所示
# 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
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
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.
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
4拌阴、啟動(dòng)Mongodb
mongod -f /etc/mongod.conf
# 權(quán)限驗(yàn)證模式绍绘,只有權(quán)限驗(yàn)證通過才能訪問
mongod -f /etc/mongod.conf --auth
5、命令行使用
使用mongo命令連接顯示以下信息就連接成功了
# 連接
[root@zzapp-server-gp002 mongo]# mongo
MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("0301bb2a-0a7d-4a16-923d-3ec2946d08c6") }
MongoDB server version: 4.0.5
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2019-10-09T16:04:19.498+0800 I STORAGE [initandlisten]
2019-10-09T16:04:19.498+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-10-09T16:04:19.498+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-10-09T16:04:20.039+0800 I CONTROL [initandlisten]
2019-10-09T16:04:20.039+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-10-09T16:04:20.039+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-10-09T16:04:20.039+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-10-09T16:04:20.039+0800 I CONTROL [initandlisten]
2019-10-09T16:04:20.039+0800 I CONTROL [initandlisten]
2019-10-09T16:04:20.039+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-10-09T16:04:20.039+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-10-09T16:04:20.039+0800 I CONTROL [initandlisten]
2019-10-09T16:04:20.039+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-10-09T16:04:20.039+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-10-09T16:04:20.039+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()
---
>
# 全部參數(shù)的連接方法:
mongo ip:port/dbname -u 用戶名 -p 密碼
# 切換到相應(yīng)的數(shù)據(jù)庫(kù)迟赃,沒有則會(huì)創(chuàng)建新的
> use DBNAME
# 創(chuàng)建用戶并設(shè)置權(quán)限
> db.createUser( {user: "username",pwd: "password",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})
Successfully added user: {
"user" : "report_dev",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
說明:
user:用戶名
pwd:密碼
roles:指定用戶的角色陪拘,可以用一個(gè)空數(shù)組給新用戶設(shè)定空角色;在roles字段,可以指定內(nèi)置角色和用戶定義的角色纤壁。role里的角色可以選:
Built-In Roles(內(nèi)置角色):
1. 數(shù)據(jù)庫(kù)用戶角色:read左刽、readWrite;
2. 數(shù)據(jù)庫(kù)管理角色:dbAdmin、dbOwner酌媒、userAdmin欠痴;
3. 集群管理角色:clusterAdmin、clusterManager秒咨、clusterMonitor喇辽、hostManager;
4. 備份恢復(fù)角色:backup雨席、restore菩咨;
5. 所有數(shù)據(jù)庫(kù)角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase抽米、dbAdminAnyDatabase
6. 超級(jí)用戶角色:root
// 這里還有幾個(gè)角色間接或直接提供了系統(tǒng)超級(jí)用戶的訪問(dbOwner 特占、userAdmin、userAdminAnyDatabase)
7. 內(nèi)部角色:__system
具體角色:
Read:允許用戶讀取指定數(shù)據(jù)庫(kù)
readWrite:允許用戶讀寫指定數(shù)據(jù)庫(kù)
dbAdmin:允許用戶在指定數(shù)據(jù)庫(kù)中執(zhí)行管理函數(shù)云茸,如索引創(chuàng)建是目、刪除,查看統(tǒng)計(jì)或訪問system.profile
userAdmin:允許用戶向system.users集合寫入标捺,可以找指定數(shù)據(jù)庫(kù)里創(chuàng)建胖笛、刪除和管理用戶
clusterAdmin:只在admin數(shù)據(jù)庫(kù)中可用,賦予用戶所有分片和復(fù)制集相關(guān)函數(shù)的管理權(quán)限宜岛。
readAnyDatabase:只在admin數(shù)據(jù)庫(kù)中可用,賦予用戶所有數(shù)據(jù)庫(kù)的讀權(quán)限
readWriteAnyDatabase:只在admin數(shù)據(jù)庫(kù)中可用功舀,賦予用戶所有數(shù)據(jù)庫(kù)的讀寫權(quán)限
userAdminAnyDatabase:只在admin數(shù)據(jù)庫(kù)中可用萍倡,賦予用戶所有數(shù)據(jù)庫(kù)的userAdmin權(quán)限
dbAdminAnyDatabase:只在admin數(shù)據(jù)庫(kù)中可用,賦予用戶所有數(shù)據(jù)庫(kù)的dbAdmin權(quán)限辟汰。
root:只在admin數(shù)據(jù)庫(kù)中可用列敲。超級(jí)賬號(hào),超級(jí)權(quán)限帖汞。