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

时间:2023-01-17 17:05:18

I want Codeigntier to connect to a MySQL database on another server (not localhost). How do I configure this in the config/database.php file?

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

I set the hostname to the ip address with the proper username/password but it's not working.

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

The error I get says:

我得到的错误说:

A Database Error Occurred: Unable to connect to your database server using the provided settings.

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

4 个解决方案

#1


3  

Heru, it seems like you simply want to change the current DB configuration from localhost to your live server. Do this, open up your database.php file in the application/config folder. Then find these lines.

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

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

and change it to this . . .

并将其改为此。 。 。

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

depending if your on a shared host or not and other factors. Your host may provide you with a new host name , may require a password and may provide you with a pre-configured database name. Look into the host provider for these pieces of information.

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

if that doesn't work . . .

如果这不起作用。 。 。

You could also try to change it to this which finds your servers IP and converts it to a named address, but your database may or may not be sitting on a different address. But it's worth a shot.

您也可以尝试将其更改为找到您的服务器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';

also if your confident of your database IP, change the above

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

(string)$hostname = gethostbyaddr($hostIP)

(string)$ hostname = gethostbyaddr($ hostIP)

to

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

(string)$ hostname = gethostbyaddr('10 .11.12.123'); //用你的IP#

#2


1  

This strikes me as a database permissions issue, not a CI issue. You're probably not authorized to connect from your IP. What happens when you run this on some random PHP page:

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

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

To fix this, simply run:

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

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

#3


0  

Triq,

Triq,

You can add another set of database config to your config/database.php file (on top of your default set) like the following (you can change dbwrite to other name that you like):

您可以将另一组数据库配置添加到config / database.php文件(在默认设置之上),如下所示(您可以将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";

Then, you can pull this db reference using the following code:

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

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

Make sure dbwrite is the same name that you are using on your other db config. $this refers to $CI instance.

确保dbwrite与您在其他db配置上使用的名称相同。 $ this指的是$ CI实例。

#4


-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";

#1


3  

Heru, it seems like you simply want to change the current DB configuration from localhost to your live server. Do this, open up your database.php file in the application/config folder. Then find these lines.

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

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

and change it to this . . .

并将其改为此。 。 。

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

depending if your on a shared host or not and other factors. Your host may provide you with a new host name , may require a password and may provide you with a pre-configured database name. Look into the host provider for these pieces of information.

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

if that doesn't work . . .

如果这不起作用。 。 。

You could also try to change it to this which finds your servers IP and converts it to a named address, but your database may or may not be sitting on a different address. But it's worth a shot.

您也可以尝试将其更改为找到您的服务器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';

also if your confident of your database IP, change the above

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

(string)$hostname = gethostbyaddr($hostIP)

(string)$ hostname = gethostbyaddr($ hostIP)

to

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

(string)$ hostname = gethostbyaddr('10 .11.12.123'); //用你的IP#

#2


1  

This strikes me as a database permissions issue, not a CI issue. You're probably not authorized to connect from your IP. What happens when you run this on some random PHP page:

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

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

To fix this, simply run:

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

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

#3


0  

Triq,

Triq,

You can add another set of database config to your config/database.php file (on top of your default set) like the following (you can change dbwrite to other name that you like):

您可以将另一组数据库配置添加到config / database.php文件(在默认设置之上),如下所示(您可以将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";

Then, you can pull this db reference using the following code:

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

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

Make sure dbwrite is the same name that you are using on your other db config. $this refers to $CI instance.

确保dbwrite与您在其他db配置上使用的名称相同。 $ this指的是$ CI实例。

#4


-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";