Issue
- How to install Microsoft ODBC driver?
- How-to establish a connection with MSSQL?
Environment
- CloudLinux
Solution
There are two ways to establish a connection with MSSQL:
DBLIB
-
Configure alt-freeTDS:
Add to /opt/alt/freetds11/etc/freetds.conf the following details about MSSQL:
[my_mssql]
host = %IP%
port = %PORT% -
Enable module for user pdo_dblib:
# selectorctl -u %USERNAME% -v %PHP_VERSION% -e pdo_dblib
-
Use the following code to establish connection with MSSQL:
$dbh = new PDO("dblib:host=my_mssql;dbname=%DB_NAME%", "%DB_USER%", P.S: "%DB_PASS%");
Alternative way (without updating /opt/alt/freetds11/etc/freetds.conf) is to use the full connection string
$dbh = new PDO("dblib:host=%IP%;port=%PORT%;dbname=%DB_NAME%", "%DB_USER%", "%DB_PASS%");
Configure ODBC
-
In /etc/odbcinst.ini add FreeTDS driver details:
[FreeTDS]
Description = MS SQL database access with Free TDS
Driver = /opt/alt/freetds/usr/lib64/libtdsodbc.so
Setup = /usr/lib64/libtdsS.so
UsageCount = 1Note: For i386/i686 use "lib" instead of "lib64" in paths
-
In /etc/odbc.ini add details about MSSQL server:
[mssql_odbc]
By adding a new section like this to your odbc.ini file, you can configure connections to multiple databases on the same server.
Description = MSSQL Server
Driver = FreeTDS //should have the same name as driver in /etc/odbcinst.ini
Database = %DB_NAME%
Server = %IP%
Port = %PORT%
Each section should contain the relevant connection details for each database you want to connect to. -
Update CageFS:
# cagefsctl --update-etc
-
Enable PHP extensions:
# selectorctl -u %USERNAME% -v %PHP_VERSION% -e pdo_odbc,odbc
The following should be added into the connection script:
$dbh = new PDO("odbc:mssql_odbc", "sa", "%DB_USER%", "%DB_PASS%");
-
Perform the following:
# /opt/alt/alt-php-config/install_odbc
# cagefsctl --force-update
# cagefsctl -M - Check if
/etc/odbcinst.ini
is available under the user:
In such a case, please add the related package# su -s /bin/bash - username
Last login: Sat Apr 6 16:39:52 +04 2024 on pts/1
[~]$ ls -la /etc/odbcinst.ini
ls: cannot access '/etc/odbcinst.ini': No such file or directoryunixODBC
to the CageFS:
# cagefsctl --addrpm unixODBC
# cagefsctl --force-update
# cagefsctl -M
Comments
0 comments
Please sign in to leave a comment.