php - Unable to authenticate mongoDB -
from mongo client looks can authenticate:
> use admin switched db admin > db.auth('admin','secretpassword'); 1 >
but when trying connect in config file in codeigniter code
$config['mongo_server'] = null; $config['mongo_dbname'] = 'admin'; $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'] = 'secretpassword'; $config['mongo_db']['default']['database'] = 'mydb'; $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; $config['mongo_db']['test']['hostname'] = 'localhost'; $config['mongo_db']['test']['port'] = '27017'; $config['mongo_db']['test']['username'] = ''; $config['mongo_db']['test']['password'] = ''; $config['mongo_db']['test']['database'] = ''; $config['mongo_db']['test']['db_debug'] = true; $config['mongo_db']['test']['return_as'] = 'array'; $config['mongo_db']['test']['write_concerns'] = (int)1; $config['mongo_db']['test']['journal'] = true; $config['mongo_db']['test']['read_preference'] = null; $config['mongo_db']['test']['read_preference_tags'] = null;
i getting error
unable connect mongodb: failed connect to: localhost:27017: authentication failed on database 'mydb' username 'admin': auth failed
i use simple library
create mongo.php in config.php
$config['mongo_server'] = 'localhost'; $config['mongo_dbname'] = 'yourdbname';
and mongo.php in libraries
<?php class ci_mongo extends mongo { var $db; function ci_mongo() { // fetch codeigniter instance $ci = get_instance(); // load mongo configuration file $ci->load->config('mongo'); // fetch mongo server , database configuration $server = $ci->config->item('mongo_server'); $dbname = $ci->config->item('mongo_dbname'); // initialise mongo if ($server) { parent::__construct($server); } else { parent::__construct(); } $this->db = $this->$dbname; } }
now can check, in controller:
function index(){ $posts = $this->mongo->yourdbname->yourcollection->find(); foreach($posts $id => $b){ echo $b['somefieldontable']; } }
Comments
Post a Comment