mongodb安装和使用

294 阅读2分钟

下载和解压缩

  1. 下载
  2. 解压缩 无需安装,直接可以使用。

启动服务器

先切换到安装目录

cd /Users/gongzhihao/Downloads/mongodb-macos-x86_64-4.2.3/bin

切换成功


gongzhihaodeMacBook-Pro:/ gongzhihaocd /Users/gongzhihao/Downloads/mongodb-macos-x86_64-4.2.3/bin

gongzhihaodeMacBook-Pro:bin gongzhihao$ ll

-bash: ll: command not found

gongzhihaodeMacBook-Pro:bin gongzhihao$ ls

bsondump    install_compass    mongo        mongod        mongodump    mongoexport    mongofiles    mongoimport    mongoreplay    mongorestore    mongos        mongostat    mongotop

gongzhihaodeMacBook-Pro:bin gongzhihao$


启动服务器

./mongod

启动成功


gongzhihaodeMacBook-Pro:bin gongzhihao$ ./mongod

2020-03-05T17:56:08.227+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'

2020-03-05T17:56:08.275+0800 I CONTROL [initandlisten] MongoDB starting : pid=23712 port=27017 dbpath=/data/db 64-bit host=gongzhihaodeMacBook-Pro.local

2020-03-05T17:56:08.275+0800 I CONTROL [initandlisten] db version v4.2.3

2020-03-05T17:56:08.275+0800 I CONTROL [initandlisten] git version: 6874650b362138df74be53d366bbefc321ea32d4

2020-03-05T17:56:08.275+0800 I CONTROL [initandlisten] allocator: system

2020-03-05T17:56:08.275+0800 I CONTROL [initandlisten] modules: none

2020-03-05T17:56:08.275+0800 I CONTROL [initandlisten] build environment:

2020-03-05T17:56:08.275+0800 I CONTROL [initandlisten] distarch: x86_64

2020-03-05T17:56:08.275+0800 I CONTROL [initandlisten] target_arch: x86_64

2020-03-05T17:56:08.275+0800 I CONTROL [initandlisten] options: {}

2020-03-05T17:56:08.278+0800 I STORAGE [initandlisten] exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /data/db, terminating

2020-03-05T17:56:08.279+0800 I NETWORK [initandlisten] shutdown: going to close listening sockets...

2020-03-05T17:56:08.279+0800 I - [initandlisten] Stopping further Flow Control ticket acquisitions.

2020-03-05T17:56:08.279+0800 I CONTROL [initandlisten] now exiting

2020-03-05T17:56:08.279+0800 I CONTROL [initandlisten] shutting down with code:100

gongzhihaodeMacBook-Pro:bin gongzhihao$ sudo chown -R xxx /data/db

chown: xxx: illegal user name

gongzhihaodeMacBook-Pro:bin gongzhihao$ sudo chown -R gongzhihao /data/db

gongzhihaodeMacBook-Pro:bin gongzhihao$

gongzhihaodeMacBook-Pro:bin gongzhihao$ ./mongod

2020-03-05T18:01:40.189+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'

2020-03-05T18:01:40.206+0800 I CONTROL [initandlisten] MongoDB starting : pid=24482 port=27017 dbpath=/data/db 64-bit host=gongzhihaodeMacBook-Pro.local

2020-03-05T18:01:40.206+0800 I CONTROL [initandlisten] db version v4.2.3

2020-03-05T18:01:40.206+0800 I CONTROL [initandlisten] git version: 6874650b362138df74be53d366bbefc321ea32d4

2020-03-05T18:01:40.206+0800 I CONTROL [initandlisten] allocator: system

2020-03-05T18:01:40.206+0800 I CONTROL [initandlisten] modules: none

2020-03-05T18:01:40.206+0800 I CONTROL [initandlisten] build environment:

2020-03-05T18:01:40.206+0800 I CONTROL [initandlisten] distarch: x86_64

2020-03-05T18:01:40.206+0800 I CONTROL [initandlisten] target_arch: x86_64

2020-03-05T18:01:40.206+0800 I CONTROL [initandlisten] options: {}

2020-03-05T18:01:40.209+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=7680M,cache_overflow=(file_max=0M),session_max=33000,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,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress],

2020-03-05T18:01:40.688+0800 I STORAGE [initandlisten] WiredTiger message [1583402500:688642][24482:0x7fff88c31380], txn-recover: Set global recovery timestamp: (0, 0)

2020-03-05T18:01:40.734+0800 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)

2020-03-05T18:01:40.772+0800 I STORAGE [initandlisten] Timestamp monitor starting

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten]

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten]

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten]

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten]

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000

2020-03-05T18:01:40.789+0800 I STORAGE [initandlisten] createCollection: admin.system.version with provided UUID: df5876fe-fbff-473d-aaa0-15fc9d10952d and options: { uuid: UUID("df5876fe-fbff-473d-aaa0-15fc9d10952d") }

2020-03-05T18:01:40.825+0800 I INDEX [initandlisten] index build: done building index _id_ on ns admin.system.version

2020-03-05T18:01:40.831+0800 I SHARDING [initandlisten] Marking collection admin.system.version as collection version: <unsharded>

2020-03-05T18:01:40.832+0800 I COMMAND [initandlisten] setting featureCompatibilityVersion to 4.2

2020-03-05T18:01:40.834+0800 I SHARDING [initandlisten] Marking collection local.system.replset as collection version: <unsharded>

2020-03-05T18:01:40.845+0800 I STORAGE [initandlisten] Flow Control is enabled on this deployment.

2020-03-05T18:01:40.851+0800 I SHARDING [initandlisten] Marking collection admin.system.roles as collection version: <unsharded>

2020-03-05T18:01:40.854+0800 I STORAGE [initandlisten] createCollection: local.startup_log with generated UUID: ec7450d1-c519-4fff-9b2b-9b4cbe4b4647 and options: { capped: true, size: 10485760 }

2020-03-05T18:01:40.879+0800 I INDEX [initandlisten] index build: done building index _id_ on ns local.startup_log

2020-03-05T18:01:40.879+0800 I SHARDING [initandlisten] Marking collection local.startup_log as collection version: <unsharded>

2020-03-05T18:01:40.879+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'

2020-03-05T18:01:40.887+0800 I SHARDING [LogicalSessionCacheReap] Marking collection config.system.sessions as collection version: <unsharded>

2020-03-05T18:01:40.887+0800 I NETWORK [listener] Listening on /tmp/mongodb-27017.sock

2020-03-05T18:01:40.887+0800 I NETWORK [listener] Listening on 127.0.0.1

2020-03-05T18:01:40.887+0800 I NETWORK [listener] waiting for connections on port 27017

2020-03-05T18:01:40.890+0800 I CONTROL [LogicalSessionCacheReap] Sessions collection is not set up; waiting until next sessions reap interval: config.system.sessions does not exist

2020-03-05T18:01:40.891+0800 I STORAGE [LogicalSessionCacheRefresh] createCollection: config.system.sessions with provided UUID: 7b40652f-5b52-40ea-8d75-997e0597c48c and options: { uuid: UUID("7b40652f-5b52-40ea-8d75-997e0597c48c") }

2020-03-05T18:01:40.921+0800 I INDEX [LogicalSessionCacheRefresh] index build: done building index _id_ on ns config.system.sessions

2020-03-05T18:01:40.947+0800 I INDEX [LogicalSessionCacheRefresh] index build: starting on config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 } using method: Hybrid

2020-03-05T18:01:40.947+0800 I INDEX [LogicalSessionCacheRefresh] build may temporarily use up to 200 megabytes of RAM

2020-03-05T18:01:40.948+0800 I INDEX [LogicalSessionCacheRefresh] index build: collection scan done. scanned 0 total records in 0 seconds

2020-03-05T18:01:40.949+0800 I INDEX [LogicalSessionCacheRefresh] index build: inserted 0 keys from external sorter into index in 0 seconds

2020-03-05T18:01:40.956+0800 I INDEX [LogicalSessionCacheRefresh] index build: done building index lsidTTLIndex on ns config.system.sessions

2020-03-05T18:01:41.006+0800 I SHARDING [ftdc] Marking collection local.oplog.rs as collection version: <unsharded>

启动客户端-自带命令行客户端

启动客户端,连接服务器。

./mongo

启动成功


gongzhihaodeMacBook-Pro:bin gongzhihao$ ./mongo

MongoDB shell version v4.2.3

connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("09f887b4-eca7-4428-acbf-8a2460063d0a") }

MongoDB server version: 4.2.3

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:

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten]

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten]

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten]

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten]

2020-03-05T18:01:40.774+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000

---

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

---

\


> //出现命令行提示符,现在可以在命令行敲命令

>


客户端默认连接到test数据库,输入db命令,然后回车就会显示当前连接数据库是test。

创建数据库(默认数据库是test)、集合(其实就是关系数据库的表)和文档(其实就是记录)

>
> db
test
>
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
>
> show collections
>
> db.mycol2.insert({"name" : "菜鸟教程"})
WriteResult({ "nInserted" : 1 })
>
> show collections
mycol2
>

参考

www.runoob.com/mongodb/mon…


《mongodb权威指南》第二版

安装

客户端