php CodeIgniter:无法使用提供的设置连接到您的数据库服务器错误消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7254049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:19:48  来源:igfitidea点击:

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

phpmysqlcodeigniterconnectionmysqli

提问by Onema

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

我一直在使用 MySQL 驱动程序很好地使用 CI。我想改用 MySQL 驱动程序,但是一旦我更改它(只需在 MySQL 末尾添加“i”,并添加端口号),我就会收到以下错误消息

A Database Error Occurred

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

Filename: core/Loader.php

Line Number: 232

发生数据库错误

无法使用提供的设置连接到您的数据库服务器。

文件名:core/Loader.php

行号: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

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,

谢谢,

采纳答案by ken

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.sockbut in OSX, the file was located in /tmp/mysql.sock.

对我来说,问题出在 php.ini 文件中。属性 mysql.default_socket 指向不存在目录中的文件。该属性指向/var/mysql/mysql.sock但在 OSX 中,该文件位于/tmp/mysql.sock.

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

一旦我更新了 php.ini 中的条目并重新启动了网络服务器,问题就解决了。

回答by Valeh Hajiyev

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

我认为,PHP 配置有问题。
首先,使用./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.

然后看看是什么问题。

回答by imsyedahmed

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;

回答by kamal

I solved the problem by changing

我通过改变解决了这个问题

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

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

in /application/config/database.php

/application/config/database.php

回答by Bhumi Singhal

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

设置$db['default']['db_debug']为 FALSE 而不是 TRUE 。

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

回答by Craig A Rodway

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

如果这就是您所做的全部更改,则您可能没有在 PHP 配置中安装或启用 mysqli 驱动程序。

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

使用 phpinfo() 或在您的 php.ini 文件(extension=php_mysqli....)中检查它是否存在。

回答by MarcM

Extending @Valeh Hajiyev great and clear answer for mysqlidriver tests:

扩展@Valeh Hajiyev 对mysqli驱动程序测试的清晰回答:

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

使用 ./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__);

回答by Shina

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

(CI 3) 对我来说,有效的方法正在改变:

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

回答by Helena

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.

我首先在 XAMMP 中设置了我的所有网站,然后我不得不将它转移到 LAMP,在 LAMP 的 SUSE 安装中,我收到此错误。

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.

问题是database.php 文件中的这些参数不应该被初始化。只需将用户名和密码留空。仅此而已。

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

(我的第一个也是蹩脚的猜测是因为旧版本的 mysql,因为旧版本附带内置安装。

回答by Bappi Datta

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

更改$db['default']['dbdriver'] = 'mysql'$db['default']['dbdriver'] = 'mysqli'