Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the installation, startup and service of MongoDB and what is the connection?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

It is believed that many inexperienced people have no idea about the installation, startup, service and connection of MongoDB. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

MongoDB install, start, service, connect install

Download: https://www.mongodb.com/download-center/community/releases, pay attention to download the corresponding version

Extract: extract the downloaded files to a folder

Create a data folder mongo-data, create a log folder: mongo-log

The directory in this article looks like this

/ opt/mongo/ bin/ # mongo program data/ # data log/ # log mongod.conf # log

All command relative paths are relative / opt/mongo

Create user groups and users

Groupadd mongoduseradd-g mongod mongod

The first command is to add a user group, and the second is to add a user mongod to the mongod user group

Access rights of group mongo users to mongo related files

Chown-R mongo:mongo

Start

. / bin/mongod-logpath=log/mongo.log-dbpath=data-port=9999-fork

The above command is started with the logout user. The mongo.log file must be created first, otherwise an error will be reported.

-- fork runs on behalf of the background

Test whether the startup is successful

. / bin/mongo-- port=9999

Connect using the mongo client. The above command is to connect to MONGOD on port 9999 of localhosts.

After the connection is successful, you will see a lot of warn

Access control is not enabled for the database. Read and write access to data and configuration is unrestricted

There are no access restrictions, which will be resolved later.

You are running this process as the root user, which is not recommended

It is not recommended to use root to start services, which will be solved later.

This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with-bind_ip 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

It is said that the service is started using localhost, which cannot be accessed by clients other than this machine, and tells you what to do, which will be solved later in the configuration file.

Soft rlimits too low:

The official document for this question: https://docs.mongodb.com/manual/reference/ulimit/

The general meaning of this problem is that the linux system has a limit on the number of resources that users can use. A limitation of the current system will affect the operation of mongo. Use ulimit-a to view various restrictions.

Officials point out that the following resources will affect the operation of mongo, and give recommended values.

-f (file size): unlimited-t (cpu time): unlimited-v (virtual memory): unlimited [1]-l (locked-in-memory size): unlimited-n (open files): 64000-m (memory size): unlimited [1] [2]-u (processes/threads): 64000

Refer to the recommended value above and compare it with the result of-ulimit-a (compare it with the previous-f _ ulimit _ a). Change which one is wrong. For example, if the-n value of the system is 1024, which is less than the recommended 64000, then execute the command-ulimit-n 64000, and then start the kill service again.

If your system uses systemd, you can also add the following configuration to the .service file

[Service] # Other directives omitted# (file size) LimitFSIZE=infinity# (cpu time) LimitCPU=infinity# (virtual memory size) LimitAS=infinity# (locked-in-memory size) LimitMEMLOCK=infinity# (open files) LimitNOFILE=64000# (processes/threads) LimitNPROC=64000

Configuration file: a simple example

SystemLog: destination: file path: "/ opt/mongo/mongodb/log/mongo.log" net: port: 9999 bindIp: 192.168.145.220127.0.0.1storage: dbPath: "/ opt/mongo/mongodb/data" processManagement: fork: true

On startup: bin/mongod-f mongod.conf

Systemctl starts mongodb

[Unit] Description=mongodbAfter=network.target remote-fs.target nss- lookup.target [service] Type=forkingExecStart=/opt/mongo/mongodb/bin/mongod-- config / opt/mongo/mongodb/mongod.confExecStop=/opt/mongo/mongodb/bin/mongod-- shutdown-- config / opt/mongo/mongodb/mongod.confPrivateTmp=trueLimitFSIZE=infinityLimitCPU=infinityLimitAS=infinityLimitMEMLOCK=infinityLimitNOFILE=64000LimitNPROC=64000Group=mongodUser=mongodExecReload=/bin/kill-s HUP $MAINPID [install] WantedBy=multi-user.target

The corresponding configuration file should have the following configuration

ProcessManagement: fork: true pidFilePath: "/ opt/mongo/mongodb/log/9999.pid"

If you do not set the pid file, only set fork to true, can not start, if there are no two, then start (service mongo start) will be stuck, but then ctrl + c you will find that the service has been started. no, no, no. It can only be normal if both options are available.

Also note the Group and User options in the .service file. The initial mongo will eliminate the previous warning.

Create a user

If you don't create a user, your database can only run without security. After you create a user, you can log in to mongo as long as you have the user login information.

Db.createUser ({user: "superuser", pwd: "123456", roles: ["root"]})

At this point, your mongo can enable authentication and login. The permissions of mongo are role-based. We have now created a superuser user with root role.

Add the following configuration to the configuration file to enable authentication login

Security: authorization: enabled

Restart the service

There are two ways to log in

Bin/mongo-- port 9999-- username superuser-- password 123456-- authenticationDatabase admin##-- authenticationDatabase represents the database you created the user bin/mongo mongodb://localhost:9999/admin?authSource=admin-- username superuser## and then asks you to enter the superman password # # / admin represents the database you want to log in # # authSource has the same effect as-- authenticationDatabases

Now we have a superman user with root privileges

Log in without auth first.

After use admin is created, switch to the admin database

Create a user

Complete statement to create a user

Use reportingdb.createUser ({user: "reportsUser", pwd: "2222", roles: [{role: "read", db: "reporting"}, {role: "read", db: "products"}, {role: "read", db: "sales"}, {role: "readWrite", db: "accounts"}]})

Explain to the above statement:

All this user needs to do this when needed.

Bin/mongo-port 9999-username reportsUser-password 2222-authenticationDatabase reporting

Or

Bin/mongo mongodb://localhost:9999/admin?reporting=admin-username superuser

Using the reporting database

Create a user with a user name of reportsUser and a password of 2222, with read access to the three reporting,products,sales databases and read and write access to the accounts database

Delete / add user roles

Use reportingdb.revokeRolesFromUser ("reportsUser", [{role: "readWrite", db: "accounts"}])

Use reportingdb.grantRolesToUser ("reportsUser", [{role: "read", db: "accounts"}])

Change user password

Log in first with a user who has the right to update the user's password

Below

Db.changeUserPassword ("reporting", "SOh4TbYhxuLiW8ypJPxmt1oOfL")

After reading the above, have you mastered the installation, startup, service and connection of MongoDB? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report