Home » Php » php – CodeIgniter: Unable to connect to your database server using the provided settings Error Message

php – CodeIgniter: Unable to connect to your database server using the provided settings Error Message

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have been using CI just fine using the MySQL driver. I want to use the MySQL driver instead, but as soon as I change it (just add the ‘i’ at the end of MySQL, and added the port number) I get the following error message

A Database Error Occurred

Unable to connect to your database server using the provided settings.

Filename: core/Loader.php

Line Number: 232

my setting look like this:

$db['default']['hostname'] = $hostname;
$db['default']['username'] = $username;
$db['default']['password'] = $password;
$db['default']['database'] = $database;
$db['default']['dbdriver'] = 'mysqli';
$db['default']['port']     = "3306";  
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE; 

where
$hostname = 'localhost';
$username = 'myusernamegoeshere';
$password = 'mypasswordgoeshere';
$database = 'mydatabasenamegoeshere'; 

I’m Using:

CI 2.0.2 php 5.3.4 Apache/2.2.17 (Unix) mysql 5.5.13 mysql.default_port 3306

Am I doing anything wrong?

Thank you,

How to&Answers:

For me the issue was in the php.ini file. The property mysql.default_socket was pointing to file in a non-existent directory. The property was pointing to /var/mysql/mysql.sock but in OSX, the file was located in /tmp/mysql.sock.

Once I updated the entry in php.ini and restarted the webserver, the issue was resolved.

Answer:

I think, there is something wrong with PHP configration.
First, debug your database connection using this script at the end of ./config/database.php :

...
  ...
  ...
  echo '<pre>';
  print_r($db['default']);
  echo '</pre>';

  echo 'Connecting 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__); 

Then see what the problem is.

Answer:

Today I fallen this kind of problem in live server and i solved the problem changing this line

$db['default']['db_debug'] = TRUE;

to

$db['default']['db_debug'] = FALSE;

Answer:

I solved the problem by changing

$db['default']['pconnect'] = TRUE; TO
$db['default']['pconnect'] = FALSE;

in /application/config/database.php

Answer:

SET $db['default']['db_debug'] to FALSE instead of TRUE .

$db['default']['db_debug'] = FALSE;

Answer:

If that is all you have changed, you may not have the mysqli driver installed or enabled within your PHP configuration.

Check for its presence using phpinfo(), or in your php.ini file (extension=php_mysqli….).

Answer:

Extending @Valeh Hajiyev great and clear answer for mysqli driver tests:

Debug your database connection using this script at the end of ./config/database.php:

/* Your db config here */ 
$db['default'] = array(
  // ...
  'dbdriver'     => 'mysqli',
  // ...
);

/* Connection test: */

echo '<pre>';
print_r($db['default']);
echo '</pre>';

echo 'Connecting to database: ' .$db['default']['database'];

$mysqli_connection = new MySQLi($db['default']['hostname'],
                                $db['default']['username'],
                                $db['default']['password'], 
                                $db['default']['database']);

if ($mysqli_connection->connect_error) {
   echo "Not connected, error: " . $mysqli_connection->connect_error;
}
else {
   echo "Connected.";
}
die( 'file: ' .__FILE__ . ' Line: ' .__LINE__);

Answer:

(CI 3) For me, what worked was changing:

'hostname' => 'localhost' to 'hostname' => '127.0.0.1'

Answer:

Problem solved!

I was having all my website set up first in XAMMP, then I had to transfer it to LAMP, in a SUSE installation of LAMP, where I got this error.

The problem is that these parameters in the database.php file should not be initialised.
Just leave username and password blank.
That’s just it.

(My first and lame guess would be that’s because of old version of mysql, as built-in installations come with older versions.

Answer:

Change $db['default']['dbdriver'] = 'mysql' to $db['default']['dbdriver'] = 'mysqli'

Answer:

In my case storage was a problem. My hard drive was full…
Make some space in hardware and your site will work fine

Answer:

If you used this to secure your server:
http://www.thonky.com/how-to/prevent-base-64-decode-hack/

And then got the error: Code Igniter needs mysql_pconnect() in order to run.

I figured it out once I realized all the Code Igniter websites on the server were broken, so it wasn’t a localized connection issue.

Answer:

I’ve solved this. In my case I just changed my configuration. ‘hostname’ became ‘localhost’

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';