os : CentOS 7.x
PHP : 7.4 / 7.3
1. odbc install
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#RedHat Enterprise Server 6 curl https://packages.microsoft.com/config/rhel/6/prod.repo > /etc/yum.repos.d/mssql-release.repo #RedHat Enterprise Server 7 curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo #RedHat Enterprise Server 8 and Oracle Linux 8 curl https://packages.microsoft.com/config/rhel/8/prod.repo > /etc/yum.repos.d/mssql-release.repo [root@xinet ~]# yum remove unixODBC-utf16 unixODBC-utf16-devel #to avoid conflicts [root@xinet ~]# yum install msodbcsql17 # optional: for bcp and sqlcmd [root@xinet ~]# yum install mssql-tools [root@xinet ~]# echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile [root@xinet ~]# echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc [root@xinet ~]# source ~/.bashrc # optional: for unixODBC development headers [root@xinet ~]# yum install unixODBC-devel |
2. sqlsrv download & install
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[root@xinet ~]# wget https://pecl.php.net/get/sqlsrv-5.8.0.tgz [root@xinet ~]# tar xvf sqlsrv-5.8.0.tgz [root@xinet ~]# cd sqlsrv-5.8.0 [root@xinet sqlsrv-5.8.0]# /usr/local/php/bin/phpize [root@xinet sqlsrv-5.8.0]# ./configure --with-php-config=/usr/local/php/bin/php-config [root@xinet sqlsrv-5.8.0]# make [root@xinet sqlsrv-5.8.0]# make install [root@xinet sqlsrv-5.8.0]# ls -l /usr/local/php/lib/php/extensions/debug-zts-20180731/ -rwxr-xr-x 1 root root 1516440 8월 19 2019 opcache.so |
2-1 pdo_sqlsrv install
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
[root@xinet ~]# wget https://pecl.php.net/get/pdo_sqlsrv-5.8.0.tgz [root@xinet ~]# tar xvf pdo_sqlsrv-5.8.0.tgz [root@xinet ~]# cd pdo_sqlsrv-5.8.0 [root@xinet pdo_sqlsrv-5.8.0]# /usr/local/php/bin/phpize [root@xinet pdo_sqlsrv-5.8.0]# ./configure --with-php-config=/usr/local/php/bin/php-config [root@xinet pdo_sqlsrv-5.8.0]# make [root@xinet pdo_sqlsrv-5.8.0]# make install [root@xinet pdo_sqlsrv-5.8.0]# ls -l /usr/local/php/lib/php/extensions/debug-zts-20180731/ -rwxr-xr-x 1 root root 3093384 10월 16 17:11 pdo_sqlsrv.so -rwxr-xr-x 1 root root 3013752 10월 16 15:00 sqlsrv.so [root@xinet pdo_sqlsrv-5.8.0]# vi /usr/local/apache/conf/php.ini |
3. 웹서버 재시작
1 |
[root@xinet sqlsrv-5.8.0]# systemctl restart httpd |
5. ms-sql 연동 확인
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<?php $serverName = "192.168.0.10"; $connectionOptions = array( "database" => "xinet", "uid" => "xinet", "pwd" => "xinet12&*(" ); // Establishes the connection $conn = sqlsrv_connect($serverName, $connectionOptions); if ($conn === false) { die(formatErrors(sqlsrv_errors())); } // Select Query $tsql = "SELECT @@Version AS SQL_VERSION"; // Executes the query $stmt = sqlsrv_query($conn, $tsql); // Error handling if ($stmt === false) { die(formatErrors(sqlsrv_errors())); } ?> <h1> Results : </h1> <?php while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { echo $row['SQL_VERSION'] . PHP_EOL; } sqlsrv_free_stmt($stmt); sqlsrv_close($conn); function formatErrors($errors) { // Display errors echo "Error information: <br/>"; foreach ($errors as $error) { echo "SQLSTATE: ". $error['SQLSTATE'] . "<br/>"; echo "Code: ". $error['code'] . "<br/>"; echo "Message: ". $error['message'] . "<br/>"; } } ?> |