Codeigniter不工作MongoDB连接

时间:2016-10-16 12:48:37

标签: php mongodb codeigniter database

我正在使用Codeigniter MVC。我安装了MongoDB,但它不起作用。

我收到此错误;

Unable to connect to MongoDB: Failed to connect to: localhost:27017: SASL Authentication failed on database 'teknikserviscim': Authentication failed.

配置文件是这样的;

$config['mongo_db']['active'] = 'newdb';    
$config['mongo_db']['newdb']['hostname'] = 'localhost';    
$config['mongo_db']['newdb']['port']     = '27017';    
$config['mongo_db']['newdb']['username'] = 'superuser';    
$config['mongo_db']['newdb']['password'] = '12345678';    
$config['mongo_db']['newdb']['database'] = 'teknikserviscim';    
$config['mongo_db']['newdb']['db_debug'] = TRUE;    
$config['mongo_db']['newdb']['write_concerns'] = (int)1;    
$config['mongo_db']['newdb']['journal'] = TRUE;    
$config['mongo_db']['newdb']['read_preference'] = NULL;
$config['mongo_db']['newdb']['read_preference_tags'] = NULL;


class PanelHesap extends CI_Model {

public function __construct()
{
    parent::__construct();
    $this->load->library('mongo_db', array('activate'=>'newdb'),'mongo_db2');       
}

好。问题是什么 ? 感谢名单。

1 个答案:

答案 0 :(得分:1)

您的数据库连接应该是

$config['mongo_db']['active'] = 'default';
$config['mongo_db']['default']['no_auth'] = FALSE;
$config['mongo_db']['default']['hostname'] = 'localhost';
$config['mongo_db']['default']['port'] = '27017';
$config['mongo_db']['default']['username'] = 'admin';
$config['mongo_db']['default']['password'] = '123456';
$config['mongo_db']['default']['database'] = 'mydatabase';
$config['mongo_db']['default']['db_debug'] = TRUE;
$config['mongo_db']['default']['return_as'] = 'array';
$config['mongo_db']['default']['write_concerns'] = (int)1;
$config['mongo_db']['default']['journal'] = TRUE;
$config['mongo_db']['default']['read_preference'] = NULL;
$config['mongo_db']['default']['read_preference_tags'] = NULL;
$config['mongo_db']['default']['no_auth'] = FALSE;

并在application/libraries中添加此库

GIT Hub

并在autoload.php

$autoload['libraries'] = array('Mongo_db');

插入,更新,删除示例

Check this