Construction of mongodb Windows Environment

Time:2022-3-31

Install mongodb

  • Download:https://www.mongodb.com/download-center#enterprise

  • install

  • Create folder: enter the installation pathC:\Program Files\MongoDB\Server\3.4\bin, createdataFolder, enterdataFolders, creatingdbFolders andlogFolder, inlogCreate under folderMongoDB.logfile

  • Start mongodb as windows service: start as Administratorcmd, CD toBin directory cd C:\Program Files\MongoDB\Server\3.4\bin, execute the commandmongod --dbpath "C:\Program Files\MongoDB\Server\3.4\bin\data\db" --logpath "C:\Program Files\MongoDB\Server\3.4\bin\data\log\MongoDB.log" --install --serviceName "MongoDB". this command creates a file namedMongoDBWindows system services

  • start-upMongoDBServices:net start mongodb

Create database (with database authentication)

mongoDBAuthentication is not enabled by default, that is, you can access the database without using user name and password. First, let’s introduce how to turn on database authentication.

Database authentication related

  • Turn on database authentication: navigate to the registryHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MongoDB, edit stringImagePathAppend after ‘numeric data’--auth, restartMongoDBService is OK

  • Turn off database authentication: navigate to the registryHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MongoDB, edit stringImagePathAfter deleting ‘numeric data’--auth, restartMongoDBService is OK

Create super administrator user

  • Turn off database authentication

  • Start in administrator modecmd, CD toBin directory cd C:\Program Files\MongoDB\Server\3.4\bin, execute the commandmongo

  • Switch toadmindatabaseuse admin, create super administratordb.createUser({"user" : "root","pwd": "root",roles: [{ role: "root", db: "admin"}]})

  • Turn on database authentication

  • Switch toadmindatabaseuse admin, use the created in the previous steprootUser logindb.auth("root", "root")If ‘1’ is returned, the login is successful

  • Create a database, for example, with the namedemoDatabase ofuse demo

  • establishdemoDatabase userdb.createUser({"user" : "demoUser","pwd": "demoUser",roles: [{ role: "dbOwner", db: "demo"}]})

  • Verify whether the database and user are created successfullymongo --host localhost -u demoUser-p demoUser --authenticationDatabase demo, the console appearsconnecting to: mongodb://localhost:27017/, it indicates that the creation is successful