Trên Ubuntu 18. 04 máy chủ chạy PHP 7. 2, tôi muốn cài đặt mô-đun Microsoft SQL Server cho PHP. Bạn có thể tìm thấy mô-đun PHP cho máy chủ SQL tại
https. //github. com/microsoft/msphpsql
Tại thời điểm viết mô-đun PHP này chỉ hỗ trợ PHP 7. 4 và mới hơn. VẬY, tôi cần tìm phiên bản cũ hơn hỗ trợ PHP 7. 2. Khi kiểm tra trang phát hành, tôi tìm thấy phiên bản 5. 8. 0 được hỗ trợ PHP 7. 2
Đầu tiên cài đặt php7. gói 2 dev với
1
apt cài đặt php7. 2 - dev
Cài đặt các mô-đun php với pcel
1
pecl cài đặt sqlsrv - 5. 8. 0
Trong quá trình cài đặt, tôi gặp lỗi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cấu hình. tạo . / config. trạng thái
cấu hình. trạng thái. tạo cấu hình. h
cấu hình. trạng thái. thực thi libtool lệnh
đang chạy. làm
/ bin / bash / tmp/pear/temp/pear-build-rootnuxjAy/sqlsrv-5.8.0 / libtool -- chế độ=compile g++ -std=c++11 -I. - Tôi / tmp / . 8. 0 pear/temp/sqlsrv -DPHP_ATOM_INC -I/tmp/pear/temp/pear-build-rootnuxjAy/sqlsrv-5.8.0 / bao gồm - I . 8. 0 tmp/pear/temp/pear-build-rootnuxjAy/sqlsrv-5.8.0 / chính - I . tmp/pear/temp/sqlsrv -I/usr/include/php/20170718 -I/usr/include/php/20170718/main -I/usr/include/php/20170718/TSRM -I/usr/include/php/20170718/Zend -I/usr/include/php/20170718/ext -I/usr/include/php/20170718/ext/date/lib -I/tmp/pear/temp/sqlsrv/shared/ -DHAVE_CONFIG_H -std=c++11 -D_FORTIFY_SOURCE=2 -O2 -fstack-protector -c /tmp/pear/temp/sqlsrv/conn.cpp - o conn. lo
libtool. biên dịch. g ++ - std . =c++11 -I. - Tôi / tmp / . 8. 0 pear/temp/sqlsrv -DPHP_ATOM_INC -I/tmp/pear/temp/pear-build-rootnuxjAy/sqlsrv-5.8.0 / bao gồm - I . 8. 0 tmp/pear/temp/pear-build-rootnuxjAy/sqlsrv-5.8.0 / chính - Tôi . tmp/pear/temp/sqlsrv -I/usr/include/php/20170718 -I/usr/include/php/20170718/main -I/usr/include/php/20170718/TSRM -I/usr/include/php/20170718/Zend -I/usr/include/php/20170718/ext -I/usr/include/php/20170718/ext/date/lib -I/tmp/pear/temp/sqlsrv/shared/ -DHAVE_CONFIG_H -std=c++11 -D_FORTIFY_SOURCE=2 -O2 -fstack-protector -c /tmp/pear/temp/sqlsrv/conn.cpp - fPIC -< . DPIC -o .lib / kết nối. o
Trong tệp được bao gồm từ / tmp/pear/temp/sqlsrv/shared/typedefs_for_linux.h. 23. 0,
từ / tmp / pear/temp/sqlsrv/shared/xplat_winnls.h. 24,
từ / tmp / pear/temp/sqlsrv/shared/FormattedPrint.h. 24,
từ / tmp / pear/temp/sqlsrv/shared/core_sqlsrv.h. 41,
từ / tmp / pear/temp/sqlsrv/php_sqlsrv_int.h. 25,
từ / tmp / pear/temp/sqlsrv/conn.cpp. 24.
/ tmp / lê / temp/sqlsrv/shared/xplat.h. 30. 10. lỗi nghiêm trọng. sql. h. Không như tệp hoặc thư mục
#include
^ ~~~~~~
quá trình biên dịch đã chấm dứt.
Tệp tạo. 194. công thức cho mục tiêu 'conn. lo' thất bại
làm. *** [kết nối. lo] Lỗi 1
LỖI. `thực hiện ' thất bại
gốc@máy chủ. ~#
Điều này được khắc phục bằng lệnh
1
apt - nhận cài đặt unixodbc-dev
cài đặt pdo_sqlsrv với
1
pecl cài đặt pdo_sqlsrv - 5. 8. 0
Chạy
1
2
printf "; priority=20\nextension=sqlsrv. vậy\n" > / vv / php/7.2 / các chế độ - sẵn có . sqlsrv.ini
printf "; priority=30\nextension=pdo_sqlsrv. vậy\n" > / vv / php/7.2 / các chế độ - sẵn có . pdo_sqlsrv.ini
Kích hoạt các mô-đun PHP với
1
phpenmod - v 7. 2 sqlsrv pdo_sqlsrv
Khởi động lại Apache, bây giờ phpinfo() hiển thị pdo_sqlsrv
Nhưng khi truy cập tập lệnh PHP kết nối với máy chủ MS SQL, tôi gặp lỗi
Tiện ích mở rộng này yêu cầu Trình điều khiển ODBC của Microsoft dành cho SQL Server để giao tiếp với SQL Server. Truy cập URL sau để tải xuống Trình điều khiển ODBC cho SQL Server cho x64
Để khắc phục điều này, hãy làm
1
cuộn https. //gói. Microsoft. com/phím/microsoft. Tăng dần. thêm khóa apt -
Đối với Ubuntu 16. 04
1
cuộn https. //gói. Microsoft. com/config/ubuntu/16. 04/sản phẩm. danh sách > /etc/apt/sources. danh sách. d/mssql-phát hành. danh sách
Đối với Ubuntu 18. 04
1
cuộn https. //gói. Microsoft. com/config/ubuntu/18. 04/sản phẩm. danh sách > /etc/apt/sources. danh sách. d/mssql-phát hành. danh sách
Đối với Ubuntu20. 04
1
cuộn https. //gói. Microsoft. com/config/ubuntu/20. 04/sản phẩm. danh sách > /etc/apt/sources. danh sách. d/mssql-phát hành. danh sách
Ubuntu 20. 10
1
cuộn https. //gói. Microsoft. com/config/ubuntu/20. 10/sản phẩm. danh sách > /etc/apt/sources. danh sách. d/mssql-phát hành. danh sách
Cập nhật bộ đệm apt
1
apt - nhận cập nhật
Cài đặt Microsoft ODBC
1
apt - nhận cài đặt - y msodbcsql17
Không bắt buộc. cho bcp và sqlcmd
1
2
3
4
apt - nhận cài đặt - y mssql-tools
echo 'export PATH="$PATH. /opt/mssql-tools/bin"' >> ~ / . .bashrc
nguồn ~ / . bashrc
apt - nhận cài đặt - y unixodbc-dev
Bây giờ mô-đun php MS SQL sẽ hoạt động. Bạn có thể tìm mã PHP mẫu tại
https. //ý chính. github. com/serverok/456b3d1d7295463df42c9822e8db3e5b
https. //github. com/microsoft/msphpsql/blob/master/sample/pdo_sqlsrv_sample. php
Đây là tài liệu của microsoft
https. // tài liệu. Microsoft. com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver15