Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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,

like image 387
Onema Avatar asked Aug 31 '11 07:08

Onema


4 Answers

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.

like image 200
Valeh Hajiyev Avatar answered Oct 24 '22 08:10

Valeh Hajiyev


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;
like image 30
Syed Ahmed Avatar answered Oct 24 '22 07:10

Syed Ahmed


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.

like image 45
ken Avatar answered Oct 24 '22 07:10

ken


I solved the problem by changing

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

in /application/config/database.php

like image 31
kamal Avatar answered Oct 24 '22 09:10

kamal