0.学习链接
1.安装与启动
1.1 下载
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.1.tgz
1.2 解压
tar -zxvf mongodb-linux-x86_64-4.0.1.tgz
1.3 给解压出来的文件夹换个名字
cp -rp mongodb-linux-x86_64-4.0.1 mongodb
1.4 添加配置文件
cd /home/muten/module/MongoDb/mongodb/bin
vim mongodb.conf
dbpath=/root/apps/mongodb/db
logpath=/root/apps/mongodb/logs/mongodb.log
port=27017
1.5 加默认的数据库文件夹
mkdir /data/db
1.6 启动服务器端程序(可注意下权限)
cd /home/muten/module/MongoDb/mongodb/bin
./mongod
1.7 服务器端启动成功的日志
[root@localhost bin]# ./mongod
2021-08-14T09:15:26.416+0800 I CONTROL [initandlisten] MongoDB starting : pid=13253 port=27017 dbpath=/data/db 64-bit host=localhost.localdomain
2021-08-14T09:15:26.416+0800 I CONTROL [initandlisten] db version v4.0.1
2021-08-14T09:15:26.416+0800 I CONTROL [initandlisten] git version: 54f1582fc6eb01de4d4c42f26fc133e623f065fb
2021-08-14T09:15:26.416+0800 I CONTROL [initandlisten] allocator: tcmalloc
2021-08-14T09:15:26.416+0800 I CONTROL [initandlisten] modules: none
2021-08-14T09:15:26.416+0800 I CONTROL [initandlisten] build environment:
2021-08-14T09:15:26.416+0800 I CONTROL [initandlisten] distarch: x86_64
2021-08-14T09:15:26.416+0800 I CONTROL [initandlisten] target_arch: x86_64
2021-08-14T09:15:26.416+0800 I CONTROL [initandlisten] options: {}
2021-08-14T09:15:26.419+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=2453M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2021-08-14T09:15:26.954+0800 I STORAGE [initandlisten] WiredTiger message [1628903726:954669][13253:0x7fa3293109c0], txn-recover: Set global recovery timestamp: 0
2021-08-14T09:15:26.958+0800 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten]
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten]
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten]
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten]
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2021-08-14T09:15:26.974+0800 I CONTROL [initandlisten]
2021-08-14T09:15:26.974+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2021-08-14T09:15:26.974+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2021-08-14T09:15:26.974+0800 I CONTROL [initandlisten]
2021-08-14T09:15:26.974+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 23590 processes, 1048576 files. Number of processes should be at least 524288 : 0.5 times number of files.
2021-08-14T09:15:26.974+0800 I CONTROL [initandlisten]
2021-08-14T09:15:26.974+0800 I STORAGE [initandlisten] createCollection: admin.system.version with provided UUID: c6e60513-7fe0-4497-98ae-3c3b44b54ec2
2021-08-14T09:15:26.979+0800 I COMMAND [initandlisten] setting featureCompatibilityVersion to 4.0
2021-08-14T09:15:26.982+0800 I STORAGE [initandlisten] createCollection: local.startup_log with generated UUID: 79207302-0f63-45e3-b8e4-ee64fd7a37c2
2021-08-14T09:15:27.003+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2021-08-14T09:15:27.004+0800 I NETWORK [initandlisten] waiting for connections on port 27017
2021-08-14T09:15:27.004+0800 I STORAGE [LogicalSessionCacheRefresh] createCollection: config.system.sessions with generated UUID: 7efbba96-58ae-4857-ba52-1ab837966500
2021-08-14T09:15:27.013+0800 I INDEX [LogicalSessionCacheRefresh] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 }
2021-08-14T09:15:27.013+0800 I INDEX [LogicalSessionCacheRefresh] building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2021-08-14T09:15:27.014+0800 I INDEX [LogicalSessionCacheRefresh] build index done. scanned 0 total records. 0 secs
1.8 启动客户端
[muten@localhost bin]$ cd /home/muten/module/MongoDb/mongodb/bin
[muten@localhost bin]$ ./mongo
1.9 客户端启动成功的样子
[muten@localhost bin]$ ./mongo
MongoDB shell version v4.0.1
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.1
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:
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten]
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten]
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten]
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten]
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2021-08-14T09:15:26.973+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2021-08-14T09:15:26.974+0800 I CONTROL [initandlisten]
2021-08-14T09:15:26.974+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2021-08-14T09:15:26.974+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2021-08-14T09:15:26.974+0800 I CONTROL [initandlisten]
2021-08-14T09:15:26.974+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 23590 processes, 1048576 files. Number of processes should be at least 524288 : 0.5 times number of files.
2021-08-14T09:15:26.974+0800 I CONTROL [initandlisten]
>
2.程序参数说明和其他说明
2.1 服务器端的参数说明
/home/muten/module/MongoDb/mongodb/bin/mongod --help
[root@localhost bin]# /home/muten/module/MongoDb/mongodb/bin/mongod --help
Options:
General options:
-v [ --verbose ] [=arg(=v)] be more verbose (include multiple times
for more verbosity e.g. -vvvvv)
--quiet quieter output
--port arg specify port number - 27017 by default
--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 syslog
message
--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
--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
--shutdown kill a running server (for init
scripts)
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 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
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
--directoryperdb each database will be stored in a
separate directory
--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)
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
[root@localhost bin]#
--dbpath 指定数据库的目录
--port 指定数据库的端口,默认是27017
--bind_ip 绑定IP
--directoryperdb 为每个db创建一个独立子目录
--logpath 指定日志存放目录
--logappend 指定日志生成方式(追加/覆盖)
--pidfilepath 指定进程文件路径,如果不指定,将不产生进程文件
--keyFile 集群模式的关键标识
--journal 启用日志
--nssize 指定.ns文件的大小,单位MB,默认是16M,最大是2GB
--maxConns 最大的并发连接数
--notablescan 不允许进行表扫描
--noprealloc 关闭数据文件的预分配功能
--fork 以后台 Daemon形式运行服务
2.2 客户端的参数说明
/home/muten/module/MongoDb/mongodb/bin/mongo --help
[root@localhost bin]# /home/muten/module/MongoDb/mongodb/bin/mongo --help
MongoDB shell version v4.0.1
usage: /home/muten/module/MongoDb/mongodb/bin/mongo [options] [db address] [file names (ending in .js)]
db address can be:
foo foo database on local machine
192.168.0.5/foo foo database on 192.168.0.5 machine
192.168.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999
Options:
--shell run the shell after executing files
--nodb don't connect to mongod on startup - no
'db address' arg expected
--norc will not run the ".mongorc.js" file on
start up
--quiet be less chatty
--port arg port to connect to
--host arg server to connect to
--eval arg evaluate javascript
-h [ --help ] show this usage information
--version show version information
--verbose increase verbosity
--ipv6 enable IPv6 support (disabled by default)
--disableJavaScriptJIT disable the Javascript Just In Time
compiler
--enableJavaScriptJIT enable the Javascript Just In Time
compiler
--disableJavaScriptProtection allow automatic JavaScript function
marshalling
--retryWrites automatically retry write operations upon
transient network errors
--jsHeapLimitMB arg set the js scope's heap size limit
Authentication Options:
-u [ --username ] arg username for authentication
-p [ --password ] arg password for authentication
--authenticationDatabase arg user source (defaults to dbname)
--authenticationMechanism arg authentication mechanism
--gssapiServiceName arg (=mongodb) Service name to use when authenticating
using GSSAPI/Kerberos
--gssapiHostName arg Remote host name to use for purpose of
GSSAPI/Kerberos authentication
file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified
2.3 用kill -2来杀死MongoDB进程
不要用kill -9 PID来杀死MongoDB进程,这样可能回导致MongoDB的数据损坏,用kill -2杀死进程.
在Linux中用Kill-2和Kill-9都能够结束进程,他们的区别为:
Kill -2:功能类似于Ctrl+C是程序在结束之前,能够保存相关数据,然后再退出。
Kill -9:直接强制结束程序。
在用nohup挂起程序时,当想要结束这个程序,最好用kill -2
2.4 可视化工具
Navicat for MongoDB http://www.navicat.com.cn/what-is-navicat-for-mongodb
MongoDB Compass Community https://www.mongodb.com/download-center/compass
NoSQLBooster(mongobooster) https://nosqlbooster.com/downloads
ClusterControl https://severalnines.com/download-clustercontrol-database-management-system
Mongo Management Studio http://mms.litixsoft.de/index.php?lang=de/
Nosqlclient https://github.com/nosqlclient/nosqlclient