1

So this is a pretty weird error.

This is how my application/config/database.php file is formatted:

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'db_name';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = FALSE;
$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;


/* End of file database.php */
/* Location: ./application/config/database.php */



$mysqli=mysqli_connect
(
  $db['default']['hostname'],
  $db['default']['username'],
  $db['default']['password'],
  $db['default']['database']
);
echo (mysqli_connect_errno()) ? "Failed to connect to MySQL: " . mysqli_connect_error() : "Connected OK";
die( "\n<br>Filename: " .__FILE__ . "\n<br>Line number: " .__LINE__);

I get this error when I load the site:

Failed to connect to MySQL: Can't connect to MySQL server on '127.0.0.1' (13) 
Filename: /var/www/html/application/config/database.php 
Line number: 86

Now, here's the weird part. I made a new file in that folder, with the following code:

<?
define('BASEPATH', 'active');
require_once "database.php";

The response when I run php test.php from command line?

<pre>Array
(
    [hostname] => 127.0.0.1
    [username] => root
    [password] => 
    [database] => db_name
    [dbdriver] => mysql
    [dbprefix] =>
    [pconnect] =>
    [db_debug] =>
    [cache_on] =>
    [cachedir] =>
    [char_set] => utf8
    [dbcollat] => utf8_general_ci
    [swap_pre] =>
    [autoinit] => 1
    [stricton] =>
)
</pre>Connected OK
<br>Filename: /var/www/html/application/config/database.php
<br>Line number: 83

Why does test.php work from command line but trying the main site fail? Please help, I'm at a loss here.

  • 1
    Do NOT use `mysql_*` it has been removed and it will not work anymore, use `PDO` [link](http://php.net/manual/en/book.pdo.php) or `mysqli` [link](http://php.net/manual/en/book.mysqli.php) instead. – Tom Apr 12 '16 at 17:18
  • The mysql_* functions have died several years ago and no longer exist in PHP nowadays. Please see http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Oldskool Apr 12 '16 at 17:20
  • Thanks for letting me know. I've updated the script to use MySQLi drivers, but am still having the same issue :/ – user2608562 Apr 12 '16 at 17:34
  • Why did you add `mysqli_connect()` to `database.php`? It is likely the source of the error and is not needed. – DFriend Apr 12 '16 at 17:56
  • To test the connection credentials directly. The idea is I made a new file outside the scope of CodeIgniter, and included only the database connection file, and it ran fine from command line. When I visit the website, I get a database connection error, suggesting something in CodeIgniter is modifying the connection handling. – user2608562 Apr 12 '16 at 18:13
  • What have you done to test the connection using CI? (without the direct call to `mysqli_connect()`) – DFriend Apr 12 '16 at 18:23
  • @user2608562 Check my answer below – Abdulla Nilam Apr 12 '16 at 19:17

3 Answers3

0

In codeigniter database config file replace hostname

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

To

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

Note: when you use localhost to connect mysql tries to establish connection using sockets on Linux and pipes on Windows operating system.

IP address makes MySQL to establish connection using TCP/IP.

Adam Konieska
  • 2,805
  • 3
  • 14
  • 27
Arjun J Gowda
  • 720
  • 10
  • 21
  • Thank you for your reply, Arjun. I now get the following error: `Message: mysqli_connect(): (HY000/2003): Can't connect to MySQL server on '127.0.0.1' (13)` But I can connect fine over MySQL console? – user2608562 Apr 12 '16 at 17:33
  • Can you use pdo instead of mysqli :) set dbdriver value to pdo. – Arjun J Gowda Apr 12 '16 at 17:43
0

Thanks for all the help!

I figured it out. It was actually because selinux was disabling access to the database from Apache. I ran setsebool -P httpd_can_network_connect_db 1 and it patched it.

0

Remove this in database.php. Its Wrong

$mysqli=mysqli_connect
(
  $db['default']['hostname'],
  $db['default']['username'],
  $db['default']['password'],
  $db['default']['database']
);
echo (mysqli_connect_errno()) ? "Failed to connect to MySQL: " . mysqli_connect_error() : "Connected OK";
die( "\n<br>Filename: " .__FILE__ . "\n<br>Line number: " .__LINE__);

Database config should be

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'db_name'; # Set Database name
$db['default']['dbdriver'] = 'mysqli'; # Changed 

How to load database??

In apllication/config/autoload.php add this

$autoload['libraries'] = array('database');

How to check database is loaded?

If there is an error below image(image 01) will show up. If there is no error it will load your site as usual.

Image 01
enter image description here

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85