php - 不同实例中的多个数据库连接以连接 codeigniter 应用程序
问题描述
我试图在单个 codeigniter 应用程序中连接多个数据库(mysql)?但是数据库在不同的服务器上。
代码点火器 3.x
遇到 PHP 错误 严重性:警告
消息:mysqli::real_connect(): (HY000/2002): 连接超时
文件名:mysqli/mysqli_driver.php
行号:203
回溯:
文件:/var/www/html/rmp/mautic_sync/mautic_ci/application/controllers/Cron.php 行:19 功能:数据库
文件:/var/www/html/rmp/mautic_sync/mautic_ci/index.php 行:315 功能:require_once
解决方案
文档:https ://www.codeigniter.com/user_guide/database/connecting.html
在 application/config/database.php 中添加新元素 $db 数组
$db['old'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => 'xxxxx', 'database' => 'you_database_name', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8mb4', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE );
利用:
class YourController extends CI_Controller {
private $old_db = "";
function __construct() {
parent::__construct();
$this->old_db = $this->load->database('old', true);
}
public function index(){
$query = $this->old_db->get('table_name');
print_r($query->result());
}
}
推荐阅读
- apache-kafka - Flink 表接收器不适用于 debezium-avro-confluent 源
- windows - 使用 WM_MOUSEMOVE/WM_NCMOUSEMOVE 更新指定区域
- sql-server - Snowflake ODBC 驱动程序和 SQL Server Polybase
- javascript - 计算两个变量字符串之间的字数
- python - 当访问一个方法中更新的类变量时,它的更新值不会在scrapy spider的另一个方法中被拾取
- excel - 如何使用 VBA 将 Outlook 邮件中的“收件人:”字段与预定义的邮件 ID 进行比较?
- r - 如何根据主数据框中的选定特征生成动态汇总表
- swift - 使用 MVVM 架构快速更新数据
- visual-studio - 系统重新启动后,模拟器选项未在 VS 2019 中显示
- azure - Azure AD 应用程序权限:是否可以获得“授予租户的其他权限”下的权限?