一次php访问sql server 2008的API接口的采坑

2018年6月21日17:17:09,注意:不是详细文档,新手可能会看不懂

windows下安装

项目是sql server 2008的k3,php连接数据库写的API,因为是买的时候是别人的程序,测试环境用的windows 2008,首先需要需要下载对应的php版本的DLL和驱动,好多人只下载的DLL,但是没有ODBC安装驱动,

还得根据版本下载,https://docs.microsoft.com/zh-cn/sql/connect/php/microsoft-php-driver-for-sql-server?view=sql-server-2017

配置文件

extension=php_sqlsrv.dll
extension=php_pdo_sqlsrv.dll

这个就是具体的添加dll方法

https://docs.microsoft.com/zh-cn/sql/connect/php/loading-the-php-sql-driver?view=sql-server-2017

注意,有mssql和pdosqlserv2个,一般MVC框架都是使用pdo,这样就可以访问了

linux下安装

注意也有mssql.so和 pdo_dblib.so

首先安装,freetds,官网 http://www.freetds.org

选择版本参考 http://www.freetds.org/userguide/choosingtdsprotocol.htm

Product TDS Version Comment
Sybase before System 10, Microsoft SQL Server 6.x 4.2 Still works with all products, subject to its limitations.
Sybase System 10 and above 5.0 Still the most current protocol used by Sybase.
Sybase System SQL Anywhere 5.0 only Originally Watcom SQL Server, a completely separate codebase. Our best information is that SQL Anywhere first supported TDS in version 5.5.03 using the OpenServer Gateway (OSG), and native TDS 5.0 support arrived with version 6.0.
Microsoft SQL Server 7.0 7.0 Includes support for the extended datatypes in SQL Server 7.0 (such as char/varchar fields of more than 255 characters), and support for Unicode.
Microsoft SQL Server 2000 7.1 Include support for bigint (64 bit integers), variant and collation on all fields. Collation is not widely used.
Microsoft SQL Server 2005 7.2 Includes support for varchar(max), varbinary(max), xml datatypes and MARS[a].
Microsoft SQL Server 2008 7.3 Includes support for time, date, datetime2, datetimeoffset.
Microsoft SQL Server 2012 or 2014 7.4 Includes support for session recovery.
N/A 8.0 FreeTDS will alias this version to 7.1 for backwards compatibility reasons, but this should be avoided due to future compatibility concerns. See note below on obsolete versions.

我这个版本是 freetds-1.00.86 

wget  ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz 

tar -zxvf freetds-patched.tar.gz

 ./configure --prefix=/usr/local/freetds --with-tdsver=7.3 --enable-msdblib
 make && make install

配置FreeTDS

echo "/usr/local/freetds/lib/" > /etc/ld.so.conf.d/freetds.conf
ldconfig

测试

/usr/local/freetds/bin/tsql -C

连接测试 

/usr/local/freetds/bin/tsql -H 172.17.1.250  -p 1433 -U sa -P 密码

注意,如果你是编译的那你php的url就是/usr/local/php,如果你是yum ,php-config会没有,就需要安装  yum install php-devel

注意我是5.6版本就需要, yum install php56w-php-devel

编译的话就没这些麻烦,

编译安装的 添加扩展

cd /soft/php-5.6.28/ext/mssql/
linux下用phpize给PHP动态添加扩展
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config --with-mssql=/usr/local/freetds/
# make && make install

cd /soft/php-5.6.28/ext/pdo_dblib/
linux下用phpize给PHP动态添加扩展
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-dblib=/usr/local/freetds/
# make && make install

whereis php查看命令路径 ,whereis需要你自己安装

[root@test software]# whereis php
php: /usr/bin/php /etc/php.ini /etc/php.d /usr/lib64/php /usr/include/php /usr/share/php

[root@test software]# whereis php-config
php-config: /usr/bin/php-config /usr/share/man/man1/php-config.1.gz

注意,yum版本扩展

/usr/bin/phpize
./configure --with-php-config=/usr/bin/php-config --with-pdo-dblib=/usr/local/freetds/

注意,yum在添加so的时候,必须

/etc/php.d/pdo.ini, 里面填在,打开phpinfo的时候搜索一下pdo

编译版不会有这个问题,pdo.so需要pdo_dblib之前,有依赖关系

extension=pdo.so
extension = pdo_dblib.so
extension = mssql.so

测试连接

error_reporting(E_ALL);
header("Content-type: text/html; charset=utf8");

$serverName = "172.17.1.250:1433"; //数据库服务器地址
$uid = "sa"; //数据库用户名
$pwd = ""; //数据库密码
$dbname = '';


$conn = new PDO("dblib:host=$serverName;dbname=$dbname", "$uid", "$pwd");

if ($conn == false) {
    echo "连接失败!";
//    die(print_r(sqlsrv_errors(), true));
} else {
    echo "连接成功!";
}

之前怀疑是windows的时候,出现http 429  Too Many Attempts.一开始认为是windows2008的并发数少,就换了linux做接口访问数据库,修改了sql server 2008 属性 连接的最大连接数,和windows2008的连接数

从widnows2008的nginx到apache,怕是nginx在windows下并发差,到linux pdo访问接口,依然还有这个问题,最后折腾了差不多一天发现是laravel的一个中间件的问题,

Laravel从5.2开始,增加了一个Throttle的中间件。如果你仔细看一下Kernel.php文件,你就会发现,api路由是默认使用了这个中间件的。

如果你需要使用就直接添加

Route::group(['domain' => ''], function() {
    Route::group(['namespace' => 'Data', 'prefix' => 'data','middleware'=>'throttle'], function () {

        //home
        Route::get('/', 'IndexController@index');                       //国内宏观
        Route::get('/test', 'TestController@test'); //测试地址
       

    });
});

一把双刃剑,最后发现每次到60次左右的访问就会出现,google才出来的,哎,百度半天不出结果

对laravel源码研究的少,太忙了,有时间读一下源码

猜你喜欢

转载自www.cnblogs.com/zx-admin/p/9209894.html
今日推荐