Connecting to remote SQL Server through PHP on CentOS 7 - php

I have followed this solution for what seems to be the exact same problem, however I am not having as much success as I had hoped.
I set up the required config files for PDO_ODBC, unixODBC and FreeTDS packages:
First by specifiying the host in /etc/freetds.conf :
[mssql]
host = DBHost
port = 1433
tds version = 7.3
Then by specifying the FreeTDS driver location in /etc/odbcinst.ini :
[freetds]
Description = Ms SQL database access with Free TDS
Driver64 = /usr/lib64/libtdsodbc.so.0
Setup64 = /usr/lib64/libtdsS.so.2
FileUsage = 1
UsageCount = 1
Then by specifying the DSN in /etc/odbc.ini :
[mssql]
Description = mssql server
Driver = FreeTDS
Database = CET_PhonesDB
ServerName = mssql
TDS_Version = 7.3
Finally here is my PHP:
try {
$pdo = new PDO('odbc:mssql', 'dbuser', 'dbpass');
}
catch(Exception $e){
echo $e->getMessage();
}
which returns: "SQLSTATE[08S01] SQLConnect: 20009 [unixODBC][FreeTDS][SQL Server]Unable to connect: Adaptive Server is unavailable or does not exist".
I've tried troubleshooting using $tsql -S mssql -U dbuser -P dbpass (which tests the FreeTDS) and $isql mssql dbuser dbpass (Which tests the unixODBC) both of which connect successfully. This leads me to believe that the problem is with PDO_ODBC or something else in the PHP configuration. Any help is greatly appreciated :)

So I narrowed the problem down to the Apache server, by running my PHP script through the command line: $php /var/www/html/repo/index.php and the connection was made succesfully.
A coworker suggested that SELinux could be interfering with httpd, and sure enough running $setenforce 0 allowed my PHP to run properly when requesting them through a web browser.
Since disabling SELinux permenantly is a VERY BAD idea, I did some fiddling and digging and thanks to this manual I found that $sudo setsebool -P httpd_can_network_connect 1 allows scripts that the Apache runs to create network connections of their own. And now SELinux should let any PHP script executed by httpd create network connections.

I had the same problem and solved it by using a custom port: port = XXXXX in freetds.conf. This port should be configured in SQL Server Configuration Mgnt
SQL Server Network Configuration -> TCP/IP (enabled) -> IP Addresses -> IPAll -> TCP Dynamic Ports
https://msdn.microsoft.com/en-us/library/ms177440.aspx

Related

configure php from Redhat to connect to MS SQL Server

I'm trying to deploy Moodle LMS on Redhat server with php56 and I want to connect it to another remote MS SQL server through freetds and php-mssql,How to configure them together to be able to connect the Moodle LMS to the database?
Assuming you installed all needed packages (php-mssql and freetds).
You have to add an entry in /etc/freetds.conf like this:
[mydb]
host = 10.0.0.199
port = 1433
tds version = 8.0
client charset = UTF-8
text size = 20971520
Restart httpd (service httpd restart)
Then in moodle set db name to mydb and everything will work.
It could be that you will have to set/disable selinux for moodle. do this temporarily in this way :
setenforce 0
Then edit /etc/selinuc/config for next boot.
Good Luck!

I am unable to use MySQL due to a "refused connection"

I am trying to run the no-install version of MySQL on Windows XP located in C:/mysql/ on my machine.
The error message I receive within a PHP file when I try to connect to MySQL reads.
Could not connect: No connection could be made because the target machine actively refused it
I have edited the config file my.ini and altered/added the following.
[client]
port = 3306
socket = C:/mysql/tmp/mysql.sock
[mysqld]
port = 3306
socket = C:/mysql/tmp/mysql.sock
basedir = C:/mysql/
datadir = C:/mysql/data/
bind-address = 127.0.0.1
skip-networking
enable-named-pipe
When I run mysqld.exe I can confirm it is running within command prompt tasklist.
However when I try cmd netstat -a I cannot see any port usage at 3306.
Inside MySQL error logs, it reads.
140411 13:30:34 [Note] c:\mysql\bin\mysqld.exe: ready for connections.
Version: '5.5.37-log' socket: '' port: 0 MySQL Community Server (GPL)
The port: 0 doesn't look correct to me, but I am not sure.
When I run mysql.exe direct in cmd I get the following returned
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)
Some things I have tried already.
Placing a copy of my.ini in C:/WINDOWS and also a copy within C:/mysql
Changing bind-address = 127.0.0.1 to bind-address =
localhost
Inside PHP file MySQL connection variants, localhost, 127.0.0.1, 127.0.0.1:3306 inside basic mysql_connect()
Created tmp/mysql.sock file (in case dir + file not existing matters)
Commented out skip-networking in my.ini
After running mysqld.exe whilst running ran mysqladmin.exe in cmd which returns 'Can't connect to MySQL server at 'localhost'
Running telnet localhost 3306 returns
'Connecting To localhost...Could not open connection to the host, on port 3306: C
onnect failed'
Commented out bind-address in my.ini to allow default settings.
Ran mysqld.exe with user root (-u root) also inserted user = root in my.ini
Set Windows Firewall to off.
Thanks in advance.
Ok. You have binded your server to specific ip so please read this MySQL bind-addres. Please unbind your server and check if you have proper user. (last paragraph in documentation)

Unable to connect to data source

I try to connect to my batabase by means of php + adodb.
This is my php code:
$kpp_db = ADONewConnection('odbc_mssql');
$kpp_db->debug = true;
$kpp_db->charSet="UTF-8";
$kpp_dbDSN = "Driver={SQL Server};Server=$kppConfig_dbHost;Database=$kppConfig_dbName;";
$kppConfig_akEnabled=$kpp_db->Connect($kppConfig_dbDSN, $kppConfig_dbLogin, $kppConfig_dbPassword);
But it's not work. I get the message
S1000: [unixODBC][FreeTDS][SQL Server]Unable to connect to data source
This is my /etc/odbc.ini file:
[SQL Server]
Driver=FreeTDS
Description=testsql
Trace=Yes
TraceFile=~/workspace/mstest.log
And /etc/odbcinst.ini file:
[FreeTDS]
Description = FreeTDS unixODBC Driver
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
UsageCount = 1
I confused. What is wrong?
In redhat, but not sure for other linux OS.
You also have to allow apache/httpd to communicate with the MSSQL, Just type the command below, then restart httpd:
setsebool -P httpd_can_network_connect on
setsebool -P httpd_can_network_connect_db on
systemctl restart httpd
Then try to connected.
Have you tried connecting through the command line like described here ? If both tsql and isql work, then your configuration files are not the problem. Depending on the OS you're using, this might be a permission issue. For me, disabling SELinux did the trick:
sudo setenforce 0

Connecting to MSSQL server using LAMP

I have a LAMP server (Ubuntu 12.04, Apache/2.2.22, MySQL 5.5.24, PHP 5.3.10).
However I also want to connect to other MsSQL server on other machine using Apache & PHP
with a credentials of:
Server name: HENYO-GP-TEST\HENYO_GPTEST
IP: 192.168.1.36
Username: someuser
Password: somepass
Database: somedb
I already installed FreeDTS using 'apt-get install freetds-common freetds-bin unixodbc php5-sybase', then configured it by 'vim /etc/freetds/freetds.conf' then edit the bottom part like this
# A typical Microsoft server
[192.168.1.36]
host = 192.168.1.36
port = 1433
tds version = 8.0
but upon testing using 'tsql -S 192.168.1.36 -U someuser -P somepass -D somedb', the following error occured.
locale is "en_PH.UTF-8"
locale charset is "UTF-8"
using default charset is "UTF-8"
Default database being set to FAC
Error 20009 (severity 9):
Unable to connect: Adaptive Server is unavailable or does not exist
OS error 111, "Connection refused"
There was a problem connecting to the server
But I know the server is on because I can ping it.
Is the connection to MSSQL server (reside on other machine) possible using LAMP?
If it is possible? What are my mistakes on the installation and configuration? Thanks in advance.
Pinging a server doesn't necessarily imply that a particular port is open; it may respond to ICMP requests, but there's no guarantee that a given port is open. Have you confirmed that port 1433 is open on the remote server and that there is nothing blocking traffic to that port?
Your SQL Server is running on an instance named HENYO_GPTEST (HENYO-GP-TEST is the server name, and HENYO_GPTEST is the instance name), but you are not specifying this instance name anywhere.
I'm not familiar with FreeTDS, so I don't know how to specify that, but you need to specify this instance name in order to connect to the server.

mysqli connect problem

This is weird. I have 2 centos boxes, prod (192.168.0.1) and vm (192.168.0.30).
Mysql database sits on prod.
App sits on vm.
From vm, if I type
mysql -u user -p -h 192.168.0.1 -D mydb
it connects lovely, so port is open and listening on prod
but in app, i do
$db=new mysqli('192.168.0.1','user','mypass','mydb');
and I get
Warning: mysqli::mysqli() [function.mysqli-mysqli]: (HY000/2003): Can't connect to
MySQL server on '192.168.0.1' (13) in /var/www/vhosts/myapp/httpdocs/dstest.php
on line 123
Both boxes have exactly same versions php, mysql, mysql.so, mysqli.so
Any advice?
P.S. This also happens if I try $db=new mysqli('127.0.0.1',... BUT NOT if I try $db=new mysqli('localhost',...
in case it helps, here is my.cnf on prod:
[mysqld]
set-variable=local-infile=0
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
max_connections = 500
max_user_connections = 150
key_buffer = 2048M
query_cache_limit=4M
query_cache_size=64M
table_cache=2048
tmp_table_size=64M
max_heap_table_size = 256M
# users buffers
sort_buffer_size=2M
read_buffer_size=2M
read_rnd_buffer_size=1M
max_allowed_packet=16M
thread_cache=16
thread_concurrency=8
thread_cache_size=128
innodb_buffer_pool_size = 128M
myisam_sort_buffer_size = 128M
wait_timeout = 240
interactive_timeout = 240
max_allowed_packet=32M
[mysql.server]
user=mysql
basedir=/var/lib
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
Check here. It looks very similar to your issue.
Edit: added blog content for posterity:
SQLSTATE[HY000] [2003] Can’t connect to MySQL server on
‘xxx.xxx.xxx.xxx’ (13)
One of the things I wish I had found in Google faster when trying to
figure out why PHP script refuses to connect to remote MySQL server
issuing an error SQLSTATE[HY000][2003]Can't connect to MySQL server on
'xxx.xxxx.xxx.xxx' (13)
I am able to connect from local shell, so first thought was if
something wrong with recent Zend Framework upgrade, but after a while
I figured out that answer is very simple – SELinux was blocking remote
connections from PHP scripts executed by Apache web server. The error
code (13) at the end of error message means “permission denied”, so
that’s the indication to see if you have similar issue or not.
Anyway, login as root and do setsebool -P httpd_can_network_connect=1
to make it work.
Of course, think twice because you make web server a bit less secure,
so don’t do that unless you are sure you need it.
If that server is local, then try this first :
$db=new mysqli('localhost','user','mypass','mydb');
instead of
$db=new mysqli('192.168.0.1','user','mypass','mydb');
This worked for me.
restart mysql service via command line by running the below command:
service mysql restart

Categories