The server built an environment with a pagoda

The pagoda cannot be connected after MongoDB is installed.

Find the problem:

View the configuration file. The default is BindIP port 27017

First release port 27017 from Pagoda security

Then add port 27017 to the security group in ali Cloud security group (my Ali Cloud security group is restricted to some ports accessed by the external network)

Finally, change the default BindIP to; And restart the mongo

Test connection with Navicat, successful!

After the connection is successful, you can click [Database] to add the database, as shown in the following sequence:

Once added, you can manage collections, views, functions, and so on.

I’m using the Thinkphp5.1 framework here to try to write data through code

First append the mongoDB configuration to the end of the array in database.php

Mongo_db =>[// database type' type' => '\think\mongo\Connection', // set query class 'query' => '\think\mongo\ query', / / the server address 'hostname' = > '', / / collection name 'database' = > 'for_think_mall', / / user name 'username' = > ', / / password is' password '= >', / / port 'hostport' = > '27017', 'pk_convert_id' = > true,]Copy the code

And then I’m going to add a method to the Controller to do that

    /**     * mongo测试 使用方法和mysql基本一致,只是需要加入table对应的集合名     */
    public function test()    {        
$data = [            
'value'=>'mongo db'        
 $res = Db::connect('mongo_db')->table('demo')->insert(($data));        
    public function testSelect()    {        
$a = Db::connect('mongo_db')->table('demo')        
Copy the code