Start the mongodb service

1. Start the mongodb service

Since mongodb does not register the win service by default, you need to manually open the service or configure the win service

Manually open the service:

  1. Configure environment variables

    • Find 我的电脑-> right click, click 属性-> 高级系统设置->环境变量->pathdouble click, 新建and fill in the bin directory under the mongodb installation directory. For example, the bin directory under my installation directory is: d:\mongodb\bin\fill in
    • Open the dos environment (open it anywhere)
    • Enter mongod -v, if the relevant version information appears, it indicates that the environment variable configuration is successful

    At this point, you can execute mongod -vcommands in the dos environment at any location

    But if you enter directly in the dos environment: mongoda direct exit will occur:

    load_05

    The reason is: by default, mongodb uses the root directory of the drive lettermongod where the command is executed as its data storage directory. What does it mean? In the previous picture, I executed the command under the disk , so it will find out whether there is a directory in the directory. If there is a directory, it will start the service, and if it is not, it will exit directly, which is the situation shown in the above figure. There are two solutions at this time:data/dbCmongodc:/data/db

    Method One : directly in the root directory of the current drive letter datacreated db Table of Contents

    Method 2 : mongod --dbpathSpecify the database storage directory by way

    1. Create database directory

      In the place where you want to store the data, create a new folder, such as:, the dbrecommended database directory is

    data/
    		db --> 数据库目录
    

    Among them: the directory where data is the installation directory of mongodb, my installation directory is:, D:\mongodbthen the directory after the db folder is created is:D:\mongodb\db\data

    ​ 2. Start the database

    ​ The current local is win10 environment

    • Open the dos environment, select the bin directory of the mongodb installation path, mine isD:\mongodb\bin

      load_01

    • Then enter the following command:, mongod --dbpath d:\mongodb\data\db或者写成mongod --dbpath=d:\mongodb\data\dbwhere, --dbpathused to specify the database, d:\mongodb\data\dbis the database directory created before

    load_02

    • Find the last line of the printout after entering the above command. If it appears port:27017, it indicates that the service started successfully, and 27017it is the default port number of mongodb

load_03

You can also visit 127.0.0.1:27017, if you see it on the webpage: It looks like you are trying to access MongoDB over HTTP on the native driver port.it also means that the service started successfully

Configure win service : refer to mongodb rookie tutorial

2. Withdraw from the service
ctrl+c
# 或者
直接关闭dos窗口
3. Connect to the database

Without closing the current dos environment, reopen a dos environment (ensure that the service is open to connect to the database), enter mongo(only if the environment variables have been configured before, you can directly open the dos environment in any directory and enter the mongocommand, otherwise you need to in mongodb d:\mongodb\binOpen the dos environment in the bin directory of the installation directory to successfully execute the mongocommand), if the following conditions occur, the connection is successful

load_04

4. Exit the database

exitJust enter , just exit the database at this time, the mongodb service is still open normally

Guess you like

Origin blog.csdn.net/chen__cheng/article/details/114914940