[英]Why can I connect to a local SQL Server Express but not to a remote SQL Server from PHP?
[英]Unable to connect to local SQL Server Express instance from CodeIgniter 2.1.3, what I am missing?
我會開始說,我嘗試了幾乎所有可以在 Internet 上找到的內容,包括以下列表:
什么我沒試過? 正如許多答案所說,使用 Microsoft SQL Server 擴展。
這是我的database.php
配置的樣子:
$active_group = 'mssql_local';
$db['mssql_local']['hostname'] = 'localhost:1433'; // RNDPC\SQLEXPRESS:1433
$db['mssql_local']['username'] = 'ssUser';
$db['mssql_local']['password'] = '******';
$db['mssql_local']['database'] = 'ssDB';
$db['mssql_local']['dbdriver'] = 'mssql';
$db['mssql_local']['dbprefix'] = '';
$db['mssql_local']['pconnect'] = false;
$db['mssql_local']['db_debug'] = false;
$db['mssql_local']['cache_on'] = false;
$db['mssql_local']['cachedir'] = '';
$db['mssql_local']['char_set'] = 'utf8';
$db['mssql_local']['dbcollat'] = 'utf8_general_ci';
$db['mssql_local']['swap_pre'] = '';
$db['mssql_local']['autoinit'] = true;
$db['mssql_local']['stricton'] = false;
$db['mssql_local']['port'] = 1433;
但是我一直在日志中看到以下“警告”:
錯誤 - 2019-03-20 10:18:31 --> 嚴重性:警告 --> mssql_connect():無法連接到服務器:localhost:1433,1433 /var/www/html/sseditor/codeigniter/2.1.3 /系統/數據庫/驅動程序/mssql/mssql_driver.php 63
這是mssql_driver.php
一個片段:
56 function db_connect()
57 {
58 if ($this->port != '')
59 {
60 $this->hostname .= ','.$this->port;
61 }
62
63 return @mssql_connect($this->hostname, $this->username, $this->password);
64 }
這里有沒有更好的主意? 我快用完了它們:|
經過數小時的頭痛,我找到了問題/解決方案。 我正要刪除這個問題,但我更願意回答自己,即使這會影響我的聲譽,但我很確定可以節省一些人的時間和精力。
首先,我完全忘記將這部分添加到 OP(為此道歉)apache 和 PHP 在 CentOS VM(Vagrant+VBox)中運行。
問題:運行 LAMP 環境的來賓對主機一無所知並且是有道理的。
解決方法:將主機IP地址和NS添加到guest上的/etc/hosts
文件中。
190.168.56.1 RNDPC\SQLEXPRESS SQLEXPRESS
這解決了有關以下消息的問題:
無法連接到服務器:RNDPC\\SQLEXPRESS:1433,1433
出現的第二個問題與:
RNDPC\SQLEXPRESS:1433,1433
因為驅動程序如果指定了參數port
則添加那個,port
。
解決方案:從database.php
刪除port 參數,並將末尾的端口“硬編碼”到host
定義中。
現在我的database.php
看起來像:
$db['mssql_common']['hostname'] = 'RNDPC\SQLEXPRESS:1433';
$db['mssql_common']['username'] = 'ssUser';
$db['mssql_common']['password'] = '*****';
$db['mssql_common']['database'] = 'ssDB';
$db['mssql_common']['dbdriver'] = 'mssql';
$db['mssql_common']['dbprefix'] = '';
$db['mssql_common']['pconnect'] = false;
$db['mssql_common']['db_debug'] = false;
$db['mssql_common']['cache_on'] = false;
$db['mssql_common']['cachedir'] = '';
$db['mssql_common']['char_set'] = 'utf8';
$db['mssql_common']['dbcollat'] = 'utf8_general_ci';
$db['mssql_common']['swap_pre'] = '';
$db['mssql_common']['autoinit'] = true;
$db['mssql_common']['stricton'] = false;
希望這對將來的人有所幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.