2/03/2017

Codeigniter: Unable to connect to your database server using the provided settings Solution Summary

Environment: Win 10, XAMPP
After I git clone a Codeigniter project, set up the environment, import the .sql file.
When I go to the localhost/index.php it shows:

Unable to connect to your database server using the provided settings

I have ..\application\config\database.php like this:
$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => 'root',
    'database' => 'comptracker_db',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,//(ENVIRONMENT != 'production'),
    'cache_on' => FALSE,
I googled this error and got some solutions:
1. in your database.php add:
$db['default']['port'] = '3306';
Some developers say it works, but not for me.

2. Go to your my.cnf and comment the line
bind 127.0.0.1
It doesn't work either. This line has already been comment in my.cnf

3. I find some code below and added to my database.php
echo '<pre>';
 print_r($db['default']);
echo '</pre>';

echo 'Trying to connect to database: ' .$db['default']['database'];
$dbh=mysql_connect
(
$db['default']['hostname'],
$db['default']['username'],
$db['default']['password'])
or die('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ($db['default']['database']);

echo '<br />   Connected OK:'  ;
die( 'file: ' .__FILE__ . '--> Line: ' .__LINE__);
Fresh localhost, I got:
Access denied for user 'root'@'localhost' (using password: YES)

Go to your C:\xampp\phpmyadmin\config.inc.php
Check this:
/* Authentication type and info */
$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['AllowNoPassword'] = true;
$cfg['Lang'] = '';
Make sure your 'password' here is the same as database.php
$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'comptracker_db',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,//(ENVIRONMENT != 'production'),
    'cache_on' => FALSE,

And this problem gets solved.


1 comment: