近期php 容器要訪問一臺(tái)遠(yuǎn)程服務(wù)器的sql server 數(shù)據(jù)庫(kù),中間遇到挺多問題的。記錄一下
install odbc/php extension
安裝操作SQL Server 需要的php擴(kuò)展和軟件驅(qū)動(dòng) ODBC丈咐,把安裝命令寫成shell script,在php 的Dcokerfile 里面執(zhí)行,文件目錄是:
-- docker-compose.yml
-- php
-- Dockerfile
-- sqlserver_extension.sh
把下面內(nèi)容放在 sqlserver_extension.sh 里面
# sqlserver
apt-get update && apt-get install -y gnupg2
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
#Download appropriate package for the OS version
#Choose only ONE of the following, corresponding to your OS version
#Debian 9
#curl https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql-release.list
#Debian 10
curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list
#Debian 11
#curl https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list
apt-get update
ACCEPT_EULA=Y apt-get install -y msodbcsql17
# optional: for bcp and sqlcmd
ACCEPT_EULA=Y apt-get install -y mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
#source ~/.bashrc
# optional: for unixODBC development headers
apt-get install -y unixodbc-dev
# optional: kerberos library for debian-slim distributions
apt-get install -y libgssapi-krb5-2
# sqlserver php extension
docker-php-source extract && cd /tmp \
&& curl -L -o /tmp/pdo_sqlsrv.tgz https://pecl.php.net/get/pdo_sqlsrv-5.9.0.tgz \
&& curl -L -o /tmp/sqlsrv.tgz https://pecl.php.net/get/sqlsrv-5.9.0.tgz \
&& tar zxvf pdo_sqlsrv.tgz && tar zxvf sqlsrv.tgz \
&& mv pdo_sqlsrv-5.9.0 /usr/src/php/ext/pdo_sqlsrv \
&& mv sqlsrv-5.9.0 /usr/src/php/ext/sqlsrv \
&& docker-php-ext-install pdo_sqlsrv && docker-php-ext-install sqlsrv
Dockerfile
FROM php:7.3.7-fpm
# install sqlserver odbc/php extension
COPY sqlserver_extension.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/sqlserver_extension.sh
RUN sh /usr/local/bin/sqlserver_extension.sh
如果命令執(zhí)行出錯(cuò),很可能是提示缺少其他依賴包捏肢。也可以進(jìn)入容器執(zhí)行單個(gè)命令,看是否出錯(cuò)饥侵。比如執(zhí)行“curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -” 時(shí)就提示報(bào)錯(cuò)了鸵赫,導(dǎo)致后面的msodbcsql17/mssql-tools 提示找不到包,無(wú)法安裝躏升。安裝
apt-get install -y gnupg2
后就可以了辩棒,要仔細(xì)耐心閱讀報(bào)錯(cuò)信息。
開放端口號(hào)膨疏,允許賬號(hào)使用ip遠(yuǎn)程登入
設(shè)置SQL Server 允許賬號(hào)ip 遠(yuǎn)程登入一睁,對(duì)方服務(wù)器是windows 的,設(shè)置方法可參看附錄佃却。設(shè)置好之后者吁,可現(xiàn)在本地驗(yàn)證:
# 看是否能否登入
sqlcmd -S 127.0.0.1(或者外網(wǎng)ip) -U ske -P
xxx
use skedb
go
select top 1 * from table order by time desc;
go
還要設(shè)置數(shù)據(jù)庫(kù)端口號(hào)對(duì) php 容器服務(wù)器開放,聯(lián)系學(xué)校網(wǎng)絡(luò)中心的老師解決了双霍⊙馀迹可以利用telnet 來(lái)驗(yàn)證是否開放,
telnet $ip 1433
還有一點(diǎn)要注意的是洒闸,數(shù)據(jù)庫(kù)端口號(hào)開放是針對(duì)特定IP的染坯,所以PHP容器的網(wǎng)絡(luò)模式要使用host,不能使用bridge 模式丘逸,即和宿主機(jī)共享網(wǎng)絡(luò)单鹿。docker-compose.yml 配置如下:
version: '2.2'
services:
php73:
build: ./php
container_name: php73
restart: always
tty: true
volumes:
- ./web:/var/www/html
- ./php/php.ini:/usr/local/etc/php/php.ini
- ./php/php-fpm.d:/usr/local/etc/php-fpm.d
- ./php/php-fpm.conf:/usr/local/etc/php-fpm.conf
- ./php/cron:/var/spool/cron/crontabs
- ./php/php_errors.log:/var/log/php_errors.log
- ./php/other_config/openssl.cnf:/etc/ssl/openssl.cnf # resolve sql server err
network_mode: host # host 是docker 預(yù)定義的網(wǎng)絡(luò),使用host 不能使用links option
other problem
ERR: SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]SSL Provider: [error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol]
解決方式:
vim /etc/ssl/openssl.cnf
# 末尾改為
[system_default_sect]
MinProtocol = TLSv1
CipherString = DEFAULT@SECLEVEL=1
參考文章: