I'm cant connect to PostgreSQL with php code. My Postgresql is docker container.
I get an IP with:
docker inspect toshi_db_1
How I'm trying to connect:
$dbconn = pg_connect("host=172.17.0.2 port=5432 dbname=toshi_development")or die("Could not connect");
Error: Warning: pg_connect(): Unable to connect to PostgreSQL server:
could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "172.17.0.2" and accepting
TCP/IP connections on port 5432?
I thought there could be problems in PostgreSQL.conf with listen_address configuration parameter, but it allows all connections, so I have no idea where the problem is.
UPDATE: I fixed it myself, just tried to expose wrong port in
docker-compose file.
I run in the same situation today. The solution was replacing the localhost port with the name of the container. Like:
$dbconn = pg_connect("host=toshi_db_1 port=5432 dbname=toshi_development")or die("Could not connect");
Not sure if it should be the same with IPs. But doesn't the IPs change?
So, for other people seeing this post: remember that 127.0.0.1 always points to current container. So you must replace that "localhost" part with your container name.
Related
I am having this error:
Warning: mysql_connect(): [2002] Connection refused (trying to connect via tcp://127.0.0.1:3306)
I saw a similar question and I tried the solution by changing localhost to 127.0.0.1, but it is still not working. I am using Mac OS X Lion. Please any help would be appreciated
This is the PHP code I use to connect:
<?php
require("constants.php");
// open database
$connect = mysql_connect(DB_SERVER,DB_USER,DB_PASS) or die('error connecting: ' . mysql_error());
// select database
mysql_select_db(DB_NAME) or die('error selecting db: ' . mysql_error());
?>
Try making sure your firewall if open to connections on port 3306.
The command should be ipfw set enable 3306, but if you want you can read more about actually setting the firewall permissions.
Another thing could be to make sure MySQL is listening on port 3306, edit the my.ini file and set
[client]
port=3306
And then restart MySQL.
I've had that problem before. You can fix it by commenting out "skip-networking" in the configuration. (my.ini or my.cnf, it's /etc/mysql/my.cnf on Linux, not sure about Mac)
-- edit
Also, you can try typing "netstat" in the Terminal, to make sure that mysql is listed there (should be something like 127.0.0.1:mysql or 127.0.0.1:3306). If it is not listed, MySQL either hasn't started, or hasn't been able to bind.
I am attempting to connect to a remote MySQL server from my local machine virtualhost using the following code:
$conn = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
mysql_select_db($dbname, $conn) or die(mysql_error());
My problem is that I am unable to connect locally, receiving the error:
Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060)
This is not the case when I upload the same PHP file to the server. I am able to query the database with no problems at all.
I am unable to connect via command line either, but I can access cPanel which rules out the chance of my IP being banned accidentally.
My local server is running PHP 5.2.9, the remote server 5.2.12
firewall of the server must be set-up to enable incomming connections on port 3306
you must have a user in MySQL who is allowed to connect from % (any host) (see manual for details)
The current problem is the first one, but right after you resolve it you will likely get the second one.
It is very easy to connect remote MySQL Server Using PHP, what you have to do is:
Create a MySQL User in remote server.
Give Full privilege to the User.
Connect to the Server using PHP Code (Sample Given Below)
$link = mysql_connect('your_my_sql_servername or IP Address', 'new_user_which_u_created', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db('sandsbtob',$link) or die ("could not open db".mysql_error());
// we connect to localhost at port 3306
I just solved this kind of a problem.
What I've learned is:
you'll have to edit the my.cnf and set the bind-address = your.mysql.server.address under [mysqld]
comment out skip-networking field
restart mysqld
check if it's running
mysql -u root -h your.mysql.server.address –p
create a user (usr or anything) with % as domain and grant her access to the database in question.
mysql> CREATE USER 'usr'#'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON testDb.* TO 'monty'#'%' WITH GRANT OPTION;
open firewall for port 3306 (you can use iptables. make sure to open port for eithe reveryone, or if you're in tight securety, then only allow the client address)
restart firewall/iptables
you should be able to now connect mysql server form your client server php script.
This maybe not the answer to poster's question.But this may helpful to people whose face same situation with me:
The client have two network cards,a wireless one and a normal one.
The ping to server can be succeed.However telnet serverAddress 3306 would fail.
And would complain
Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060)
when try to connect to server.So I forbidden the normal network adapters.
And tried telnet serverAddress 3306 it works.And then it work when connect to MySQL server.
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
I've made a database on my server but can't reach it.
I tried to connect to the database using php:
mysql_connect("$host", "$username", "$password")or die(mysql_error());
I get this message:
Warning : mysql_connect (): 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. in C: \ xampp \ htdocs \ New Site Ron (app) \ connection.php on line 7
Is it because I'm using xampp or is it something else?
Thanks in advance,
Sam
I fixed it by using my sever instead of using xampp.
I can't connect to MySQL at "thehostnameyouprovided.com" either ... not on port 3306 anyway.
So either the MySQL daemon/server isn't running there, or it's not running on TCP/IP 3306 (for example, it's only running on a local socket), or the server has a firewall that isn't allowing access to me from my IP address (and might not be allowing it to your from your address either).
At any rate, making absolutely sure the server is accepting connections from your address on port 3306 should be your first line of inquiry.
I am attempting to connect to a remote MySQL server from my local machine virtualhost using the following code:
$conn = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
mysql_select_db($dbname, $conn) or die(mysql_error());
My problem is that I am unable to connect locally, receiving the error:
Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060)
This is not the case when I upload the same PHP file to the server. I am able to query the database with no problems at all.
I am unable to connect via command line either, but I can access cPanel which rules out the chance of my IP being banned accidentally.
My local server is running PHP 5.2.9, the remote server 5.2.12
firewall of the server must be set-up to enable incomming connections on port 3306
you must have a user in MySQL who is allowed to connect from % (any host) (see manual for details)
The current problem is the first one, but right after you resolve it you will likely get the second one.
It is very easy to connect remote MySQL Server Using PHP, what you have to do is:
Create a MySQL User in remote server.
Give Full privilege to the User.
Connect to the Server using PHP Code (Sample Given Below)
$link = mysql_connect('your_my_sql_servername or IP Address', 'new_user_which_u_created', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db('sandsbtob',$link) or die ("could not open db".mysql_error());
// we connect to localhost at port 3306
I just solved this kind of a problem.
What I've learned is:
you'll have to edit the my.cnf and set the bind-address = your.mysql.server.address under [mysqld]
comment out skip-networking field
restart mysqld
check if it's running
mysql -u root -h your.mysql.server.address –p
create a user (usr or anything) with % as domain and grant her access to the database in question.
mysql> CREATE USER 'usr'#'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON testDb.* TO 'monty'#'%' WITH GRANT OPTION;
open firewall for port 3306 (you can use iptables. make sure to open port for eithe reveryone, or if you're in tight securety, then only allow the client address)
restart firewall/iptables
you should be able to now connect mysql server form your client server php script.
This maybe not the answer to poster's question.But this may helpful to people whose face same situation with me:
The client have two network cards,a wireless one and a normal one.
The ping to server can be succeed.However telnet serverAddress 3306 would fail.
And would complain
Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060)
when try to connect to server.So I forbidden the normal network adapters.
And tried telnet serverAddress 3306 it works.And then it work when connect to MySQL server.