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:
-
Configure environment variables
- Find
我的电脑
-> right click, click属性
->高级系统设置->环境变量->path
double 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 -v
commands in the dos environment at any locationBut if you enter directly in the dos environment:
mongod
a direct exit will occur:The reason is: by default, mongodb uses the root directory of the drive letter
mongod
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/db
C
mongod
c:/data/
db
Method One : directly in the root directory of the current drive letter
data
created db Table of ContentsMethod 2 :
mongod --dbpath
Specify the database storage directory by way-
Create database directory
In the place where you want to store the data, create a new folder, such as:, the
db
recommended database directory is
data/ db --> 数据库目录
Among them: the directory where data is the installation directory of mongodb, my installation directory is:,
D:\mongodb
then 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 is
D:\mongodb\bin
-
Then enter the following command:,
mongod --dbpath d:\mongodb\data\db或者写成mongod --dbpath=d:\mongodb\data\db
where,--dbpath
used to specify the database,d:\mongodb\data\db
is the database directory created before
- Find the last line of the printout after entering the above command. If it appears
port:27017
, it indicates that the service started successfully, and27017
it is the default port number of mongodb
- Find
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 mongo
command, otherwise you need to in mongodb d:\mongodb\bin
Open the dos environment in the bin directory of the installation directory to successfully execute the mongo
command), if the following conditions occur, the connection is successful
4. Exit the database
exit
Just enter , just exit the database at this time, the mongodb service is still open normally