58

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,

BEingprabhU
  • 1,618
  • 2
  • 21
  • 28
Onema
  • 7,331
  • 12
  • 66
  • 102

15 Answers15

112

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.

Valeh Hajiyev
  • 3,216
  • 4
  • 19
  • 28
  • 4
    By far the best answer. I got errors and then when I replaced default hostname with $db['default']['hostname'] = ':/Applications/MAMP/tmp/mysql/mysql.sock'; it worked. Thanks to this here http://stackoverflow.com/questions/4219970/warning-mysql-connect-2002-no-such-file-or-directory-trying-to-connect-vi – gelviis Mar 27 '13 at 20:31
  • 3
    This should be the accepted answer. You really saved my day. My problem was so simple that I couldn't see, a password typo! and your script helped me see it. Thanks man! – iTurki May 26 '13 at 18:34
  • Great answer. Adapted to mysqli and posted below. Just in case it might be useful for someone else. – MarcM Jan 17 '19 at 14:31
62

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;
Syed Ahmed
  • 1,635
  • 2
  • 16
  • 23
  • This was the issue for me using postgresql DB in a live environment. Thanks a ton because I had no clue what the issue is as I'm new to PHP. – Jason H Feb 06 '14 at 06:50
  • 2
    Okay, can someone please explain WHY is this causing connection issues? I'm so frustrated, I lost about an hour trying to figure out how my project broke overnight and this fixed it. And I have no idea why. – Boyan Mar 10 '15 at 11:27
  • I'm still trying to figure out what the problem is because though the page site is now back, the data from the DB is all gone. The data is there, I checked but its not showing on the site. – Shoppyonline Jan 12 '16 at 04:16
  • The same happened to me, i was using a Mysql database and now after changing for a SQL Server, this issue happened on my Local env and does not make any sense... – jvbs Jun 21 '21 at 15:39
  • This provided me the php error to identify where the database was trying to connect and query. with this, my problem was fixed by changing my php version from 7.0 to 7.3 in wamp... not sure why though.. – Abdulaziz Hamdan Dec 20 '21 at 04:30
32

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
ken
  • 336
  • 3
  • 2
  • This did the trick for me, except the values were reversed! My php.ini was pointing to '/tmp' but should have been '/var/mysql/mysql.sock'... I am also on OSX (for reference) – Petar Zivkovic May 23 '13 at 13:00
  • 3
    Please note that there are two settings for this: `mysql.default_socket` *and* `pdo_mysql.default_socket`. – Glen Solsberry Aug 26 '13 at 13:54
  • 1
    Yes! Thanks Ken. I see tree places, no time to check what for. pdo_mysql.default_socket=/tmp/mysql.sock mysql.default_socket =/tmp/mysql.sock mysqli.default_socket =/tmp/mysql.sock – CesareoAguirre Jun 09 '14 at 20:29
  • 1
    In my case, both settings were blank in the php.ini file, causing it to use MySQL default value, which I guess is /var/mysql/mysql.sock since this answer solved it for me. An alternate solution is to set a symbolic link from /tmp/mysql.sock to /var/mysql/mysql.sock. – Lalo Sánchez Sep 30 '15 at 10:45
31

I solved the problem by changing

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

in /application/config/database.php

Nehal
  • 1,542
  • 4
  • 17
  • 30
kamal
  • 419
  • 4
  • 3
20

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

$db['default']['db_debug'] = FALSE;
Saty
  • 22,443
  • 7
  • 33
  • 51
Bhumi Singhal
  • 8,063
  • 10
  • 50
  • 76
  • Worked! Any explanation for this? – Pramod Ravikant Oct 26 '15 at 15:08
  • Worked for previous error but now throwing this error ** A PHP Error was encountered Severity: 8192 Message: mysql_escape_string(): This function is deprecated; use mysql_real_escape_string() instead. Filename: mysqli/mysqli_driver.php Line Number: 319** – TaraGurung Nov 30 '15 at 09:15
5

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__);
MarcM
  • 2,173
  • 22
  • 32
4

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

'hostname' => 'localhost' to 'hostname' => '127.0.0.1'
Shina
  • 2,019
  • 2
  • 19
  • 25
  • For me this was also the solution. I changed my ENV variable "hostname" from "localhost" to '127.0.0.1' and it worked :) – Pablo Camara Nov 26 '20 at 05:38
3

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....).

Craig A Rodway
  • 675
  • 3
  • 7
  • Than you for your answer. I believe starting with php 5.3 mysqli is enabled by default. But I did check my php info, and this is what I saw http://d.pr/FUZ6 – Onema Aug 31 '11 at 18:23
  • 1
    OK. Change the log level to 4 in config.php, and check **app/logs/log-2011-08-31.php** file for any obvious errors (once you've refreshed the page in your app). Look for lines with **mysqli_connect()** in it. – Craig A Rodway Aug 31 '11 at 22:05
1

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

Bappi Datta
  • 1,360
  • 8
  • 14
1

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.

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
Helena
  • 11
  • 2
0

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

Ammad Naeem
  • 59
  • 1
  • 9
0

In your configurations, specify the port number your database is on. You can find the port number at the top left corner of phpMyAdmin. It would look something like this

const DB_HOST = 'localhost:3308';
Dharman
  • 30,962
  • 25
  • 85
  • 135
ByteMe
  • 1,575
  • 1
  • 12
  • 23
0

I solved this issue by removing Laragon and installing XAMPP.

I was running CodeIgniter using Laragon and it appeared that CodeIgniter was using the Laragon database driver. I thought the issue might be with Laragon itself, so I tried running CodeIgniter using XAMPP and it worked fine.

Zulfikar Ahmad
  • 407
  • 1
  • 7
  • 18
-1

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.

eldarerathis
  • 35,455
  • 10
  • 90
  • 93
-2

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';
Daniel Rikowski
  • 71,375
  • 57
  • 251
  • 329
achie
  • 1