PHP timeout when connecting to mssql under apache - php

PHP Version 5.4,
Apache Version 2.4,
CentOS 7.3
Trying to connect to mssql db using the following php code which works fine when I run it from the command line. However, when I place code under apache (on same server) and call through browser I get a timeout error.
Error: Connection failed: SQLSTATE[HYT00] SQLConnect: 0
[unixODBC][Microsoft][ODBC Driver 13 for SQL Server]Login timeout
expired+
set_time_limit(30);
echo "+ Connection\n";
try {
$pdo = new PDO("odbc:sqlsrv_msodbc", "username", "password");
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}

I just had the same issue. I am using CentOS 7.3 in vagrant and solved it by disabling SELinux.
If you're on a dev environment and disabling SELinux is cool you can do it by:
sudo vi /etc/sysconfig/selinux
Set
SELINUX=disabled
I found this over at serverfault: https://serverfault.com/questions/240015/how-do-i-allow-mysql-connections-through-selinux

Related

PHP connection to SQL Server 2008

I need to connect from PHP to an SQL Server 2008 instance. The Webserver is Apache 2.4 on a Centos 8 with PHP 7.4, php-sqlsrv extension, and I am using PDO to handle the connection.
First I tested the connection from cli, and I got the following error:
Connection failed: SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]SSL Provider: [error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol]
A little bit of googling sent me to a direction toward openssl.cnf, where I set the minimum version to TLS 1.0 (I know it's not secure, but it seemed to be working), and then connection worked, but only in cli, when I try it from the site, it still gives the same error.
The code I use for the connection is this (I changed the connection data):
try {
$conn = new PDO("sqlsrv:Server=database.local,1433;Database=database", "user", "password");
}
catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Can you help me figure out what I missed?
Thank you
update-crypto-policies --set LEGACY

PHP PDO + PostgreSQL Connection Error: Could not find Driver

There are a few similar questions that I have read through and followed the advice but to no end, and only being fairly new to this, I figured now was a good time to stop 'trying' things in case I break anything any further.
I'm receiving the following error when trying to connect to my database via PDO:
Connection error: could not find driver
1. I've ensured that Apache is linked to my homebrew PHP.
$ which php
/usr/local/bin/php
2. Have uncommented the following in php.ini
extension=php_pdo_pgsql.dll
The pdo_pgsql module shows up in php_info
3. My database connection is:
<?php
class Database
{
public $conn;
public function getConnection()
{
try {
$this->conn = new PDO("postgres://$user:$pass#$host:$port/$db_name");
print_r($this->conn);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->conn->exec("set names utf8");
} catch (PDOException $exception) {
echo "Connection error: " . $exception->getMessage();
exit();
}
return $this->conn;
}
}
I've triple checked these details and they are correct (albeit ommitted). I can connect with the same URI via IntelliJ's Database connection Wizard
4. I’ve edited /usr/local/var/postgres/postgresql.conf to include:
#listen_addresses = '*'
I'm still not having any luck and am looking at some guidance in this head scratcher.
As I see you are using Linux, but tried enable .dll library which is used for Windows machines. It makes sense to comment this line.
Make sure that you have pdo_pgsql module enabled:
# php -m | grep pdo_pgsql
pdo_pgsql
Install it if it is not
# yum install php-pgsql
Here is steps that I did to make PHP+PostgreSQL work on my clean CentOS 7 install:
Install PostgreSQL (but I think you already have this installed and
configured)
# yum install postgresql-server postgresql-contrib
Updated config /var/lib/pgsql/data/pg_hba.conf, changed from ident to md5
host all all 127.0.0.1/32 ident
host all all ::1/128 ident
After
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
Restart postgresql service
# service postgresql restart
Install PHP and PDO connector
# yum install php php-pgsql
Here is an example of PHP script I used to test connection:
<?php
// Configure DB Parameters
$host = "localhost";
$dbname = "masterdb";
$dbuser = "automation";
$userpass = "fGmK4hvDZPB6fr6c";
$dsn = "pgsql:host=$host;port=5432;dbname=$dbname;user=$dbuser;password=$userpass";
try{
// create a PostgreSQL database connection
$conn = new PDO($dsn);
// display a message if connected to the PostgreSQL successfully
if($conn){
echo "Connected to the $dbname database successfully!";
echo "\n";
}
}catch (PDOException $e){
// report error message
echo $e->getMessage();
}
And the output:
# php pdo_test.php
Connected to the masterdb database successfully!

Using PDO to connect to Access database (.accdb)

I am having difficulties with connecting to an Access database (specifically an Access 2013 database with an .accdb extension). Here is the code I'm trying to run:
$dbName = $_SERVER["DOCUMENT_ROOT"] . "/test/testdb.accdb";
echo $dbName."<br />";
if (!file_exists($dbName)) {
die("Could not find database file.<br />".$dbName);
}
try {
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};;Dbq=$dbName");
} catch(PDOException $e) {
echo "Error: ".$e->getMessage()."<br />";
}
I have made sure that the testdb file exists in the correct folder, but when I try to create the new PDO, I am getting the following error: "could not find driver".
Now, I have gone back to my ini settings and confirmed that I have the following extensions selected:
php_curl
php_gd2
php_mbstring
php_mssql
php_mysql
php_mysqli
php_pdo_mssql
php_pdo_mysql
php_pdo_sqlite
I am using WAMP version 2.4 with PHP 5.4.16 on a Windows 7 machine. I would appreciate any and all help I can get.
You'll have to install (if your distro has one) or compile the pdo-odbc generic driver: http://php.net/manual/en/ref.pdo-odbc.php

MSSQL VIA FreeTDS, ODBC, and Cpanel Unknown host machine name (severity 2)

I have installed FreeTDS 0.91, ODBC, on a Cpanel server running Centos 6.5x64. Everything appears to be running fine and I can connect to the remote MSSQL 2012 server using:
/usr/local/freetds/bin/tsql -S sqlserver -U test -P mypassword
and succesfully execute queries in the database.
I can also connect through:
isql -v sqlserverdatasource test mypasswordhere
But for some reason /usr/local/freetds/bin/tsql -LH server.ip.here
returns no information or errors which doesn't make much sense when it is proven I can connect with the other methods above.
So now when running a test script from a cpanel account on the machine I get:
Unknown host machine name (severity 2)
Here is the test script:
//Database connection function.
function getConnection() {
try {
//$dbconnect = new PDO("sqlserver:Server=server.ip.here,1433;Database=dbname", "user", "password");
$dbconnect = new PDO("dblib:host=server.ip.here,1433;dbname=dbname", 'user', 'password');
} catch (PDOException $e) {
echo "CONNECTION ERROR.<br>Error message:<br><br>" . $e->getMessage();
die();
}
if (!$dbconnect) {
die('Cant connect to database. Please try again later!');
}
else{
echo "i'm in!";
return $dbconnect;
}
}
The first commented line is the old one using sqlserv which I found did not work at all from what i can tell because of the x64 OS. I have also tried with "" around user and pass as well as no marks at all.
php -m does show PDO and pdo-dblib.
Any ideas where I can look next?
Update: This was fixed. I missed in freetds.conf:
[global]
# TDS protocol version
tds version = 8.0
It was originally set to 4.5 instead of 8.
The fix for me was with three steps:
First, I edited /etc/freetds/freetds.conf and changed the tds version like this:
tds version = 8.0
The second step was not entering port number. The port was already 1433, and not specifying it fixed the exact same issue on my case.
Lastly, to connect properly, I had to restart networking as #user1054844 mentioned as this:
/etc/init.d/networking restart
After all these steps, I was able to connect and work with the SQL Server database.
You actually did not need ODBC at all since your connect script is using pdo_dblib not odbc. You can just install FreeTDS than enable pdo_dblib via the compile time flag in rawopts and rebuild via EasyApache. Of course cPanel specifics for this are a bit different.
I just did this for a friend and decided to document it since it is hard to find accurate clear information for FreeTds and pdo_dblib on cPanel.
Guide is here: FreeTDS And pDO_dblib On cPanel

Want to connect PHP5 to MS SQL SERVER 2005 in linux

I want to connect mssql server using php code on linux operating system. i want to use mssql_connect() .please help me. my code is
$server='x.x.x.x\SQLEXPRESS';
$link = mssql_connect($server, 'username', 'password');
if (!$link)
{
die("Couldn't connect to SQL Server on $server. Error: " . mssql_get_last_message());
}
else
{
echo "Connected!";
}
it show error "Unable to connect to server: x.x.x.x\SQLEXPRESS"
For me, this worked under debian lenny:
apt-get install libsybdb5 freetds-common php5-sybase
/etc/init.d/apache2 restart
I found the solution here: http://docs.moodle.org/20/en/Installing_MSSQL_for_PHP

Categories