我正在创建codeigniter和mongodb数据库的应用程序。由于codeigniter不包含mongodb驱动程序,我使用内置库与mongodb连接。我使用此链接创建mongodb配置文件和Mongo_db库。配置文件类似于applicationconfigmongodb:
<?php
$config['default']['mongo_hostbase'] = 'localhost:27017';
$config['default']['mongo_database'] = 'test';
$config['default']['mongo_username'] = '';
$config['default']['mongo_password'] = '';
$config['default']['mongo_persist'] = FALSE;
$config['default']['mongo_persist_key'] = 'ci_persist';
$config['default']['mongo_replica_set'] = FALSE;
$config['default']['mongo_query_safety'] = 'safe';
$config['default']['mongo_suppress_connect_error'] = FALSE;
$config['default']['mongo_host_db_flag'] = FALSE;
?>
和Mongo_db库:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mongo_db
{
public function __construct()
{
//Check mongodb is installed in your server otherwise display an error
if ( ! class_exists('Mongo'))
{
$this->_show_error('The MongoDB PECL extension has not been installed or enabled', 500);
}
//get instance of CI class
if (function_exists('get_instance'))
{
$this->_ci = get_instance();
}
else
{
$this->_ci = NULL;
}
//load the config file which we have created in 'config' directory
$this->_ci->load->config('mongodb');
$config='default';
// Fetch Mongo server and database configuration from config file which we have created in 'config' directory
$config_data = $this->_ci->config->item($config);
try{
//connect to the mongodb server
$this->mb = new Mongo('mongodb://'.$config_data['mongo_hostbase']);
//select the mongodb database
$this->db=$this->mb->selectDB($config_data['mongo_database']);
}
catch (MongoConnectionException $exception)
{
//if mongodb is not connect, then display the error
show_error('Unable to connect to Database', 500);
}
}
?>
当我运行应用程序时,它给了我一个错误,像遇到错误您的应用程序/config/mongodb.php文件似乎不包含有效的配置数组。
谁能告诉如何解决这个错误和设置与mongodb的连接?
你可能想试试这个repo: https://github.com/vesparny/cimongo-codeigniter-mongodb-library,看起来很容易使用。
完整的PHP库列表可以在这里找到:http://docs.mongodb.org/ecosystem/drivers/php-libraries/
尝试替换
$this->mb = new Mongo('mongodb://'.$config_data['mongo_hostbase']);
在库/mongo_db.php
中使用这两行$url = 'mongodb://'.$config_data['mongo_username'].':'.$config_data['mongo_password'].'@'.$config_data['mongo_hostbase'].'/'.$config_data['mongo_database'];
$this->mb = new MongoClient($url);
为我做了这项工作:)
如果你想在codeIgniter项目中使用mongoDB作为数据库,那么这篇文章可能对你有帮助。有一些简单的步骤可以在项目中配置mongoDB。
- 安装PECL包笼
对于这个你可以从PHP官方网站- http://php.net/manual/en/install.pecl.php
得到帮助- 下载CodeIgniter库
- 安装mongoDB。
- 设置库
- 加载库。
你必须下载一个库,它有助于创建与mongoDB的连接在这里输入链接说明
你必须在你的系统中安装mongoDB数据库。根据您的操作系统从https://docs.mongodb.com/manual/installation下载可安装文件并安装。
提取库,你会得到一个配置文件,你需要放在应用程序/配置文件夹和一个库文件,应该放在应用程序/库文件夹。在mongoDB配置文件中输入您的凭据,如
$config['mongo_db']['default']['no_auth'] = FALSE;
$config['mongo_db']['default']['hostname'] = 'localhost';
$config['mongo_db']['default']['port'] = '27017';
$config['mongo_db']['default']['username'] = 'aniket';
$config['mongo_db']['default']['password'] = 'password';
$config['mongo_db']['default']['database'] = 'admin';
$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;
作为一个正常的库,无论何时你想在你的项目中使用mongoDB,你都需要加载mongoDB库。为此,您可以使用以下代码:
$ this ->加载>图书馆(mongo_db,数组("激活"=>"newdb"),"mongo_db_conn");
参考这个youtube视频,简单而几分钟的解决方案配置mongodb与codeigniter。
https://www.youtube.com/watch?v=SMDznjUz57c