PHP MongoDB Connect to another IP - php

I am trying to figure out why I can not connect to MongoClient() hosted on some different IP. I am using below code to connect to another IP and accessing its DB and Collections
<?php
$username = 'root';
$password = '';
$conn = new MongoClient("mongodb://SYSTEMIP:27017", array("username" => $username, "password" => $password));
$database = $conn->testdb; //Creating New Database
$collection = $database->users; //Calling users collection
?>
The exception printed is MongoConnectionException: Failed to connect to: SYSTEM:27017: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond..
Is there anything that I need to change on system. MongoDB is properly installed on the IP that I need to connect to. Pls correct me if I am doing something wrong.
Thanks.

MongoDB 2.6 ships with bindIp config set. Look into your config for it.
http://docs.mongodb.org/manual/reference/configuration-options/#net.bindIp
Also, check whether 27017 is TCP permissible. Check for Firewalls or other restrictions.

Related

Amazon RDS php integration

I'm kinda new to this database world.
I'm trying to implement some php scripts to be used with Amazon RDS system.
I have already created the RDS db instance, but i'm struggling to connect to the database with php.
To connect:
$link = new mysqli($host, $user, $password, $dbname, $port);
$host is the endpoint provided on the db instance
$user and $password are the details i used to create the database.
$dbname, i really don't know where to get this value because on the tags of my database, the database name is empty. $port is 3306.
When i run the script, this warning comes up:
Warning: mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL
server through socket '/var/run/mysqld/mysqld.sock' (2)~
Can anyone help me please? Thanks.
The database name is the name of the actual database that you created, not the name (tag) of the DB server instance. You had to specify a database name when you created the RDS instance, at the same time you specified the database username and password.
In the RDS console when you are viewing the properties of the database instance there is a field labeled "DB Name". That's your database name.
Also, you'll need to be sure to open the port in the security group assigned to the RDS instance before you will be able to connect.

Connecting to a MySQL db created in an Ubuntu AWS instance

Ok so I created an ubuntu instance from AWS and downloaded LAMP on it as you would in a regular Ubuntu OS. When I try to connect to the db from my php script it's rejecting the connection. saying "Connection failed: Connection refused". So I guess my question is this: Does amazon not allow you to connect to a db without using their RDS database service or am I putting something wrong here? (I've hidden some of the data for security purposes, the ... are numbers of my instance). When I put "ec2-34-...-..-64.compute-1.amazonaws.com" into the browser the apache message comes up so I don't see why this is not working as a server name?
$servername = "ec2-34-...-..-64.compute-1.amazonaws.com";
$username = "root";
$password = “hidden";
$dbname = "questions87";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
You need to change your EC2 Security Group Inbound Rules related with 3306, you can find that on the AWS FAQ. Then, you need to be sure that your user#YOURIP have all permissions on your MySQL database. For your Inbound Rules it is recommended to use a Custom Rule to your IP, not All Traffic.
Reference and further reading:
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html

MySQL connect failed. Can't connect to MySQL server on 'http' (4)

I try to connect my android application using JSON Parser to the web hosting. But whenever I try to connect (even just open using url in the browser) I will get the error message.
<?php
$dbHost = 'http://sql4.000webhost.com/localhost';
$dbUser = 'a6410240_cbetTD';
$dbPass = 'xxxxxx';
$dbName = 'a6410240_cbetTD';
$conn = mysql_connect ($dbHost, $dbUser, $dbPass) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db($dbName,$conn);
?>
This is my database.php file. The full error message is
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'http' (4) in /home/a6410240/public_html/database.php on line 8.
I have tried change the $conn but still it didn't worked for me.
Thanks
If your database and application is on same server then use "locahost" in $dbhost.
And if your database and application is on different servers then you need to use IP address or hostname in $dbhost and the database user should be added on database server provided with required privileges.
The problem you are having was already mentioned in one of the comments, this one to be precise.
For your solution to work, all you need to do is omit the part http:// at the beginning and probably /localhost at the end.
The host is only the domain you are referring to. In this case sql4.000webhost.com. With /localhost you tried to already connect to a database, although your configured database is supposed to be a6410240_cbetTD.
MySQL use TCP port 3306 by default
($dbport) and hostname or IP address ($dbhost). For LAMP (Linux-Apache-MySQL-php) you can find a lot of tutorials.
Usually MySQL server listens internal port (which can't be reached via Internet) for security purposes.
If you familiar with docker, you can simply download examples of LAMP solutions from hub.docker.com.

PHP connection to PostgreSQL, FATAL: password authentication failed for user

I am new to postgreSQL and web development and lately I tried to connect my previously created database via PHP. But things didn't worked out, I get the password authentication failed for the user postgres. I've been looking everywhere but can't find anything to solve this issue.
The database is hosted on my iMac, port 5432 and php script on heliohost.org which will be used later on to query the database.
Here is my php script:
<?php
$host = "host=127.0.0.1";
$port = "port=5432";
$dbname = "dbname=osm";
$credentials = "user=postgres password=newPassword";
$db = pg_connect( "$host $port $dbname $credentials" );
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
?>
My pg_hba.conf looks like this :
local all postgres md5
and my postgresql.conf has : listen_addresses = localhost
I am a bit confused with this, if anyone have suggestion to get this up and running.
Thanks everyone!
If you specify 127.0.0.1 as your host, it will attempt to hit your web server at heilohost.org, not your iMac. If you really want to do this (and there are a TON of reasons why it's a terrible idea), you'll have to set listen_address=0.0.0.0, forward the port on your router to your iMac, and use your public IP address in the connection string.

PHP Localhost Application connect to server database

I want to ask about how to connect my localhost application (C:xampp/htdocs/myproject) to database at my server host (www.someweb.somedomain)?
Am I possible to do that? If it is, how to connect it at my php config? Right now my config (server.php) is:
<?php
$host = "my web IP public:3306";
$user = "root";
$pass = "";
$db = "dispatcherDB";
$conn = mysql_connect($host, $user, $pass) or die ("Cant connect to mySQL");
mysql_select_db($db);
?>
what I got:
Warning: mysql_connect(): No connection could be made because the target machine actively refused it. in C:\xampp\htdocs\XMS\server.php on line 7
So, what must I filled for $host? I'm trying using website IP, it still can't connect. Maybe there's someone here have experience at this problem?
Sorry for my bad English
If you have cPanel access to the remote server then you need to mention that from which ip addresses it should allow access to MySQL..
In cPanel you will get Remote MySQL under heading Databases:
Clicking Remote MySQL will give you the option to add hosts from where you want to allow connections to your MySQL server:
Also, you can check localhost without specifying port number as value of $host..
Probably Mysql server is not allowed root access from remote servers.
you could check this post

Categories