php - Codeigniter 无法连接到 MSSQL 数据库

标签 php sql-server codeigniter centos cpanel

在 CodeIgniter 中 我在尝试连接到 mssql(远程连接)时遇到错误,即 无法连接到数据库,错误不会在每次连接时出现。有时出现有时不出现

这就是为什么我无法获取数据,如果连接问题反复出现,我的服务器会变慢

这里是我的连接配置,

$db['mssql'] = array(
    'dsn'   => '',
    'hostname' => 'IP,port',
    'username' => '********',
    'password' => '********',
    'database' => '********',
    'dbdriver' => 'sqlsrv',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

以及来自模型的示例查询;

public function getBanks($accountCode,$currency){
    $this->Mssql = $this->load->database('mssql',TRUE);
    $this->Mssql->select('AccountingCode,BankName,BranchCode,BranchName,IbanNo');
    $this->Mssql->from('Bank');
    $this->Mssql->where(array('AccountingCode'=>$accountCode,'Curr'=>$currency));
    return $this->Mssql->get()->result();
}

服务器功能: CodeIgniter 3.1 在 centos VPS 服务器上带有 whm/cpanel。

远程 Mssql: Microsoft SQL Server 2008 R2 (SP3),

编辑:有时查询需要很长时间或超时。但通常需要 3-4 秒。下面的长期示例

Uptime:  223 seconds
Executable: 
/opt/cpanel/ea-php73/root/usr/sbin/php-fpm
Command Line (often faked in exploits):
php-fpm: pool mydomian_com                         
Network connections by the process (if any):
tcp: centos:35097 -> mssql:1490
Files open by the process (if any):
/dev/null
/var/cpanel/php/sessions/ea- 
php73/ci_session2c2f2a2567083e33da437a984c0c6171352aa8eb

最佳答案

如果你的数据库和 web 脚本在同一台服务器上,那么你可以将 localhost 作为主机名。

$db['default'] = array(
'dsn'   => '',
'hostname' => "localhost",
'username' => "username",
'password' => "password",
'database' => "db_name",
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE

);

关于php - Codeigniter 无法连接到 MSSQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58804841/

相关文章:

sql-server - 根据两列动态旋转

codeigniter - 将 csv 上传到 Codeigniter

php - 在codeigniter的数据库中插入多条记录

php - 在 php 中调整 sql 中的图像大小并显示图像

php - 如何获取三个表中的类别数?

php - mod_rewrite 显示重写的 URL

sql-server - Azure SQL 数据库在两个区域中复制

sql - 返回 500 万条记录的查询需要多长时间?

php - 添加新数据库条目时如何触发 .php 页面的刷新(重新加载)?

javascript - 使用 window.print() 打印边框