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.
Related
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.
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.
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 have recently configured a MySQL server on my Windows home computer. In addition, I have also created a website using 0fees.net-- a free hosting provider which comes with a vista panel that has various services including PHP support, FTP file-hosting, its own MySQL server etc.
For that website, I have created a "login" PHP script in order for people to login to my webpage. However, instead of reading from the MySQL database given to me on the cPanel on 0fees.net, I want the PHP logon script to read directly from the MySQL server that I have configured on my home computer. To do this, I have taken several steps:
1) Configured MySQL using the MySQL Server Instance Configuration Wizard
In that configuration, I have enabled TCP/IP Networking on port 3306 (and enabled a firewall exception for that port) and have enabled root access from remote machines.
2) On the MySQL command-line-client, I have granted remote access capabilities to other units by using:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'password';
3) I have forwarded port 3306 to my router (and have used various port checkers to confirm that it is open)
4) I have created the login script called "login.php" which lies in my site's .htdocs directory and that process an HTML login form on the homepage of my website. It attempts (and fails) to connect to my local MySQL server. The segment of interest of that php script is:
$host="my_external_ip:3306"; // External IP of my computer and port to listen on
$username="root"; // Username specified in the GRANT command-line-client command
$password="password"; // Password specified in the GRANT command-line-client command
$db_name="logon"; // MySQL database name
$tbl_name="accounts"; // Table name within the database
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die(mysql_error());
mysql_select_db("$db_name")or die("cannot select DB");
When attempting to connect to the MySQL sever, I get an error message that states:
Can't connect to MySQL server on 'my_external_ip' (4).
It should also be noted that I am successfully able to connect to my local MySQL server remotely from other machines which are on other internet networks (tested with Navicat) using this same external IP address on port 3306. Any ideas as to why my website cannot also connect?
Thanks in advance for any insights.
Since I can tell that your server is definitely running (and visible from the internets in general), there are a few possibilities:
You forgot to run FLUSH PRIVILEGES
Your username or password are wrong
Your free host provider is blocking the connection
My money is on #3, there are a lot of incentives to do that.
#Chris mentioned that your provider might be blocking outbound requests on port 3306. Here's a quick way to check if that's the case and get around it:
Configure your router to port forward from port 80 on the router to port 3306 on your box. This will allow you to connect to MySQL from outside your network on port 80 instead of port 3306.
Change your PHP code to connect to your IP on port 80 instead of 3306.
Your hosting provider might block outbound port 3306, but the almost certainly wouldn't block outbound port 80 requests (lots of people using CURL or web services and such).
Well I will let you in on a little trick I learned, all you got to do is go into wamp>mysql>my.ini and replace all 3306 with 3305 then save and then apply this to your firewall and port forward 3305. Then when you are connecting remotely, simply do
mysql_connect("ip adress:3305", "username", "password");
I've tested this and it works because the web hosting sites block incoming traffic from port 3306.
I have the following PHP code to connect to my db:
<?php
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
?>
However, I get the following error:
Warning: mysql_connect() [function.mysql-connect]: [2002] A
connection attempt failed because the connected party did not (trying
to connect via tcp://localhost:3306) in C:\Program Files (x86)\EasyPHP-
5.3.2i\www\checklogin.php on line 11
Warning: mysql_connect() [function.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:\Program Files (x86)\EasyPHP-
5.3.2i\www\checklogin.php on line 11
Fatal error: Maximum execution time of 30 seconds exceeded in
C:\Program Files (x86)\EasyPHP-5.3.2i\www\checklogin.php on line 11
I am able to add a db/tables via phpmyadmin but I can't connect using PHP.
Here is a screenshot of my phpmyadmin page:
What could be the problem?
Check the following:
Is MySQL running?
Is there any firewall that could be blocking your computer from accepting connections to MySQL on port 3306?
Is MySQL listening on port 3306, or did it get changed to something nonstandard?
An odd one, but try changing from localhost to 127.0.0.1
Basically, the errors you're getting mean that it cannot connect to the server. It sends request to localhost:3306, and only waits so long for a reply. it's not getting it, which means the request is either blocked (firewall) or ignored (MySQL is not running and/or is listening on a different port)
If phpMyAdmin came with the MySQL install, then it could be that it was configured to use the appropriately different port
ha i also had that issue now i fix it by changing port number e.g "find.db.13344.hostedfind.com:3306". before it my connection was trying to access the "find.db.13344.hostedfind.com:3307" so it gave me this error
Warning: mysql_connect() [function.mysql-connect]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306) .
Maybe your MySQL is not using TCP for localhost. Please try
$host='/tmp/mysql.sock';
or whatever socket it might be using.
I also have noticed this issue linux version of xampp with MySSQL 5.6.12-log running over network. Also, a question asked in MySQL forum (http://forums.mysql.com/read.php?52,294772,294772) resembles this, but not satisfactorily answered.
What I have noticed is, it is probably due to Networking being unstable, or too many mysql connections from other developers in the network connecting to the database server at the same time - and keeping the port number busy for a fraction of time. It is not often related to the configured connection time, nor maximum execution time, nor wrong username/password etc.
Amazingly, the same script, same password, and same network allows the same server run properly on the second attempt - without having to do anything as a fix.
Hence, it should be your momentarily unstable network fluctuation (or busy mysql connections), that goes on and off, and your script attempted to connect to the server when network was down for a very short time.
That is a guessed answer from personal experience related to this error, may not be exact.
But if the error message is persistent, you should really need to do something.