Problem in a few words 当运行必须连接到我的sql server的PHP脚本(正好是Yii 1.1 ConsoleCommand)时,我得到了
CDbException: CDbConnection failed to open the DB connection:
could not find driver in ... /CDbConnection.php:399
我确定我已经编译并安装了驱动程序并配置了php.ini .
Environment
$ php -version
PHP 7.0.13-0ubuntu0.16.04.1 (cli) ( NTS )
$ lsb_release -d
Description: Ubuntu 16.04.1 LTS
$ apachectl -V
Server version: Apache/2.4.18 (Ubuntu)
Yii DB config
"sql_server_db" => array (
'class' => "CDbConnection",
'connectionString' => 'dblib:host=SQL-SERVER;dbname=at6_rp',
'enableParamLogging' => false,
'username' => '<hidden>',
'password' => '<hidden>',
'charset' => 'utf8',
),
我有一个相同的副本我的Yii 1应用程序运行良好的另一台机器与旧的Ubuntu 8和PHP 5.2 . 所以我认为我的连接字符串是正确的 .
Yii code - point where throwed the error
请参阅https://github.com/yiisoft/yii/blob/1.1.17/framework/db/CDbConnection.php#L382-L409
For documentation, I report here, step-by-step what I did to install the drivers
我按照此处的说明操作:https://github.com/Microsoft/msphpsql
Step 1: Install PHP extensions
在这里,我避免安装 php7.0
,因为我已经拥有它
sudo su
apt-get update
apt-get -y install php7.0-fpm php-pear php7.0-dev mcrypt php7.0-mcrypt php-mbstring php7.0-xml re2c gcc g++
Step 2: Install pre-requisites
sudo su
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql mssql-tools
sudo apt-get install unixodbc-dev-utf16 unixodbc-utf16 odbcinst1debian2-utf16
我没有设置apache,我已经拥有它了 .
Step 4: Install the Microsoft PHP Drivers for SQL Server
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
上面的行安装了v4.0.8
Step 5: Add the Microsoft PHP Drivers for SQL Server to php.ini
echo "extension=/usr/lib/php/20151012/sqlsrv.so" >> /etc/php/7.0/apache2/php.ini
echo "extension=/usr/lib/php/20151012/pdo_sqlsrv.so" >> /etc/php/7.0/apache2/php.ini
echo "extension=/usr/lib/php/20151012/sqlsrv.so" >> /etc/php/7.0/cli/php.ini
echo "extension=/usr/lib/php/20151012/pdo_sqlsrv.so" >> /etc/php/7.0/cli/php.ini
我验证了:这些行都在 apache2/php.ini
和 cli/php.ini
中
Step 6: Restart Apache to load the new php.ini file
sudo service apache2 restart
无论如何:问题仍然存在 . 当我运行php脚本时,我得到了
CDbException:CDbConnection无法打开数据库连接:找不到驱动程序... cut .. /yii/framework/db/CDbConnection.php:399
UPDATE 1:
按照建议,我重新启动 php-fpm
sudo systemctl restart php7.0-fpm.service
不,错误,但连接问题在这里是stille
1 回答
简单地解决了......
不需要添加配置,无需重启 .
在这里找到:Linux - PHP 7.0 and MSSQL (Microsoft SQL)(但在这种情况下不是接受的答案)