Home Database Mysql Tutorial How to quickly build your own database in mongodb

How to quickly build your own database in mongodb

Sep 21, 2018 pm 05:03 PM
database

This chapter will introduce you to how to quickly build your own database with mongodb. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

mongoddb installation

  • Install locally

Directly through the official website Download the compressed package corresponding to the machinemongodb

  • Install on the cloud server (centos system)

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.4.9.tgztar zxvf mongodb-linux-x86_64-3.2.6.tgz
mv mongodb-linux-x86_64-3.2.6.tgz mongodb
cd mongodb
Copy after login

How to quickly build your own database in mongodb

Please download the corresponding version according to your system~

Environment configuration & start server

Create in the file directory The general directory of the folder where data is stored is /usr/local/mongodb/data/db/. Run the command to start mongodb

./bin/mongod --dbpath=/usr/local/mongodb/data/db/ --rest
Copy after login

Here are several parameters to highlight. Mongod can provide you with mongodb command line support. Start. If necessary, you can edit /etc/profile to enter the global environment. dbpath is the data path, which corresponds to the data directory you created. --rest is a graphical support.
The default path of mongodb is //localhost:27017. After successful operation, accessing this address will prompt a success message and adding the rest parameter can access //localhost:28017

. /bin/mongo can open the shell
Common commands:

#查询所有数据库    show dbs;
#删除当前使用数据库   db.dropDatabase(); 
#克隆主机数据   db.cloneDatabase(“127.0.0.1”); 
#修复当前数据库 db.repairDatabase(); 
#查看当前使用的数据库  db.getName(); 
#显示当前db状态  db.stats(); 
#查看当前db的链接机器地址  db.version();
Copy after login

Deploy the mongodb environment on the cloud server

Now the cloud is very popular, many People have chosen to use cloud servers to deploy their projects. Here is an introduction to the mongodb configuration in the cloud.

./bin/mongod --fork --dbpath=/usr/local/mongodb/data/db/ --logpath=/usr/local/mongodb/data/log/error.log -logappend --rest
Copy after login

When the database is deployed to the cloud server, it needs to run in the background. When I first used the centos forever plug-in, I found that the database could not be run in the background. After checking the information, I found that there is an official command - fork to start the background service --logpath --logappend parameter to add a log log to the background service. The rest effect is the same as above.

Make the database more intuitive

Isn’t the dense data ugly? feeling bad? At this time we need a plug-in to beautify the database

There are many on the Internet such as mongovue and adminmongo. You can search or check them on Github.

The following is database encryption. I learned from the experience and updated the encryption process after being attacked online.

Add super administrator

First, run your database and execute the shell operation after success.

> use admin
> db.createUser(
   {
     user: "your name",
     pwd: "your pwd",
     roles: [ { role: "root", db: "admin" } ]
   }
)
Successfully added user: {
    "user" : "admin",
    "roles" : [
        {
            "role" : "root",
            "db" : "admin"
        }
    ]
}
Copy after login

root means super permissions, so an account with super permissions is created. You can view users with the following command.

[Uploading image_088895.png . . .]

db.getUsers()
Copy after login

MongoDB database role

##role refers to the role, manages and controls the database Permissions, the first user is preferably the root user, who can perform any operation.

It is best to create a user with root permissions for initialization. When the auth mode is turned on, any operation requires permissions to perform. You must not start the background running mode directly with auth, otherwise you will not have the authority to close the database.

  • Read: Allow users to read the specified database

  • readWrite: Allow users to read and write the specified database

  • dbAdmin: Allow users to perform management functions in the specified database, such as index creation, deletion, view statistics or access system.profile

  • userAdmin: Allow users to write to the system.users collection , you can create, delete and manage users in the specified database

  • clusterAdmin: only available in the admin database, giving the user administrative rights to all sharding and replication set-related functions.

  • readAnyDatabase: Only available in the admin database, giving the user read permissions to all databases

  • readWriteAnyDatabase: Only available in the admin database, giving the user read permissions to all databases Read and write permissions for all databases of the user

  • userAdminAnyDatabase: only available in the admin database, grant userAdmin permissions for all databases of the user

  • dbAdminAnyDatabase: only Available in the admin database, giving the user dbAdmin permissions on all databases.

  • root: Only available in the admin database. Super account, super permissions

Start auth mode

Encryption can only be started after the super administrator is created. database, otherwise even you yourself will not have the right to operate the database.

 //后台模式需要在shell中关闭之前的普通模式数据库,输入以下命令
 > use admin
 > db.shutdownServer()
//终端 启动加密数据库 --auth
$ ./bin/mongod --fork --dbpath=/root/mongodb/db/ --logpath=/root/mongodb/log/error.log -logappend --auth
//通过命令运行数据库 ,在你的运行命令加上后缀 --auth,这样就启动了加密数据库 ,再次执行数据库操作
> show dbs
2017-09-23T14:09:58.922+0800 E QUERY    [thread1] Error: listDatabases failed:{
    "ok" : 0,
    "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
    "code" : 13,
    "codeName" : "Unauthorized"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
shellHelper.show@src/mongo/shell/utils.js:769:19
shellHelper@src/mongo/shell/utils.js:659:15
@(shellhelp2):1:1
//  发现报错,需要认证信息
> db.auth('your name','your pwd')
成功返回1  失败返回0 输入之前创建的超级账号,OK,简单的加密就完成了。
Copy after login

How to quickly build your own database in mongodb

Encryption successful, authentication is required to operate the database

链接加密数据库

数据库加密后我们的服务端代码也要相应变动。

xxx.db('mongodb://your name: your pwd@localhost:27017/db?authSource=admin');
Copy after login

xxx表示你用的插件 比如 mongoose 、mongoskin之类的。

到此为止,你的数据库就加密完成了,当你的项目变大,你也许还需要创建许多用户,或者升级用户权限,这些官方都有相关的API去操作。

The above is the detailed content of How to quickly build your own database in mongodb. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Tips and practices for handling Chinese garbled characters in databases with PHP Tips and practices for handling Chinese garbled characters in databases with PHP Mar 27, 2024 pm 05:21 PM

PHP is a back-end programming language widely used in website development. It has powerful database operation functions and is often used to interact with databases such as MySQL. However, due to the complexity of Chinese character encoding, problems often arise when dealing with Chinese garbled characters in the database. This article will introduce the skills and practices of PHP in handling Chinese garbled characters in databases, including common causes of garbled characters, solutions and specific code examples. Common reasons for garbled characters are incorrect database character set settings: the correct character set needs to be selected when creating the database, such as utf8 or u

Analysis of the basic principles of MySQL database management system Analysis of the basic principles of MySQL database management system Mar 25, 2024 pm 12:42 PM

Analysis of the basic principles of the MySQL database management system MySQL is a commonly used relational database management system that uses structured query language (SQL) for data storage and management. This article will introduce the basic principles of the MySQL database management system, including database creation, data table design, data addition, deletion, modification, and other operations, and provide specific code examples. 1. Database Creation In MySQL, you first need to create a database instance to store data. The following code can create a file named "my

See all articles