Can't Connect to PostgreSQL with PHP pg_connect() - php

EDIT: I just realized that this question may be better suited to ServerFault. Instead of copying it, a moderator please move it over? Thanks.
I've checked php-info, and the Postgresql extension is there (pg_connect() is not undefined). I am also able to connect to postgresql using psql on localhost (I've edited my pg_hba.conf file appropriately). Here is the code that's not working:
<?php
$dbconn = pg_connect("host=localhost port=5432 dbname=mydb user=myuser password=mypass") or die('Could not connect: ' . pg_last_error());
?>
This code simply results in "Could not connect: " being displayed in the browser.
I checked my apache log, and here's the relevant error message:
PHP Warning: pg_connect() [<a href='function.pg-connect'>function.pg-connect</a>]:
Unable to connect to PostgreSQL server: could not connect to server: Permission
denied\n\tIs the server running on host "localhost" and accepting\n\tTCP/IP
connections on port 5432?
How can I fix/debug this?
Edit: I'm on Centos 5.4 btw.

could not connect to server: Permission denied
Edit: I'm on Centos 5.4 btw.
Check /var/log/audit/audit.log. Chances are that you're hitting a SELinux rule.

Related

Unable to connect to Server: WIN-71KGFN2PPCC\SQLEXPRESS in C:\inetpub\wwwroot\index.php on line 7 Something went wrong while connecting to MSSQL

I have tested my MS SQL Server connection with this PHP code:
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$server = 'WIN-71KGFN2PPCC\SQLEXPRESS';
// Connect to MSSQL
$link = mssql_connect($server, 'sa', 'test1234');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}else{
}
?>
and had this warning and error message:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: WIN-71KGFN2PPCC\SQLEXPRESS in C:\inetpub\wwwroot\index.php on line 7
Something went wrong while connecting to MSSQL
I tried many solutions, I also enabled the TCP/IP protocol of my MS SQL Server from the SQL Configuration Manager, adding the port 1433 to it and to the firewall. Any other solution to it? Please help me.

Mongodb:Failed to connect

I am new to MongoDB.I installed mongodb and used with Laravel framework.Its had been worked for a long time without any issues.But currently while i try to acess my website it shows:
Failed to connect to: localhost:27017: Connection refused
when i try to acces mongodb via command line,it shows:
warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146 exception: connect failed
when i try to access it via rock tool,it shows:
Unable to connect MongoDB, please check your configurations. MongoDB said:Failed to connect to: 127.0.0.1:27017: Connection refused.
what i will do? I can't figure what's wrong from my part since its working after the past months.I do nothing since the last days.Thanks in advance..
After a short break, I can find out the solution.
The origin of that issue is the unexpected shutdown of the system.So i run the below code in command prompt:
sudo rm /var/lib/mongodb/mongod.lock
mongod --repair
sudo service mongodb start
then after the above code run, its working fine :)

Failed to connect to: localhost:27017: Connection refused

I have installed MongoDB in xampp on Ubuntu using Laravel 5.1. But I always get this error
MongoConnectionException in TestController.php line 19: Failed to
connect to: localhost:27017: Connection refused
Nor I'm able
I have gone through a lot of questions, but all proved to be trivial solution.
Please help me with this as I'm stuck on it.
I think that first thing to do is to ssh on that server and check that mongod process is alive, then try open mongo shell using command - mongo . If all works try telnet on this server and port from your machine ,if there is no errors , read mongo logs

Setting up MySQL issue under Eclipse PHP

I have a snippet of code under the PHP file in Eclipse PHP.
$username = mysql_real_escape_string( $username );
It generates an error Warning:
mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /web_development/php_work/new_test/test.php on line 37
Looks like the Eclipse PHP couldn't connect to the MySQL of XAMPP, so How do I fix that?
I added two lines
#mysql_connect('localhost','root', '') or die('Could not connect to : ' . mysql_error());
#mysql_select_db('test') or die( "-Unable to select database");
but Eclipse still throws me.
mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Can't connect to local MySQL server through socket '/tmp/mysql.sock'
thx.

No connection could be made using mysql_* API [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
I tried to connect mysql database.but am getting the following error.
Warning: mysql_connect(): [2002] No connection could be made because the target machine actively (trying to connect via tcp://localhost:3306) in test.php on line 5
Warning: mysql_connect(): No connection could be made because the target machine actively refused it. in test.php on line 5 Warning: mysql_close() expects parameter 1 to be resource, boolean given in test.php on line 15
test.php:
<?php
$link = mysql_connect(localhost, dbuser, dbpass);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
This happened to us when running a PHP & MySQL on IIS7 in Windows Server 2008 in VirtualBox. It turns out that the Windows hosts file didn't have an IPv4 entry for localhost (127.0.0.1) so the route to MySQL on localhost was not found.
We added the following to the hosts file which resolved it:
127.0.0.1 localhost # Additional IPv4 entry
::1 localhost # Existing IPv6 localhost entry
Reference: A brief description of PHP/MySQL Localhost via IPv4 & IPv6
The MySQL server isn't running, or you have a firewall blocking port 3306.
This error message means the system did not accept the TCP connection request.
In my.ini find string:
bind-address = 127.0.0.1
Change from localhost to network IP.
Got the same message trying to connect to 'localhost' by mistake (on a Windows server). After fixing the connection string to the correct (remote) server the error disappeared.

Categories