mongodb installation and use

The advantages and disadvantages of mongodb will not be described in this article. Baidu searches a lot. I mainly summarize the installation method and usage method of mongodb here.

Install:

Official website: https://www.mongodb.com/ 

You can download the version of mongodb corresponding to the operating system.

After downloading, except for windows, after decompression, rename the directory, delete all text files except the bin directory, and put them in any location to use, but before use, it is more convenient to configure and run again. For example, setting the daemon process, setting the database to be stored in a separate directory, setting the database path, etc.

Configuration reference: http://docs.mongoing.com/manual-en/reference/configuration-options.html

Documentation reference: https://docs.mongodb.com/master/reference/method/db.collection.drop/#db.collection.drop

In windows my configuration file is as follows:

systemLog:

    destination: file

    path: D:\mongodb\log\mongod.log

    logAppend: true

storage:

    journal:

        enabled: true

    dbPath: D:\mongodb\db

    directoryPerDB: true

net:

    bindIp: 127.0.0.1

    port: 27017

 

Set up mongod as a service:

Setting up mongodb as a windows service writes
D:\mongodb\bin\mongod --config D:\mongodb\conf\mongo.conf --install --serviceName "MongoDB" --journal

 Start the service:

net start MongoDB

If you want to view the MongoDB in the windows service, right-click on my computer, manage--services, and you can find the MongoDB service. In theory, it has been automatically started. That is to say, the next time you start windows, mongodb starts automatically.

 

 mongodb does not have control permissions enabled by default, so when we connect with the D:\mongodb\bin\mongo client in the windows console, we can see a warning: WARNING: Access control is not enabled for the database.

To enable control permissions, follow these steps:

1. D:\mongodb\bin\mongo connects to the mongodb database.

2. Switch the database to admin: use admin

3. Create a super administrator, because we always use the mysql database, so let's create a root user (you can also create an admin user): 

The operation commands here are for version 3.0 and later versions, please Baidu before version 3.0.

mongodb create super administrator root user writes
db.createUser({user:'root',pwd:'root',roles:['dbAdminAnyDatabase','userAdminAnyDatabase','readWriteAnyDatabase']})

To update administrators or other users:

wrote
db.updateUser('root', {pwd:'root',roles:['dbAdminAnyDatabase','userAdminAnyDatabase','readWriteAnyDatabase']});

 If you want to delete a user:

wrote
db.dropUser('root');

 View users in the current database:

show users;

 

具体权限参考这篇文章吧:http://justcoding.iteye.com/blog/2270466

上面用户一定要添加三种角色,只添加dbAdminAnyDatabase的话,虽然能对所有数据库添加管理员,但却无法读取、修改数据库,这样在rockmongo管理的时候,如果我们想类似mysql一样用root用户登陆上去管理,会出错。当然,如果你太重视安全,那么就只给dbAdminAnyDatabase即可。

这样,root用户就创建好了。

 

4、我们修改配置启用验证:

systemLog:

    destination: file

    path: D:\mongodb\log\mongod.log

    logAppend: true

storage:

    journal:

        enabled: true

    dbPath: D:\mongodb\db

    directoryPerDB: true

net:

    bindIp: 127.0.0.1

    port: 27017

security:

    authorization: enabled

备注:linux中的配置只要增加守护进程的设置即可:

processManagement:

  fork: true

  pidFilePath: /data/web/mongodb/mongod.pid

systemLog:

  destination: file

  path: /data/web/mongodb/log/mongod.log

  logAppend: true

storage:

  dbPath: /data/web/mongodb/db

  directoryPerDB: true

  journal:

    enabled: true

net:

  bindIp: 127.0.0.1

  port: 27017

security:

 

  authorization: enabled

5、再重启mongodb服务:

net stop MongoDB

net start MongoDB

 

6、命令行连接:

D:\mongodb\bin\mongo

7、验证:

use admin

db.auth('root', 'root')

8、创建比如某个应用或web站点要使用的数据库:webdb

use webdb

9、为webdb创建用户:

db.createUser({user:'webdb',pwd:'webdb',roles:[{role:'dbOwner',db:'webdb'}]})

至此,用户控制搞定。以后要给别的数据库添加用户也是用这种方法。

 

接下来我们用rockmongo来管理mongodb,这个跟phpMyAdmin很相似,但界面要差很多,凑合用吧,下载地址:

https://github.com/iwind/rockmongo

下载后,解压缩,然后重命名文件夹,拷贝到相应的目录,设置虚拟主机以后,可以访问网站(php技术)。

访问后,如果提示:To make things right, you must install php_mongo module. Here for installation documents on PHP.net.

这代表没有安装php_mongo扩展,参考文档去下载扩展安装:

http://php.net/manual/en/mongo.installation.php#mongo.installation.manual

注意的是:除了dll文件放入相应的目录以外,php.ini添加extension=php_mongo.dll,还有就是如果启动服务器报错,可以将php目录添加到系统环境变量Path中。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326679936&siteId=291194637