php connect m$sql@m$server

某個情況下需要做這樣的連線。php版本是7.3, m$sql是2016放在win2012上。

先裝套件

apt install php-pear g++ make unixodbc-dev

sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
# 在這裡我遇到了不能下載的問題,因此我直接去官方抓
https://pecl.php.net/package-search.php?pkg_name=sqlsrv
sudo pecl install sqlsrv-5.9.0.tgz
pecl install pdo_sqlsrv-5.9.0.tgz

printf “; priority=20\nextension=sqlsrv.so\n” > /etc/php/7.3/mods-available/sqlsrv.ini
printf “; priority=30\nextension=pdo_sqlsrv.so\n” > /etc/php/7.3/mods-available/pdo_sqlsrv.ini
exit
sudo phpenmod -v 7.3 sqlsrv pdo_sqlsrv

# 參考下面安裝driver

https://docs.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?redirectedfrom=MSDN&view=sql-server-ver15

sudo su
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 8
curl https://packages.microsoft.com/config/debian/8/prod.list > /etc/apt/sources.list.d/mssql-release.list

#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

exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql17
# optional: for bcp and sqlcmd
sudo 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
sudo apt-get install -y unixodbc-dev
# optional: kerberos library for debian-slim distributions
sudo apt-get install -y libgssapi-krb5-2

sudo vim /var/www/html/test_connect_m$sql.php

<?php
    $serverName = "192.168.0.250\\MSSQLSERVER, 1433";
    $connectionOptions = array(
		"Database" => "database",
		"Uid" => "username",
		"PWD" => "password"
	);
	    //Establishes the connection
	$conn = sqlsrv_connect($serverName, $connectionOptions);
	if($conn){
		echo "Connected!";
	} else {
		echo "iConnection failed";
		die(print_r(sqlsrv_errors(), true));
	}

	sqlsrv_close($conn);	
?>

# 還是連不上,後來查到這個,改完就搞定了

https://owendavies.net/articles/docker-php-mssql-error/

Adding the following line to the d takes the SSL down to the 1024 bit RSA key

sed -i -E 's/(CipherString\s*=\s*DEFAULT@SECLEVEL=)2/\11/' /etc/ssl/openssl.cnf

發佈留言