Connecting to MSSQL server using LAMP - php

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.

Related

Connecting to remote SQL Server through PHP on CentOS 7

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

ERROR 2003 (HY000) while trying to connect to mysql remotely

I am trying to connect to mysql remotely. Because this is such a common question I read several tutorials/questions but I keep getting this error:
ERROR 2003 (HY000): Can't connect to MySQL server on 'the-ip-address' (60)
I edited the /etc/mysql/my.cnf and commented the line of bind_address like this:
#bind-address = 127.0.0.1
After that I ran:
$ sudo service mysql restart
with the output of:
mysql stop/waiting
mysql start/running, process 9853
mysql is running on default port: 3306
Using the user with which I am trying to connect remotely is working locally on the server (I can connect to the mysql from the server).
In phpmyadmin I configure these users:
user#localhost
user#127.0.0.1
user#87.45.34.23
Then I am trying to connect with the following:
mysql -u user -p -h 87.45.34.23
Maybe I am missing something... Thank you in advanced
UPDATE
As #Geoffrey suggested in the comments the problem was with a firewall. For this reason I will accept his answer although the answer itself is not for that but in the comments he was right.
Connection refused means the MySQL server is not listening or is firewalled.
By commenting out the bind line, MySQL doesn't bind to anything and only allows local socket access, you need to bind it to either the local IP, or all IPs by specifying 0.0.0.0
Also ensure that skip-networking is not set anywhere.

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!

Can't connect Websocket when using vagrant envirionment

I'm using Vagrant box using puphpet, the environment is PHP 5.5 + ubuntu 12.04 + apache + mysql. My Vagrant VM ip: 192.168.11.11, local machine hosts points to 192.168.11.11 reactphp.dev, and it works.
And, I'm using this code: https://github.com/muuknl/phprealtimechat to test websocket.
Then I visit reactphp.dev in my Chrome browser, and I start server script using: php bin/server.php, and after I type in the username I just got the error:
WebSocket connection to 'ws://192.168.11.11:2000/' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT
It works well when I changed the websocket ip to `ws://127.0.0.1:2000/', but why it does not work if I change to my VM's ip address?
It's been a while I haven't used PuPHPet, but when I had troubles connecting through the port (minus port 22), I've had to issue the command sudo ufw disable in the vagrant machine.
If uwf isn't the managing firewall of your vm, try sudo service iptables stop, or sudo iptables -F.
Perhaps they've changed the behavior since then, but it's still good to try.
The SSH tunnel (over the opened port 22) might allow you to access the local port 2000, which is proxified over. The actual port 2000 of the VM might be limited due to firewall.
Try it, and tell me if this works. If not, I'll dig in deeper.

Cannot connect to remote PostgreSQL server via PHP [duplicate]

This question already has an answer here:
Can't Connect to PostgreSQL with PHP pg_connect()
(1 answer)
Closed 8 years ago.
Connecting to a PostgreSQL database via a remote IP address, I have been successful via Windows using pgAdmin III, but I get errors whenever I try connecting from my local CentOS 6 Apache web server using the standard php-pgsql library using pg_connect().
Notes:
I am not in control of the remote server, but could inquire about additional info if needed.
My password does contain a special character (although it's not a quote character).
Creds are as follows:
Host (IP): xx.xx.xx.xx
Port: 5432
DBname: sandbox
Username: abc
Password: ***
MaintenanceDB: template1
This is the PHP code I have attempted to run in my local server:
pg_connect("host=xx.xx.xx.xx port=5432 dbname=sandbox user=abc password=***");
I've also tried:
pg_connect("host=xx.xx.xx.xx port=5432 dbname=template1 user=abc password=***");
When I attempt to connect, I receive the following error:
Warning: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Permission denied Is the server running on host "xx.xx.xx.xx" and accepting TCP/IP connections on port 5432?
I have added/uncommented extension=pgsql.so in the /etc/php.d/ directory and service httpd restart. I've even gone as far as opening my iptables ports as such so there should be no doubt about local firewall ports being blocked:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 5432 -j ACCEPT
Thanks in advance :-)
As per your postgresql connection problem, this solution may work. i just take this answer from php manual and i am not sure about it, so please see here for more http://php.net/manual/en/function.pg-connect.php#38291
You should try to leave the host= and port= parts out of the connection string. This sounds strange, but this is an "option" of Postgre. If you have not activated the TCP/IP port in postgresql.conf then postgresql doesn't accept any incoming requests from an TCP/IP port.

Categories