Home  >  Article  >  Database  >  How to quickly build your own database in mongodb

How to quickly build your own database in mongodb

青灯夜游
青灯夜游Original
2018-09-21 17:03:472312browse

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