- 启动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