Unable to connect an app to IIS server running postgresql - postgresql

I realise this will be a very vague post. We have a new server running Windows server. It turns out it was not setup fully.
It was missing the IIS webserver.
I have set up IIS, installed PHP7 using Web Platform Installer, and tested the connection through a psql PDO connection. We have an android app which communicates to the database via php, it uses a PDO connection. The app works when we point it to our old server 192.168.1.102 but it gives an error when we direct it to the new server 192.168.1.100
I've added these extensions to the php.ini file:
extension=php_pgsql.dll
extension=php_pdo_pgsql.dll
Here is the contents of the db connection which was setup by someone else a few years ago:
<?php
class PgDB {
private $dbConnection;
function __construct() {
$this->dbConnection = new PDO('pgsql:dbname=' . PG_DB .';host=' . PG_HOST . ';user=' . PG_USERNAME . ';password=' . PG_PASSWORD);
$this->dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
function getDB() {
return $this->dbConnection;
}
}
And here are the connection parameters (with sensitive information replaced by <>, note both files lack the ?> ending for some reason, I assume poor practice to exclude this?
<?php
/**
* Created by PhpStorm.
* User: msenol
* Date: 02.04.2015
* Time: 11:57
*/
// PG info
define("PG_HOST", "192.168.1.100");
define("PG_PORT", "5432");
define("PG_DB", "<database name>");
define("PG_USERNAME", "<usrname>");
define("PG_PASSWORD", "<pw>");
// SQL Fields
define("TYPE", "reading_location");
define("HUE", "hue");
define("LIGHTNESS_VALUE", "lightness_value");
define("CHROMA", "chroma");
define("DESCRIPTION", "description");
// Image Upload Path
define("BASE_IMAGE_PATH", "e:/gygaia/images/");
define("TEST_MODE", false);
I used the standard script to connect and was successful.
<?php
$dsn = "pgsql:host=$host;port=$port;dbname=$db;user=$user;password=$pw";
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 <strong>$db</strong> database successfully!";
}
}catch (PDOException $e){
// report error message
echo $e->getMessage();
}
$conn = null;
?>
We are all at a loss, and we need the system up and running since we are on fieldwork.

Related

Using SSPI to connect to remote PostgreSQL server using Npgsql

I am using nHibernate + Npgsql to connect to PostgreSQL server using windows authentication (SSPI). I have added the windows user to the Postgres and have configured the settings as recommended by http://wiki.postgresql.org/wiki/Configuring_for_single_sign-on_using_SSPI_on_Windows.
Here is my connection string:
"Server=;Port=5432;Database=;Integrated Security=true"
This is my code:
if (!sessionFactories.TryGetValue(sessionFactoryConfigPath, out sessionFactory))
{
Configuration cfg = new Configuration();
if (sessionFactoryConfigPath != DefaultConfigFile)
{
//Check.Require(File.Exists(sessionFactoryConfigPath),
// "The config file at '" + sessionFactoryConfigPath + "' could not be found");
cfg.Configure(sessionFactoryConfigPath);
}
else
{
cfg.Configure();
}
// Now that we have our Configuration object, create a new SessionFactory
sessionFactory = cfg.BuildSessionFactory();
When I run this I get the following error at sessionFactory = cfg.BuildSessionFactory();
"FATAL: XX000: could not accept SSPI security context"
When I checked the PostgreSQL logs the following entry was found:
"The token supplied to the function is invalid (80090308)"
I am using "PostgreSQL 9.3.2, compiled by Visual C++ build 1600, 32-bit". I got it from postgres website. I am using Windows 7 SP1 64-bit.
What am I missing here? Please help.

Uncaught exception 'MongoConnectionException'

Fatal error: Uncaught exception 'MongoConnectionException' with message
'Failed to connect to: localhost:27017: Permission denied' in
/var/www/html/test.php:8 Stack trace: #0 /var/www/html/test.php(8):
MongoClient->__construct() #1 {main} thrown in /var/www/html/test.php
on line 8
Hi Mongo experts...
I am a developer wanting to try out MongoDB. So installed centoOS 6.5 64bit in a test machine (Dell E520 Intel Dual Core 4GB Ram), installed PHP (Apache was already present).
Then installed MongoDB (yum install mongo-10gen mongo-10gen-server), then installed "pecl install mongo" (install ok: channel://pecl.php.net/mongo-1.4.5), added extension=mongo.so to php.ini.
To install pecl, I installed few other things like C++ compiler & php-pear. php5-dev & php5-cli was not available in yum so installed php-devel & php-cli (installed versions are php-devel-5.3.3-27.el6_5.x86_64 & php-cli-5.3.3-27.el6_5.x86_64
I turned off iptables firewall. Ran mongo --host localhost:27017 from shell & connected without problems
[root#localhost ~]# mongo --host localhost:27017
MongoDB shell version: 2.4.8
connecting to: localhost:27017/test
>
These are the contents of test.php
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
// connect
$m = new MongoClient();
//$m = new MongoClient("mongodb://localhost:27017");
// select a database
$db = $m->comedy;
// select a collection (analogous to a relational database's table)
$collection = $db->cartoons;
// add a record
$document = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
$collection->insert($document);
// add another record, with a different "shape"
$document = array( "title" => "XKCD", "online" => true );
$collection->insert($document);
// find everything in the collection
$cursor = $collection->find();
// iterate through the results
foreach ($cursor as $document) {
echo $document["title"] . "\n";
}
?>
As you can see I have tried both
$m = new MongoClient(); & $m = new MongoClient("mongodb://localhost:27017");
But I am getting the same error. Where am I going wrong?
This problem is documented at http://ca3.php.net/manual/en/mongo.installation.php#mongo.installation.fedora
Red Hat,Fedora and CentOS:
The default Apache settings on these systems do not let requests make network connections, meaning that the driver will get "Permission denied" errors when it tries to connect to the database. If you run into this, try running:
$ /usr/sbin/setsebool -P httpd_can_network_connect 1
Then restart Apache. (This issue has also occurred with SELinux.)
Thanks for all your support!
Hope this thread helps someone from going in circles!

I've installed MongoDB on a server. What now?

I'm interesting in playing around with NoSQL and particularly MongoDB. To this end, I've installed MongoDB on an external Linux box by SSHing into the server and running the following command:
sudo pecl install mongo
Mongo seems to have installed correctly and there's now a 'Mongo' section if I run phpinfo().
But what now? I seem to be having trouble going from here to using it in production. The problem being, I don't think the MongoDB service is running, and I haven't configured any MongoDB users because I'm not sure how to. As a result, the following test script fails:
<?php
// connect
$m = new Mongo();
// select a database
$db = $m->comedy;
// select a collection (analogous to a relational database's table)
$collection = $db->cartoons;
// add a record
$obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
$collection->insert($obj);
// add another record, with a different "shape"
$obj = array( "title" => "XKCD", "online" => true );
$collection->insert($obj);
// find everything in the collection
$cursor = $collection->find();
// iterate through the results
foreach ($cursor as $obj) {
echo $obj["title"] . "\n";
}
?>
As I get the following error message:
Fatal error: Uncaught exception 'MongoConnectionException' with message 'connecting to failed: Transport endpoint is not connected' in /home/[username]/public_html/mongodbtest/index.php:4 Stack trace: #0 /home/[username]/public_html/mongodbtest/index.php(4): Mongo->__construct() #1 {main} thrown in /home/woohoobi/public_html/mongodbtest/index.php on line 4
How can I get from here to using MongoDB?
You never actually installed MongoDB, but rather the PHP extension that lets you connect to a MongoDB instance. Install MongoDB by downloading it and installing it on your server, and then connect to it from your PHP script.

nodejs chat example does not work

I've come across a node chat example on github, When I try to run it, I see the following error:
Error connecting to mongo perhaps it isn't running ?
I've installed mongo 0.9.2, nodejs 5.2 pre, npm 3.0 and other dependencies. The example can be found here: https://github.com/gregstewart/chat.io
I can not determine whether if the example not really works or I didn't run it right. Please help.
Did you install and start mongo-db on your system? This error is mostly because of a missing mongo instance running on the local machine.
Check out the follwing code excerpts from chat.io.
main.js:
/**
* Configure the user provider (mongodB connection for user data storage)
*/
var userProvider = new UserProvider('localhost', 27017);
Creates a new UserProvider object using host and port for database (localhost:27017, mongo-db default).
UserProvider.js:
UserProvider = function(host, port) {
this.db = new mongo.Db('node-mongo-chat', new Server(host, port, {auto_reconnect: true}, {}));
this.db.addListener('error', function(error) {
console.log('Error connecting to mongo -- perhaps it isn\'t running?');
});
this.db.open(function() {
});
};
Opening the connection to the server, printing out an error on failure (the error you mentioned above).
Consider reading up on the mongo-db docs concerning installation and setup here

problems using Zend_Db when running php as cli script

I am trying to add some administration scripts to a Zend Framework project, that will end up being run nightly via cron.
However I've hit my first problem with the script when trying to use Zend_Db. I am currently doing a very simple SQL call to get some results and just display them using var_dump() however I get the following error message:
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
The script so far looks like this:
<?php
chdir(dirname(__FILE__));
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
date_default_timezone_set('Europe/London');
//directory setup and class loading
set_include_path('.' . PATH_SEPARATOR . '../library/' . PATH_SEPARATOR . '../application/models/'
. PATH_SEPARATOR . '../library/MyApp/'
. PATH_SEPARATOR . get_include_path());
include "Zend/Loader/Autoloader.php";
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);
//start sessions
Zend_Session::start();
//read configuration data
$config = new Zend_Config_Xml('../application/config/config.xml', 'app');
//we create an ability to capture errors and write them to an error log if there are problems.
$log = new Zend_Log();
$writer = new Zend_Log_Writer_Stream($config->log->logfile);
$log->addWriter($writer);
$filter = new Zend_Log_Filter_Priority((int)$config->log->level);
$log->addFilter($filter);
//we now need to get the list of graduates that need to have their CV's removed from the search engine
$date = new Zend_Date();
$date->sub(3, Zend_Date::MONTH);
echo $date->get(Zend_Date::ISO_8601);
$sql = "SELECT userid, guid FROM users WHERE lastlogin = ? AND active = 1";
$db = Zend_Db::factory($config->database);
try{
$results = $db->fetchAll($sql, $date->get(Zend_Date::ISO_8601));
var_dump($results);
}catch(Zend_Db_Exception $dbe){
$log->debug($dbe->getMessage()."\n".$dbe->getTraceAsString());
}
//close database.
if($db->isConnected()){
$db->closeConnection();
}
What am I missing? I know that the database connection settings in the Zend_Config object works and the web application is running fine with no issues.
I am running this using Zend Framework v1.7.6 at present.
Also are there any generic tips for using the Zend Framework in a cli script?
Many thanks.
PHP will try to connect to mysql via tcp/ip, unless the mysql server is on the same machine as the script and "localhost" is used as the db host. Then it will use a filesystem socket.
It could be that mysql cannot create /tmp/mysql.sock due to permissions, or that it's not configured to do so. The Mysql manual should have more on that.
A quick way to get around this for now should be to change the db host in your Zend_Db settings to an actual ip mysql listens on, not "localhost" or "127.0.0.1".
also check your php.ini and look for the mysql_sock
this is often a trap for Mac users with Mamp installed: Snow Leopard comes with a php configured which is separate to the ph included with Mamp