- 啓動mongo
終端輸入mongod,有一些啓動選項,可以通過 --help查看。
--dbpath
指定數據目錄,mac默認是/data/db下,每個mongod進程都需要獨立的數據目錄。當mongod啓動時,會在數據目錄中創建mongod.lock文件。這個文件用於防止其他mongod進程使用該數據目錄。若當有一個mongd啓動後,再啓動另一個mongod時,若再使用剛剛已經啓動mongod的目錄,那麼會報錯。
--port
指定端口號,默認27017
–fork
後臺運行,等同於nohup … &
–logpath
指定日誌輸出目錄,如果想保留原來的日誌,還需要使用–logappend
--config or -f
指定配置文件,配置文件具體另外再說
--directoryperdb
使數據庫的文件分目錄保存
--auth
數據庫驗證啓動,訪問修改數據庫必須用戶驗證,命令行如下:
> use admin
> db.auth(user,pwd)
-v
-vv
-vvv
-vvvv
-vvvvv
指定日誌記錄的級別,最高5級
-
停止mongo
停止有幾種方法,介紹兩種常用的
1.直接在命令窗口ctrl+c
2.mongo登錄以後,調用db.shutdownServer() -
添加用戶
Mongodb 基於 role-based access control (RBAC) 角色訪問控制來管理數據庫,用戶被授予若干個角色,這些角色決定了用戶訪問MongoDB資源和操作的權限。常規用戶創建,基本需要兩個權限,創建用戶的 createUser,分配角色的grantRole。
對應userAdmin 和 userAdminAnyDatabase
1.如何創建用戶
第一次配置好,從終端登錄mongo。
show dbs 查看數據庫,默認有admin和local兩個db。
> use admin
> db.createUser({user:“root”, pwd:“123123”, roles:[{role:“userAdminAnyDatabase”, db:“admin”}]})
這樣就創建了一個用戶 root, 並且賦予了 userAdminAnyDatabase 的角色, admin db是mongo的默認用戶庫,訪問操作數據庫的用戶都必須在這裏用 db.createUser 接口添加。
2.驗證用戶
> db.auth(“root”, “123123”)
1
3.更新用戶 角色或密碼
> db.updateUser(“root”, {roles:[{role:“userAdminAnyDatabase”, db:“admin”}, {role:“readWrite”, db:“counts”}]})
> db.updateUser(“root”, {pwd:“123456”})
出現如上面所示的提示,就證明你驗證成功了,鏈接db時候可以使用,mongodb://root:123123@localhost:27017
命令行驗證連接mongo
mongo -u username -p password
4.查看用戶
>use admin
>show users
>use admin
>db.system.users.find()
5.用戶 mongo角色表_文檔
Database User Roles | 數據庫用戶角色 |
---|---|
read | 提供讀取所有非系統集合和以下系統集合上的數據的能力:system.indexes, system.js, 和 system.namespaces |
readWrite | 提供read角色的所有特權,以及修改所有非系統集合和system.js集合上的數據的能力。 |
Database Administration Roles | 數據庫管理角色 |
---|---|
dbAdmin | 提供執行管理任務的能力,例如與模式相關的任務、索引和收集統計信息。此角色不爲用戶和角色管理授予特權。 |
dbOwner | 數據庫所有者可以對數據庫執行任何管理操作。此角色組合了 readWrite, dbAdmin 和 userAdmin。 |
userAdmin | 提供在當前數據庫上創建和修改角色和用戶的功能。由於 userAdmin 允許用戶向任何用戶(包括他們自己)授予任何特權,所以該角色還間接地提供了對數據庫或集羣的超級用戶訪問(如果範圍限定爲管理數據庫)。 |
Cluster Administration Roles | 集羣管理角色 |
---|---|
clusterAdmin | 提供最大的集羣管理訪問。這個角色組合了clusterManager、clusterMonitor和hostManager角色授予的特權。此外,該角色還提供dropDatabase操作。 |
clusterManager | 提供集羣上的管理和監視操作。具有此角色的用戶可以分別訪問分片和複製中使用的配置和本地數據庫。 |
clusterMonitor | 提供對監視工具的只讀訪問,如MongoDB雲管理器和Ops Manager監視代理。 |
hostManager | 提供監視和管理服務器的能力。 |
Backup and Restoration Roles | 備份和恢復角色 |
---|---|
backup | 提供備份數據所需的最小特權。該角色提供了足夠的特權來使用MongoDB雲管理器備份代理、Ops管理器備份代理或使用mongodump備份整個mongod實例。 |
restore | 版本3.6中的更改:在非系統集合上提供convertToCapped。提供從不包含系統的備份中還原數據所需的特權。資料收集數據。當使用mongorestore恢復數據而不使用 --oplogReplay 選項時,這個角色就足夠了。 |
All-Database Roles | 所有數據庫角色 |
---|---|
readAnyDatabase | 除本地和配置外,提供與讀取所有數據庫相同的只讀特權。該角色還提供了整個集羣上的listDatabases操作。在3.4版中更改:在3.4版之前,readAnyDatabase包括本地數據庫和配置數據庫。要在本地數據庫上提供讀權限,請在管理數據庫中創建一個具有本地數據庫中的read角色的用戶。 |
userAdminAnyDatabase | 提供與除本地和配置外的所有數據庫上的讀寫權限相同的讀寫權限。該角色還提供了整個集羣上的listDatabases操作。在3.4版中更改:在3.4版之前,readWriteAnyDatabase包括本地數據庫和配置數據庫。要在本地數據庫上提供讀寫權限,請在管理數據庫中創建具有本地數據庫中的readWrite角色的用戶。 |
dbAdminAnyDatabase | 對用戶管理操作的訪問與userAdmin對所有數據庫(本地和配置除外)的訪問相同。 userAdminAnyDatabase角色允許用戶向任何用戶授予任何特權,包括用戶本身,該角色還間接提供超級用戶訪問權。 在3.4版本中進行了更改:在3.4版本之前,userAdminAnyDatabase包括本地數據庫和配置數據庫。 |
dbAdminAnyDatabase | 在除本地和配置外的所有數據庫上提供與dbAdmin相同的特權。該角色還提供了整個集羣上的listDatabases操作。在3.4版中更改:在3.4版之前,dbAdminAnyDatabase包括本地數據庫和配置數據庫。要在本地數據庫上提供dbAdmin特權,請在管理數據庫中創建一個具有本地數據庫中的dbAdmin角色的用戶。 |
Superuser Roles | 超級用戶角色 |
---|---|
root | 提供對readWriteAnyDatabase、dbAdminAnyDatabase、usminanydatabase、clusterAdmin、restore和backup組合的操作和所有資源的訪問。 |
Internal Role | 內部角色 |
---|---|
__system | 提供對數據庫中的任何對象執行任何操作的特權。除非在特殊情況下,否則不要將此角色分配給表示應用程序或人工管理員的用戶對象。 |
mongo --help
General options:
一般操作
-v [ --verbose ] [=arg(=v)] be more verbose (include multiple times for more verbosity e.g. -vvvvv)
更詳細日誌等級(-v, -vv, -vvv, -vvvv, -vvvvv )v越多日誌越詳細
--quiet quieter output
靜默模式
--port arg specify port number - 27017 by default
端口號,默認27017
--logpath arg log file to send write to instead of stdout - has to be a file, not directory
日誌寫入路徑,必須是文件路徑,不能是文件夾路徑(啓動時若文件已存在會自動生成一個帶時間的新日誌文件)
--syslog log to system's syslog facility instead of file or stdout
系統級日誌
--syslogFacility arg syslog facility used for mongodb syslogmessage
--logappend append to logpath instead of over-writing
繼續寫入日誌文件,而不是重寫文件
--logRotate arg set the log rotation behavior (rename|reopen)
--timeStampFormat arg Desired format for timestamps in log messages. One of ctime, iso8601-utc or iso8601-local
日誌內容時間格式,iso8601-utc (標準 +0) 或者 iso8601-local(本地)
--setParameter arg Set a configurable parameter
-h [ --help ] show this usage information
查看使用幫助
--version show version information
查看版本信息
-f [ --config ] arg configuration file specifying additional options
--bind_ip arg comma separated list of ip addresses to listen on - localhost by default
--bind_ip_all bind to all ip addresses
--ipv6 enable IPv6 support (disabled by default)
--listenBacklog arg (=128) set socket listen backlog size
--maxConns arg max number of simultaneous connections - 1000000 by default
--pidfilepath arg full path to pidfile (if not set, no pidfile is created)
--timeZoneInfo arg full path to time zone info directory, e.g. /usr/share/zoneinfo
--keyFile arg private key for cluster authentication
--noauth run without security
--transitionToAuth For rolling access control upgrade. Attempt to authenticate over outgoing connections and proceed regardless of success. Accept incoming connections with or without authentication.
--clusterAuthMode arg Authentication mode used for cluster authentication. Alternatives are (keyFile|sendKeyFile|sendX509|x509)
--nounixsocket disable listening on unix sockets
--unixSocketPrefix arg alternative directory for UNIX domain sockets (defaults to /tmp)
--filePermissions arg permissions to set on UNIX domain socket file - 0700 by default
--fork fork server process
後臺啓動
--slowms arg (=100) value of slow for profile and console log
輸出最近固定條數的日誌
--slowOpSampleRate arg (=1) fraction of slow ops to include in the profile and console log
--networkMessageCompressors [=arg(=disabled)] (=snappy)
Comma-separated list of compressors to use for network messages
--auth run with security
帶用戶驗證的啓動
--clusterIpSourceWhitelist arg Network CIDR specification of permitted origin for `__system` access.
--profile arg 0=off 1=slow, 2=all
--cpu periodically show cpu and iowait utilization
--sysinfo print some diagnostic system information
--noIndexBuildRetry don't retry any index builds that were interrupted by shutdown
--noscripting disable scripting engine
--notablescan do not allow table scans
Replication options:
--oplogSize arg size to use (in MB) for replication op log. default is 5% of disk space (i.e.large is good)
--master Master/slave replication no longer supported
--slave Master/slave replication no longer supported
Replica set options:
--replSet arg arg is <setname>[/<optionalseedhostlist>]
--replIndexPrefetch arg specify index prefetching behavior (if secondary) [none|_id_only|all]
--enableMajorityReadConcern [=arg(=1)] (=1)
enables majority readConcern
Sharding options:
--configsvr declare this is a config db of a cluster; default port 27019; default dir /data/configdb
--shardsvr declare this is a shard db of a cluster; default port 27018
SSL options:
--sslOnNormalPorts use ssl on configured ports
--sslMode arg set the SSL operation mode
(disabled|allowSSL|preferSSL|requireSSL)
--sslPEMKeyFile arg PEM file for ssl
--sslPEMKeyPassword arg PEM file password
--sslClusterFile arg Key file for internal SSL authentication
--sslClusterPassword arg Internal authentication key file password
--sslCAFile arg Certificate Authority file for SSL
--sslClusterCAFile arg CA used for verifying remotes during outbound connections
--sslCRLFile arg Certificate Revocation List file for SSL
--sslDisabledProtocols arg Comma separated list of TLS protocols to disable [TLS1_0,TLS1_1,TLS1_2]
--sslWeakCertificateValidation allow client to connect without presenting a certificate
--sslAllowConnectionsWithoutCertificates
allow client to connect without presenting a certificate
--sslAllowInvalidHostnames Allow server certificates to provide non-matching hostnames
--sslAllowInvalidCertificates allow connections to servers with invalid certificates
--sslFIPSMode activate FIPS 140-2 mode at startup
--sslCertificateSelector arg SSL Certificate in system store
--sslClusterCertificateSelector arg SSL Certificate in system store for internal SSL authentication
Storage options:
--storageEngine arg what storage engine to use - defaults to wiredTiger if no data files present
--dbpath arg directory for datafiles - defaults to /data/db
啓動路徑,默認/data/db
--directoryperdb each database will be stored in a separate directory
分文件目錄保存db
--noprealloc disable data file preallocation - will often hurt performance
--nssize arg (=16) .ns file size (in MB) for new databases
--quota limits each database to a certain number of files (8 default)
--quotaFiles arg number of files allowed per db, implies --quota
--smallfiles use a smaller default file size
--syncdelay arg (=60) seconds between disk syncs (0=never, but not recommended)
--upgrade upgrade db if needed
--repair run repair on all dbs
--repairpath arg root directory for repair files - defaults to dbpath
--journal enable journaling
--nojournal disable journaling (journaling is on by default for 64 bit)
--journalOptions arg journal diagnostic options
--journalCommitInterval arg how often to group/batch commit (ms)
Free Monitoring options:
--enableFreeMonitoring arg Enable Cloud Free Monitoring (on|runtime|off)
--freeMonitoringTag arg Cloud Free Monitoring Tags
WiredTiger options:
--wiredTigerCacheSizeGB arg maximum amount of memory to allocate for cache; defaults to 1/2 of physical RAM
--wiredTigerJournalCompressor arg (=snappy)
use a compressor for log records [none|snappy|zlib]
--wiredTigerDirectoryForIndexes Put indexes and data in different directories
--wiredTigerCollectionBlockCompressor arg (=snappy)
block compression algorithm for collection data [none|snappy|zlib]
--wiredTigerIndexPrefixCompression arg (=1)
use prefix compression on row-store leaf pages