search
HomeDatabaseMysql TutorialHow to quickly build your own database in mongodb

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

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

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();

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

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"
        }
    ]
}

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()

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,简单的加密就完成了。

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');

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
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
深入理解MySQL索引优化器工作原理深入理解MySQL索引优化器工作原理Nov 09, 2022 pm 02:05 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于索引优化器工作原理的相关内容,其中包括了MySQL Server的组成,MySQL优化器选择索引额原理以及SQL成本分析,最后通过 select 查询总结整个查询过程,下面一起来看一下,希望对大家有帮助。

sybase是什么数据库sybase是什么数据库Sep 22, 2021 am 11:39 AM

sybase是基于客户/服务器体系结构的数据库,是一个开放的、高性能的、可编程的数据库,可使用事件驱动的触发器、多线索化等来提高性能。

visual foxpro数据库文件是什么visual foxpro数据库文件是什么Jul 23, 2021 pm 04:53 PM

visual foxpro数据库文件是管理数据库对象的系统文件。在VFP中,用户数据是存放在“.DBF”表文件中;VFP的数据库文件(“.DBC”)中不存放用户数据,它只起将属于某一数据库的 数据库表与视图、连接、存储过程等关联起来的作用。

数据库系统的构成包括哪些数据库系统的构成包括哪些Jul 15, 2022 am 11:58 AM

数据库系统由4个部分构成:1、数据库,是指长期存储在计算机内的,有组织,可共享的数据的集合;2、硬件,是指构成计算机系统的各种物理设备,包括存储所需的外部设备;3、软件,包括操作系统、数据库管理系统及应用程序;4、人员,包括系统分析员和数据库设计人员、应用程序员(负责编写使用数据库的应用程序)、最终用户(利用接口或查询语言访问数据库)、数据库管理员(负责数据库的总体信息控制)。

microsoft sql server是什么软件microsoft sql server是什么软件Feb 28, 2023 pm 03:00 PM

microsoft sql server是Microsoft公司推出的关系型数据库管理系统,是一个全面的数据库平台,使用集成的商业智能(BI)工具提供了企业级的数据管理,具有使用方便可伸缩性好与相关软件集成程度高等优点。SQL Server数据库引擎为关系型数据和结构化数据提供了更安全可靠的存储功能,使用户可以构建和管理用于业务的高可用和高性能的数据应用程序。

access数据库的结构层次是什么access数据库的结构层次是什么Aug 26, 2022 pm 04:45 PM

结构层次是“数据库→数据表→记录→字段”;字段构成记录,记录构成数据表,数据表构成了数据库。数据库是一个完整的数据的记录的整体,一个数据库包含0到N个表,一个表包含0到N个字段,记录是表中的行。

go语言可以写数据库么go语言可以写数据库么Jan 06, 2023 am 10:35 AM

go语言可以写数据库。Go语言和其他语言不同的地方是,Go官方没有提供数据库驱动,而是编写了开发数据库驱动的标准接口,开发者可以根据定义的接口来开发相应的数据库驱动;这样做的好处在于,只要是按照标准接口开发的代码,以后迁移数据库时,不需要做任何修改,极大方便了后期的架构调整。

mysql查询慢的因素除了索引,还有什么?mysql查询慢的因素除了索引,还有什么?Jul 19, 2022 pm 08:22 PM

mysql查询为什么会慢,关于这个问题,在实际开发经常会遇到,而面试中,也是个高频题。遇到这种问题,我们一般也会想到是因为索引。那除开索引之外,还有哪些因素会导致数据库查询变慢呢?

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools