MongoDB使用学习笔记
1. MongoDB安装
2. MongoDB使用
(1)MongoDB的启停
-
参考博客:
-
启动:
-
停止: ./bin/mongod --dbpath ./data/db --shutdown
-
顺带提几句,参考博客里面说到停止mongoDB, 如果使用了杀死进程的方法,不能使用pkill,kill -9这些命令。
-
先上一篇参考博客:Mongodb入门系列(3)————kill -9 mongod之后mongodb服务无法开启问题 -
大概意思就是用了这两个命令后,就不能正常启动mongoDB服务了。而我上面讲到的那种停止的命令是可以正常再次启动mongoDB的。如果真的用了kill -9杀进程了,那可以通过“删除/data下的mongod.lock”解决。 -
实际上,我刚刚试着用kill -9杀进程,还是可以正常启动的……并且也能使用。待我多用一段时间看看有没有长进之后再看看这个情况吧。后续有什么新认识再补充回来。 [root@VM-20-8-centos mongdb]# ./bin/mongod --dbpath ./data/db --shutdown
{"t":{"$date":"2021-11-21T12:11:13.071+08:00"},"s":"I", "c":"NETWORK", "id":4915701, "ctx":"-","msg":"Initialized wire specification","attr":{"spec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":13},"outgoing":{"minWireVersion":0,"maxWireVersion":13},"isInternalClient":true}}}
{"t":{"$date":"2021-11-21T12:11:13.071+08:00"},"s":"I", "c":"CONTROL", "id":23285, "ctx":"-","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
{"t":{"$date":"2021-11-21T12:11:13.071+08:00"},"s":"W", "c":"ASIO", "id":22601, "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
{"t":{"$date":"2021-11-21T12:11:13.071+08:00"},"s":"I", "c":"NETWORK", "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
{"t":{"$date":"2021-11-21T12:11:13.073+08:00"},"s":"W", "c":"ASIO", "id":22601, "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
{"t":{"$date":"2021-11-21T12:11:13.073+08:00"},"s":"I", "c":"REPL", "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationDonorService","ns":"config.tenantMigrationDonors"}}
{"t":{"$date":"2021-11-21T12:11:13.073+08:00"},"s":"I", "c":"REPL", "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationRecipientService","ns":"config.tenantMigrationRecipients"}}
There doesn't seem to be a server running with dbpath: ./data/db
[root@VM-20-8-centos mongdb]# ./bin/mongod --dbpath ./data/db --fork --logpath=./logs/mongodb.log
about to fork child process, waiting until server is ready for connections.
forked process: 7367
child process started successfully, parent exiting
[root@VM-20-8-centos mongdb]# kill -9 7367
[root@VM-20-8-centos mongdb]# ./bin/mongod --dbpath ./data/db --fork --logpath=./logs/mongodb.log
about to fork child process, waiting until server is ready for connections.
forked process: 7545
child process started successfully, parent exiting
[root@VM-20-8-centos mongdb]# ./bin/mongo
MongoDB shell version v5.0.4
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("d9afca99-c310-4e8f-8aa7-af9ef8748fcb") }
MongoDB server version: 5.0.4
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
---
The server generated these startup warnings when booting:
2021-11-21T12:11:35.302+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2021-11-21T12:11:35.747+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2021-11-21T12:11:35.747+08:00: You are running this process as the root user, which is not recommended
2021-11-21T12:11:35.747+08:00: This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip <address> to specify which IP addresses it should serve responses from, or with --bind_ip_all to bind to all interfaces. If this behavior is desired, start the server with --bind_ip 127.0.0.1 to disable this warning
2021-11-21T12:11:35.747+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
2021-11-21T12:11:35.747+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
---
---
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()
---
> exit
bye
[root@VM-20-8-centos mongdb]#
(2)进入MongoDB命令行
- 安装MongoDB的bin目录下包含两个文件:mongo和mongod
- 进入MongoDB命令行
./mongo
3. MongoDB命令学习
(1)创建数据库
- use DATABASE_NAME,如果数据库不存在,则创建数据库,否则切换到指定数据库。
use DATABASE_NAME
(2)查看所有数据库
- 只能显示有数据的数据库,所以想要看到想看的数据库,需要往里面插入几条数据。
- 集合只有在内容插入后才会创建! 就是说,创建集合(数据表)后要再插入一个文档(记录),集合才会真正创建。
- MongoDB 中默认的数据库为 test,如果你没有创建新的数据库,集合将存放在 test 数据库中。
show dbs
(3)MongoDB 创建集合
(4)MongoDB 插入文档
- 参数说明:
- name: 要创建的集合名称
- options: 可选参数, 指定有关内存大小及索引的选项
db.createCollection(name, options)
- 上述操作记录:
> use cmdb;
switched to db cmdb
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
> db.createCollection(hardware);
uncaught exception: ReferenceError: hardware is not defined :
@(shell):1:1
> db.createCollection("hardware");
{ "ok" : 1 }
> db.hardware.in
db.hardware.initializeOrderedBulkOp( db.hardware.insert( db.hardware.insertOne(
db.hardware.initializeUnorderedBulkOp( db.hardware.insertMany(
> db.hardware.insert({)
... ^C
> db.hardware.insert({"name":"我是程咬金"});
uncaught exception: SyntaxError: illegal character :
@(shell):1:36
> show dbs;
admin 0.000GB
cmdb 0.000GB
config 0.000GB
local 0.000GB
> show collections;
hardware
>
(5)MongoDB 删除数据库
(6)MongoDB 删除Collection
|