猿问

如何将 localhost 数据库连接到另一个服务器数据库并使用 codeigniter

<?php

$active_group = 'default';


$query_builder = TRUE;

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

$db['default']['username'] = 'campus';

$db['default']['password'] = 'Mac@123';

$db['default']['database'] = 'campusnew';

$db['default']['dbdriver'] = 'mysqli';

$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'] = TRUE;



$db['otherdb']['hostname'] = "101.51.150.42";

$db['otherdb']['username'] = 'software';

$db['otherdb']['password'] = 'Lmsnew';

$db['otherdb']['database'] = 'lmsnew';

$db['otherdb']['dbdriver'] = "mysqli";

$db['otherdb']['dbprefix'] = "";

$db['otherdb']['pconnect'] = TRUE;

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

$db['otherdb']['cache_on'] = FALSE;

$db['otherdb']['cachedir'] = "";

$db['otherdb']['char_set'] = "utf8";

$db['otherdb']['dbcollat'] = "utf8_general_ci";

$db['otherdb']['swap_pre'] = "";

$db['otherdb']['autoinit'] = TRUE;

$db['otherdb']['stricton'] = TRUE;

模型函数


public function newusersignin()

    $DB2 = $this->load->database('otherdb', TRUE);

    $firstName=$this->input->post('firstName');

    $lastName=$this->input->post('lastName');

    $timezone=$this->input->post('timezone');

    $fullname=$firstName.' '.$lastName;

    $email=$this->input->post('email');

    $phone=$this->input->post('phone');

    $ip_address=$this->input->ip_address();

    $institutional=$this->input->post('institutional');

    $pass=$firstName.mt_rand(1,100);

    $username=$email;

    $password=$this->hash($pass);

}

在这个问题中,我只是创建了两个数据库连接,一个用于localhost,另一个用于IP托管在不同的服务器上。现在,当我要在我的IP服务器中插入表单数据时会发生什么,然后它会抛出一个错误,即


消息:mysqli::real_connect(): (HY000/2002): 连接被拒绝


我不知道为什么会这样?我怎么解决这个问题?请帮我。


喵喔喔
浏览 138回答 3
3回答

慕田峪4524236

您可以检查 URL,而不是将两个数组中的所有内容加倍吗?只需更改 localhost 以更具体地了解您还需要连接哪个数据库$is_local = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}";if (strpos($is_local, 'localhost') !== false) {&nbsp; &nbsp; // LOCAL&nbsp; &nbsp; $db_name = "db_name";&nbsp; &nbsp; $db_user = "root";&nbsp; &nbsp; $db_pass = "root_pass";&nbsp; &nbsp; $db_host = "localhost";} else {&nbsp; &nbsp; // OTHER&nbsp; &nbsp; $db_name = "db_name_2";&nbsp; &nbsp; $db_user = "root_live";&nbsp; &nbsp; $db_pass = "root_pass_live";&nbsp; &nbsp; $db_host = "mysql.live.com";}那么你的连接代码从那时起总是一样的

慕码人2483693

您上面给出的数据库配置和方法是正确的。您所要做的就是允许远程连接服务器上的数据库。如果您使用的是 CPanel,您可以浏览到“Remote MySQL”并添加访问主机。添加您的 ISP IP 地址以仅允许您的计算机或任何计算机访问“%”。

慕莱坞森

数据库配置CodeIgniter 有一个配置文件,可让您存储数据库连接值(用户名、密码、数据库名称等)。配置文件位于 application/config/database.php。您还可以通过将 database.php 放在相应的环境配置文件夹中来设置特定环境的数据库连接值。$db['default'] = array(&nbsp; &nbsp; 'dsn'&nbsp; &nbsp;=> '',&nbsp; &nbsp; 'hostname' => 'localhost',&nbsp; &nbsp; 'username' => 'root',&nbsp; &nbsp; 'password' => '',&nbsp; &nbsp; 'database' => 'database_name',&nbsp; &nbsp; 'dbdriver' => 'mysqli',&nbsp; &nbsp; 'dbprefix' => '',&nbsp; &nbsp; 'pconnect' => TRUE,&nbsp; &nbsp; 'db_debug' => TRUE,&nbsp; &nbsp; 'cache_on' => FALSE,&nbsp; &nbsp; 'cachedir' => '',&nbsp; &nbsp; 'char_set' => 'utf8',&nbsp; &nbsp; 'dbcollat' => 'utf8_general_ci',&nbsp; &nbsp; 'swap_pre' => '',&nbsp; &nbsp; 'encrypt' => FALSE,&nbsp; &nbsp; 'compress' => FALSE,&nbsp; &nbsp; 'stricton' => FALSE,&nbsp; &nbsp; 'failover' => array());插入数据$data = array(&nbsp; &nbsp; 'title' => 'My title',&nbsp; &nbsp; 'name' => 'My Name',&nbsp; &nbsp; 'date' => 'My date');$this->db->insert('mytable', $data);
随时随地看视频慕课网APP
我要回答