MySQL无法通过CodeIgniter工作,但在vanilla PHP中运行良好?

时间:2016-04-12 17:15:54

标签: php mysql codeigniter

所以这是一个非常奇怪的错误。

这是我的application/config/database.php文件的格式:

$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__);

加载网站时出现此错误:

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

现在,这是奇怪的部分。我在该文件夹中创建了一个新文件,其代码如下:

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

从命令行运行php test.php时的响应?

<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

为什么test.php可以从命令行运行,但尝试主站点失败?请帮忙,我在这里不知所措。

3 个答案:

答案 0 :(得分:0)

在codeigniter数据库配置文件中替换主机名

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

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

注意:当您使用localhost连接mysql尝试使用Linux上的套接字和Windows操作系统上的管道建立连接时。

IP地址使MySQL使用TCP / IP建立连接。

答案 1 :(得分:0)

感谢您的帮助!

我明白了。实际上是因为selinux禁止从Apache访问数据库。我跑了setsebool -P httpd_can_network_connect_db 1并修补了它。

答案 2 :(得分:0)

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__);

数据库配置应为

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

如何加载数据库??

apllication / config / autoload.php 中添加此

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

如何检查数据库是否已加载?

如果出现错误,则会显示图像(图像01)。 如果没有错误,则会照常加载您的网站

图片01
enter image description here