MongoDB数据库开发环境搭建与配置,Windows环境下

x33g5p2x  于2022-01-13 转载在 Go  
字(3.8k)|赞(0)|评价(0)|浏览(472)

MongoDB是一种NoSQL数据库。本文以Windows环境为例搭建配置MongoDB。

MongoDB下载链接:

MongoDB Community Download | MongoDB

https://www.mongodb.com/try/download/community

解压后,可以看到在\bin目录下有两个.exe文件,mongo.exe和mongod.exe。mongo.exe是连接MongoDB数据库的客户端,mongod.exe是服务器端程序。

(1)启动MongoDB服务器端。

新建一个data文件目录,假设是D:\mongodb\data。data文件目录作为后续数据库存数据的目录。

通过命令:

mongod --dbpath=d:/mongodb/data

启动MongoDB服务器。--dbpath=d:/mongodb/data指示MongoDB以D:\mongodb\data作为数据存储目录。

如果以这条命令启动:

mongod --dbpath=d:/mongodb/data --logpath=d:/mongodb/mongo.log

则控制台不输出调试信息,调试信息将全部输出到d:\mongodb\mongo.log文件里面。

如果启动成功,可以看到其中的控制台消息输出为:

,"s":"I",  "c":"NETWORK",  "id":23016,   "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}

表明MongoDB服务器端已经启动成功,在端口27017等待客户端连接。

(2)客户端连接MongoDB服务器端。

第一种方式,控制台命令行方式。

在控制台执行命令:

mongo

该命令将连接MongoDB服务器端,如果连接成功,控制台输出:

D:\mongodb>mongo
MongoDB shell version v5.0.5
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("9a333cc0-2a3b-45a8-8664-f86dc5f3801d") }
MongoDB server version: 5.0.5

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/

Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
        https://community.mongodb.com

第二种方式,GUI客户端方式。

为了方便MongoDB的管理,下载MongoDB的GUI管理程序:

MongoDB Compass Download | MongoDBMongoDB Compass, the GUI for MongoDB, is the easiest way to explore and manipulate your data. Download for free for dev environments.

https://www.mongodb.com/try/download/compass下载完成后,解压,找到MongoDBCompass.exe,运行,连接MongoDB服务器端。可在GUI可视化界面管理MongoDB。

(3)在Java程序代码中使用MongoDB作为数据存储后端。

用MongoDBCompass客户端连接MongoDB后,创建一个数据库,假设数据库名字叫zhangphil_db,创建zhangphil_db后,在zhangphil_db里面创建集合zhangphil_collection。

第一步,在pom.xml里面加入MongoDB驱动模块:

<dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver-sync</artifactId>
            <version>4.4.1</version>
        </dependency>

第二步,写Java程序:

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.Arrays;

public class MainClass {
    public static void main(String[] args) {
        final String DB = "zhangphil_db";
        final String COLLECTION = "zhangphil_collection";

        //create()函数不指定主机名和端口号,就是localhost和端口27017。
        MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");

        MongoDatabase database = mongoClient.getDatabase(DB);
        MongoCollection<Document> collection = database.getCollection(COLLECTION);

        Document doc = new Document("name", "zhangphil")
                .append("age", "2021")
                .append("city", "Chengdu")
                .append("it", Arrays.asList("spring", "mongo", "mysql", "python", "android", "java"));
        //往MongoDB远程数据库写入一“条”数据(结构化文档)。
        collection.insertOne(doc);

        System.out.println("数据集:" + collection.countDocuments());
        Document myDoc = collection.find().first();
        System.out.println("文档内容:" + myDoc.toJson());
    }
}

保证MongoDB服务器端运行在27017端口。运行成功后,当前idea控制台输出:

数据集:1
文档内容:{"_id": {"$oid": "61dea31fe74004760a936564"}, "name": "zhangphil", "age": "2021", "city": "Chengdu", "it": ["spring", "mongo", "mysql", "python", "android", "java"]}

在MongoDBCompass的客户端,刷新数据库,看到数据已经插入:

写入成功。

相关文章