im trying to use pg_connect to access postgres on another server,,
i did opened the remote access and i can use bash to connect to my postgres server ! and i did host all all client_ip/32 trust in config but when im trying to use pg_connect i get error 500 !
pgsql is also installed and i've checked it with function_exists('pg_connect') and i returns true !
my code is :
$dbconn = pg_connect("host=remote_IP port=5432 dbname=myDB user=postgres") or die("Could not connect");
i've also tried this and get fatal error 500 again
$dbconn = pg_connect("host=remote_IP port=5432 dbname=myDB
user=another_super_user_i_made password=user_password") or die("Could not connect");
what is the problem ?
when you are able to connect to a server from one machine on your network, but get a connection timed out issue from another machine, it usually is due to one of a few things.
1) DNS. The machine you are testing from (the "remote access" as you call it) is able to resolve the DNS name and the webserver running your PHP code is not. You can use actual IP address instead of DNS names to test this.
2) Connectivity. Different machines are connected in different ways in a network. If you are able to get to an IP from the "remote access" machine but the web server is having trouble connecting to it (with "connection timed out"), try connecting directly to that web server (using "remote access" like SSH) and see if you can connect manually from there.
If it's not a network related issue it could be an issue with your default PG timeouts. Try setting them manually with the connect string. Specifically, the "connect_timeout" option, like this:
$d=pg_connect('host=example.com user=pgsql dbname=postgres connect_timeout=5');
There was host blocking issue !
my client hosting was not enabling remote access to DB !
you should check with your hosting that they can trace your problem
Related
I have a .php file which should receive and show data from a remote database. I run my program from PHPStorm (which is connected to the remote database through the "Database" right-hand pane) and a browser. Both ways I get an error which depends on the number of arguments I pass to pg_connect() function.
If I use
$dbconn = pg_connect("host=pg hostaddr=server.address.ru port=5432 dbname=studs user=... password=...")
than the error is
Unable to connect to PostgreSQL server: could not parse network address "server.address.ru": Unknown host in...
But I am sure that I wrote the address correctly (there are no typos in it). This way I am not sure about the correctness of the format of the passed arguments.
If I use
$dbconn = pg_connect("host=server.address.ru dbname=studs user=... password=...")
command, the error is
pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Connection timed out
I found a lot of information about this errors, but it mostly refers to localhosts and doesn't solve my problem. I guess, the problem can be in the way this connection is set in the function, but I do not know why it doesn't work properly. How can I solve it?
Thanks to #TangentiallyPerpendicular, I got on a right way of setting the connection. But since I have PostgreSQL remote connection, it wasn't just up to this answer.
What I did and how I set the connection (I work from PHPStorm so all the actions are based on this platform):
Open PuTTY and set an SSH connection (an SSH tunnel) between the server's DB port (for PostgreSQL it's usually 5432) and your local computer's PostgreSQL port (5432 most often too). You can do the same from a command line.
Open PHPStorm and in "Database" section (an icon on the right-hand side of the environment or "Data Sources and Drivers" in Settings) set general information ("General" section) and set another SSH tunnel ("SSH/SSL"). In SSH Configurations (the same "SSH/SSL" section) set a local port - it will be important in our connection! Let's say, this port is 20000. The port of the server you're connecting to is a usual one (usually 22 or 2222).
In the program the right use of function is $dbconn = pg_connect("host=localhost port=20000 dbname=studs user=... password=...") or die('Error: ' . pg_last_error());
The connection is set.
For those who has troubles setting an SSH tunnel with a remote PostgreSQL from PHP this can be useful too.
I am running PHP inside of the official PHP Docker container found here: https://github.com/docker-library/php
I am trying to get my application to connect to an external MySQL server using the old mysql_connect function as that's what our application uses. When trying a very simple script it yields the error: Warning: mysql_connect(): No route to host.
I have verified that using the standard MySQL CLI that I can connect to the intended server from inside the docker container so know that my container can speak to the outside world with no issues. Similarly things like file_get_contents() work with no issues indicating no DNS issues.
Both the host and the container have their DNS set to 8.8.8.8 and the container was launched with the --net host flag.
For reference here is the script I tried:
<?php
mysql_connect("my-external-rds.amazonaws.com", "user", "password")
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
Any ideas on how to resolve this issue? I can provide any information necessary.
I'm trying to learn to use SQL stuff in PHP but I'm having an issue simply connecting to my database.
The database is almost certainly working with remote access to some extent. I disabled iptables temporarily (I know, bad bad bad!), so there's no firewall blocking access. The MySQL user I am using is configured to be able to connect from any IP. The SQL server is listening properly to all connections and is run on a Linux dedicated server.
[root#1742CC-XEON ~]# netstat -lpnut | grep mysql
tcp 0 0 0.0.0.0:3315 0.0.0.0:* LISTEN 3915/mysqld
I've also used the exact same credentials to successfully remotely connect to my database in Java, like so:
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://__ip__:_port_/_dbname_", "username", "password");
System.out.println("Connection established.");
This functions just fine, and I can successfully run queries, etc. Since I have iptables disabled at the moment this connection functions just fine anywhere that has Java and the Java SQL connector.
Now, I'm trying to connect to the same database in PHP:
$db = mysqli_connect($sql_host, $sql_user, $sql_pass, $sql_db, $sql_port);
And this connection fails with the following error:
Warning: mysqli_connect(): (HY000/2003): Can't connect to MySQL server on 'xx.xx.xx.ip' (111)
Note that $sql_user, $sql_pass, $sql_host, etc. are all the same as the credentials used in the Java connection, so there shouldn't be any issue with the user not being able to access the server from a new IP, since that user is granted access on % and I've made sure that it is indeed able to access the SQL server from multiple IPs not explicitly allowed in the users table. I've also tried different users, including root, and all give the same connection error.
I've spent a few hours looking around but I honestly can't find anything that seems to be the right answer, would really appreciate any help!
Is it possible that this is caused by my webhost? I've tried this script on HostGator webhost as well as the whois.com hosting and both give the same error. Could they possibly be preventing the PHP script from connecting to the remote database?
Have you tried telneting to the server on 3315 from your webhosts?
(this question might not belong here, but on ServerFault, I don't know exactly where the problem comes from)
I'm trying to connect to an FTP server like this:
$con = ftp_connect( '86.xxx.xx.xxx', 21 ) or die("FTP connect error");
and it always throws me the error message.
I can connect like this to other FTP servers, but not this one. I can also connect to this one via an FTP client.
What server settings could be wrong that permit a client connect but not PHP?
Thanks for any help!
It seems my host was blocking remote FTP access to other servers. Oddly enough it allowed me to connect to my other servers at this host.
I get this everytime I use mysql_connect() no matter what database I choose:
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'IP' (111) in filename.php on line 17
A MySQL error has occurred: Can't connect to MySQL server on 'IP' (111)
The exact same file works on my personal website fine. I have tried multiple databases hosted on different servers and it always gives that output.
The database itself is hosted on the same server, but using its full IP in mysql_connect(). Using localhost:port doesn't work either as it says:
Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in filename.php on line 17
A MySQL error has occurred: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
But using the IP should work as it has worked calling it via the same file hosted on other servers.
This is the code:
$connect = mysql_connect($db_url,$db_user,$db_pass); // connects
if ($connect == false) exit("A MySQL error has occurred: " . mysql_error());
Now since the file works on other servers i am guessing it is something to do with the server it is on and might need something changed. I don't personally have root access to the server (just my part of the shared host). Is there anything I can do i php, editing the php.ini file or something I should pass on to someone with root access?
Edit: Ok it turns out that the server doesn't have access to outside databases, so thats why the IP didn't work. Thanks for all your answers but we have decided simply to change hosting provider. We need to be able to access an outside database.
This is on a hosting service? Check their documentation, there will be something that tells you where to find mysql. It isn't necessarily localhost.
For example, on startlogic.com, you use: yourdomain.startlogicmysql.com
Can you connect using mysqladmin using the same host, username and password?
mysqladmin -h $db_url -u $db_user -p $db_pass
Replace $db_xxxx with real values.
If that works from the same host as your php script, then sudo to the apache User and try the same test. One of those must be failing.
EDIT: nevermind on sudo part, I noticed that you don't have root access.
Something else to try: Use '127.0.0.1' instead of 'localhost'. I have had issues before where mysql stupidly assumed it could silently change 'localhost:' to '/var/run/mysqld/mysqld.sock'.
Your wording is not very clear, I hope you are not thinking you can connect to the same mysql server from any old web server just because you know the IP address and port number. If the web host is at all competent, they have probably firewalled mysql so it is only accessible through their own web servers.