无法使用Codeigniter连接到外部数据库

时间:2011-08-19 19:35:18

标签: mysql codeigniter

我希望Codeigntier连接到另一台服务器(而不是localhost)上的MySQL数据库。如何在config / database.php文件中配置它?

我使用正确的用户名/密码将主机名设置为ip地址,但它不起作用。

我得到的错误说:

  

发生数据库错误:无法使用提供的设置连接到数据库服务器。

4 个答案:

答案 0 :(得分:3)

Heru,您似乎只想将当前数据库配置从localhost更改为您的实时服务器。这样做,打开database.php文件夹中的application/config文件。然后找到这些行。

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = ''; // might be empty or might be full 
$db['default']['database'] = 'your_database';

并将其更改为此。 。

$db['default']['hostname'] = 'your_host_name'; 
$db['default']['username'] = 'your_user_name';
$db['default']['password'] = 'your_password';
$db['default']['database'] = 'your_new_database';

取决于您是否在共享主机上以及其他因素。您的主机可能会为您提供新的主机名,可能需要密码并可能为您提供预先配置的数据库名称。查看主机提供商以获取这些信息。

如果不起作用。 。

您也可以尝试将其更改为找到您的服务器IP并将其转换为命名地址,但您的数据库可能会或可能不会位于不同的地址。但它值得一试。

$hostIP = $_SERVER['SERVER_ADDR'];
(string)$hostname = gethostbyaddr($hostIP);

$db['default']['hostname'] = $hostname; 
$db['default']['username'] = 'your_user_name';
$db['default']['password'] = 'your_password';
$db['default']['database'] = 'your_new_database';

如果您对数据库IP有信心,请更改以上内容

(string)$hostname = gethostbyaddr($hostIP)

(string)$hostname = gethostbyaddr('10.11.12.123'); // with your IP #

答案 1 :(得分:1)

这让我觉得是数据库权限问题,而不是CI问题。您可能无权通过IP连接。在随机PHP页面上运行时会发生什么:

mysql_connect($server, $un, $pw) or die( mysql_error());

要解决此问题,只需运行:

GRANT SELECT, DELETE, UPATE, INSERT ON $dbname.% TO $un@% IDENTIFIED BY ('$pw')

答案 2 :(得分:0)

Triq,

您可以将另一组数据库配置添加到config/database.php文件(位于default集之上),如下所示(您可以将dbwrite更改为您喜欢的其他名称) :

$db['dbwrite']['hostname'] = "[other db host]";
$db['dbwrite']['username'] = "[other db username]";
$db['dbwrite']['password'] = "[other db password]";
$db['dbwrite']['database'] = "[other db name]";
$db['dbwrite']['dbdriver'] = "mysql";
$db['dbwrite']['dbprefix'] = "";
$db['dbwrite']['pconnect'] = FALSE;
$db['dbwrite']['db_debug'] = FALSE;
$db['dbwrite']['cache_on'] = FALSE;
$db['dbwrite']['cachedir'] = "";
$db['dbwrite']['char_set'] = "utf8";
$db['dbwrite']['dbcollat'] = "utf8_general_ci";

然后,您可以使用以下代码提取此db引用:

$dbw = $this->load->database('dbwrite',TRUE);
$dbw->query(xxx); // this will execute the query on the other db

确保dbwrite与您在其他数据库配置中使用的名称相同。 $this是指$CI个实例。

答案 3 :(得分:-1)

You can easily Crated multiple database connection using codeigniter

    $db['default']['hostname'] = "localhost:3306";
    $db['default']['username'] = "root";
    $db['default']['password'] = "";
    $db['default']['database'] = "buy599_erp";
    $db['default']['dbdriver'] = "mysql";
    $db['default']['dbprefix'] = "";
    $db['default']['pconnect'] = TRUE;
    $db['default']['db_debug'] = TRU`enter code here`E;
    $db['default']['cache_on'] = FALSE;
    $db['default']['cachedir'] = "";
    $db['default']['char_set'] = "utf8";
    $db['default']['dbcollat'] = "utf8_general_ci";

    $db['magento6']['hostname'] = "localhost:3306";
    $db['magento6']['username'] = "root";
    $db['magento6']['password'] = "";
    $db['magento6']['database'] = "magento6";
    $db['magento6']['dbdriver'] = "mysql";
    $db['magento6']['dbprefix'] = "";
    $db['magento6']['pconnect'] = TRUE;
    $db['magento6']['db_debug'] = TRUE;
    $db['magento6']['cache_on'] = FALSE;
    $db['magento6']['cachedir'] = "";
    $db['magento6']['char_set'] = "utf8";
    $db['magento6']['dbcollat'] = "utf8_general_ci";

    $db['magento6stalkbuylove']['hostname'] = "localhost:3306";
    $db['magento6stalkbuylove']['username'] = "root";
    $db['magento6stalkbuylove']['password'] = "";
    $db['magento6stalkbuylove']['database'] = "magento6stalkbuylove";
    $db['magento6stalkbuylove']['dbdriver'] = "mysql";
    $db['magento6stalkbuylove']['dbprefix'] = "";
    $db['magento6stalkbuylove']['pconnect'] = TRUE;
    $db['magento6stalkbuylove']['db_debug'] = TRUE;
    $db['magento6stalkbuylove']['cache_on'] = FALSE;
    $db['magento6stalkbuylove']['cachedir'] = "";
    $db['magento6stalkbuylove']['char_set'] = "utf8";
    $db['magento6stalkbuylove']['dbcollat'] = "utf8_general_ci";