> 前序:遇到一个非常老的 sql server 2000 数据库,做数据转移。就想直接用 laravel 调用。
## 工具箱
>* 操作系统:CentOS 7.2
>* 运行环境: 宝塔 5.9
>* PHP版本: 7.2 (选安装 pdo_odbc 扩展)
>* Laravel 版本: 5.7
>* SQL Server 版本: 2000 (需要安装 sp4 补丁)
>* FreeTDS 版本: v0.95.81
>* UnixODBC 版本: 2.3.1
## 一、宝塔 5.9 环境安装:
#### 1、安装: 不做详解 [传送门](https://www.bt.cn/bbs/thread-1186-1-1.html)
#### 2、选择安装需要 php 版本等 如图:
![file](https://iocaffcdn.phphub.org/uploads/images/201809/19/19816/9R0i943yOU.png?imageView2/2/w/1240/h/0)
## 二、安装 UnixODBC FreeTDS
#### 1、UnixODBC 相关安装 具体参考:[传送门](https://blog.csdn.net/lgq421033770/article/details/51123803)
```
yum -y install unixODBC-*
```
#### 2、FreeTDS 相关安装 具体参考:[传送门](https://blog.csdn.net/xhu_eternalcc/article/details/12576907)
```
yum install freetds
```
![file](https://iocaffcdn.phphub.org/uploads/images/201809/19/19816/G31QjCkWyh.png?imageView2/2/w/1240/h/0)
#### 3、UnixODBC 加载 FreeTDS 驱动
查看 /usr/lib64 里面是否有 libtdsodbc.so 驱动文件 如果没有请重命名 命令如下
```
cd /usr/lib64
mv libtdsodbc.so.0.0.0 libtdsodbc.so
```
修改 /etc/odbcinst.ini 添加 FreeTDS 驱动
```
vi /etc/odbcinst.ini
```
最后面添加下面代码
```
[FreeTDS]
Description = ODBC for FreeTDS
Driver64 = /usr/lib64/libtdsodbc.so
Setup64 = /usr/lib64/libtdsodbc.so
FileUsage = 1
```
:wq! 保存退出
#### 4、FreeTDS 关联远程 SQL Server 2000 数据库
```
vi /etc/freetds.conf
```
```
[SqlServer20]
host = 192.168.0.133
port = 1433
tds version = 8.0
client charset = UTF-8
```
## 三、PHP7.2 pdo_odbc 扩展安装
进入 ssh 命令行
具体参考: [传送门](https://blog.csdn.net/lgq421033770/article/details/51123803)
#### 1、编译安装
```
wget http://cn2.php.net/distributions/php-7.2.10.tar.gz
tar xzf php-7.2.10.tar.gz
cd php-7.2.10/ext/pdo_odbc
/www/server/php/72/bin/phpize
./configure --with-php-config=/www/server/php/72/bin/php-config --with-pdo-odbc=unixODBC,/usr
make
make install
```
![file](https://iocaffcdn.phphub.org/uploads/images/201809/19/19816/RBVEpDOVqv.png?imageView2/2/w/1240/h/0)
#### 2、php引用 pdo_odbc 扩展驱动
修改 php.ini 文件
![file](https://iocaffcdn.phphub.org/uploads/images/201809/19/19816/JNYDDi4YMJ.png?imageView2/2/w/1240/h/0)
php.ini 文件最后面添加下面代码
其中 no-debug-non-zts-20170718 文件夹需要对应 make install 命令后的返回地址
```
extension = /www/server/php/72/lib/php/extensions/no-debug-non-zts-20170718/pdo_odbc.so
```
#### 3、重启 PHP
![file](https://iocaffcdn.phphub.org/uploads/images/201809/19/19816/7Qr9D0cK5b.png?imageView2/2/w/1240/h/0)
#### 4、查看 phpinfo odbc 是否支持
![file](https://iocaffcdn.phphub.org/uploads/images/201809/19/19816/TTxZqRlIMv.png?imageView2/2/w/1240/h/0)
PDO drivers 包含 ODBC 证明已经开启
![file](https://iocaffcdn.phphub.org/uploads/images/201809/19/19816/oz9hO9eTqj.png?imageView2/2/w/1240/h/0)
## 四、创建网站 安装 Laravel 5.7
#### 1、通过宝塔创建网站(不做详解)
![file](https://iocaffcdn.phphub.org/uploads/images/201809/19/19816/UOxRZkiMsj.png?imageView2/2/w/1240/h/0)
#### 2、删除禁用函数 proc_open
![file](https://iocaffcdn.phphub.org/uploads/images/201809/19/19816/KGDsXsyPjX.png?imageView2/2/w/1240/h/0)
#### 3、升级 composer
```
composer self-update
```
#### 4、使用 composer 国内镜像
```
composer config -g repo.packagist composer https://packagist.laravel-china.org
```
#### 5、安装最新版 laravel 5.7
```
cd /www/wwwroot/laravel.test
composer create-project --prefer-dist laravel/laravel ./
```
#### 6、修改网站运行目录 laravel 对应 public 目录
![file](https://iocaffcdn.phphub.org/uploads/images/201809/19/19816/2KtazS7P3I.png?imageView2/2/w/1240/h/0)
#### 7、修改目录权限为 755 所有者 www
![file](https://iocaffcdn.phphub.org/uploads/images/201809/19/19816/JARyh0txHK.png?imageView2/2/w/1240/h/0)
## 五、安装 tck/odbc ODBC larave 扩展包
具体参考:[传送门](https://packagist.org/packages/tck/odbc)
```
composer require tck/odbc
```
#### 1、修改 .evc 文件 最后添加一下内容
Servername 对应 freetds.conf 配置
```
ODBC_DB_DSN="odbc:Driver={FreeTDS};Servername=SqlServer20;Database=laravel"
ODBC_DB_USERNAME=sa
ODBC_DB_PASSWORD=
```
![file](https://iocaffcdn.phphub.org/uploads/images/201809/19/19816/Opedp7u7SB.png?imageView2/2/w/1240/h/0)
#### 2、引入 tck/odbc 扩展包
修改 config/app.php Service Provider
```
/*
* Package Service Providers...
*/
TCK\Odbc\OdbcServiceProvider::class,
```
#### 3、修改 config/database.php
connections 内添加
```
'odbc' => [
'driver' => 'odbc',
'dsn' => env('ODBC_DB_DSN', 'odbc:DB_CONNECTION_STRING'), //
'host' => env('ODBC_DB_HOST', 'localhost'),
'database' => env('ODBC_DB_DATABASE', 'forge'),
'username' => env('ODBC_DB_USERNAME', 'forge'),
'password' => env('ODBC_DB_PASSWORD', ''),
'charset' => 'utf8',
],
```
## 六、测试 SQL Server 20000 通信
### 1、laravel 篇
#### 1⃣️修改 .env 配置文件
```
DB_CONNECTION=odbc
```
#### 2⃣️修改 routes/web.php 代码测试
```
use Illuminate\Support\Facades\DB;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
$users = DB::select('select * from users where id = 1');
dd($users);
Route::get('/', function () {
return view('welcome');
});
```
### 2、访问 laravel.test
保证数据库存在 users 表 并且存在 id 1这条数据
[图片]![file](https://iocaffcdn.phphub.org/uploads/images/201809/19/19816/pgVxGozrB3.png?imageView2/2/w/1240/h/0)
> 如果正常返回数据。到此结束。
报错
![file](https://iocaffcdn.phphub.org/uploads/images/201809/19/19816/bSMzrZ7DyK.png?imageView2/2/w/1240/h/0)
> 如果报错 请继续阅读 3、sql server 2000 篇
### 3、SQL Server 2000 篇
上面报错原因分析:
#### 1⃣️ SQL Server服务是否正常开启
#### 2⃣️ 服务器防火墙 1433 端口是否开启
#### 3⃣️ SQL Server 2000 打 sp4 补订
[下载 sp4 补订](https://www.microsoft.com/en-us/download/details.aspx?id=18290)
查询 SQL Server 2000 版本
```
SELECT @@VERSION
```
显示结果如下:
```
Microsoft SQL Server 2000 - 8.00.2039 (Intel X86) May 3 2005 23:18:38 Copyright (c) 1988-2003 Microsoft Corporation Personal Edition on Windows NT 5.1 (Build 2600: Service Pack 2)
```
![file](https://iocaffcdn.phphub.org/uploads/images/201809/20/19816/92r3pKl99T.png?imageView2/2/w/1240/h/0)
至此 SQL Server 2000 sp4 补丁安装完成