Linux TP5 远程连接SQL Server

系统:Linux CentOS7_x86_64

PHP版本:5.6.38

以下参考:https://moell.cn/article/6?tdsourcetag=s_pctim_aiomsg

一、安装unixODBC+Freetds

下载unixODBC+Freetds

# tar -zxv -f unixODBC-2.3.2.tar.gz

# ./configure --prefix=/usr/local/unixODBC

# make & make install

# tar -zxv -f freetds-0.91.112.0.0.tar.gz

# ./configure --prefix=/usr/local/freetds --with-tdsver=7.1 --enable-msdblib --enable-dbmfix --with-gnu-ld --with-unixodbc=/usr/local/unixODBC

# make & make install

二、安装pdo_dblib

pdo_dblib在php安装包的ext文件夹有,没有自行下载

# cd php-5.6.38/ext/pdo_dblib

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

# make & make install

三、修改Sqlsrv.php文件

修改thinkphp\library\think\db\connector下的Sqlsrv.php文件的parseDsn方法

    protected function parseDsn($config)
    {
//        $dsn = 'dblib:Database=' . $config['database'] . ';Server=' . $config['hostname'];
//        if (!empty($config['hostport'])) {
//            $dsn .= ',' . $config['hostport'];
//        }
        $dsn = 'dblib:host='.$config['hostname'].':'.$config['hostport'].';dbname='.$config['database'];
        return $dsn;
    }

四、测试连接

    $db   =  [
        // 数据库类型
        'type'      =>  'sqlsrv',
        // 服务器地址
        'hostname' => '192.168.1.100',
        // 数据库名
        'database' => 'Test',
        // 用户名
        'username' => 'Test',
        // 密码
        'password' => 'Test',
        // 端口
        'hostport' => '1433',
    ];

$data = Db::connect($db)->table('test')->select();

halt($data);

猜你喜欢

转载自blog.csdn.net/qq_39197402/article/details/84930187
今日推荐