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 127.0.0.1 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 127.0.0.1 to 0.0.0.0; 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' = > '192.168.3.216', / / 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 = [            
'name'=>'test',            
'value'=>'mongo db'        
];       
 $res = Db::connect('mongo_db')->table('demo')->insert(($data));        
$this->success("res",$res);    
}
    public function testSelect()    {        
$a = Db::connect('mongo_db')->table('demo')        
->where('id','5ff6c6cf6f6f0000f1000d18')        
->select();        
$this->success("res",$res);    
}
Copy the code