trouble connecting to MySql DB (PHP) - php

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.

Related

Error connecting a remote server from a PHP page

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.

Connect to database using PHP

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.

unable to connect external server database

how to access cpanel database access in godady server using mysql php connectivity
i have tried a lot but last long it gives me
Warning: mysql_connect() [function.mysql-connect]: Lost connection to
MySQL server at 'reading initial communication packet', system error:
111 in /home/content/q/u/i/quickandkosh/html/searchdata.php on line 6
Could not connect: Lost connection to MySQL server at 'reading initial
communication packet', system error: 111
I'm not sure about go daddy but I know some web hosts need a specific path to mysql server. Most us localhost but others, such as ipage use a full path through the virtual server. Go into your cpanel at godaddy and then "mysql databases" and it should have a "server" title with address below it somewhere in the page.
Just to note, you should also be using mysqli_connect.
I think your host could be wrong, wrong port, a firewall blocked the connection. So I would double check your connection settings.

local server connection to remote mysql

I have found this question here and around internet for few times, but none answered helped me.
So I have a local apache+php server and trying to connect to remote mysql database. But script returns me error and in error log i see:
PHP Warning: mysql_connect(): Premature end of data (mysqlnd_wireprotocol.c:553) in D:\\_SERVER\\_WWW\\project\\api\\classes\\database.php on line 13
PHP Warning: mysql_connect(): OK packet 1 bytes shorter than expected in D:\\_SERVER\\_WWW\\project\\api\\classes\\database.php on line 13
PHP Warning: mysql_connect(): mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication. Please use an administration tool to reset your password with the command SET PASSWORD = PASSWORD('your_existing_password'). This will store a new, and more secure, hash value in mysql.user. If this user is used in other scripts executed by PHP 5.2 or earlier you might need to remove the old-passwords flag from your my.cnf file in D:\\_SERVER\\_WWW\\project\\api\\classes\\database.php on line 13
The script for connection (just the part connecting to server):
function db_connect(){
// Connect to the database server
$connection = mysql_connect("server.com:3306","username","password",true);
if (!$connection){
echo "Could not connect to the Database server, please check your settings";
die;
}
...
}
The trick with setting PASSWORD did not work for me, can someone please help me ?
Thank you
The remote needs to allow incoming MySQL connections from your server's IP address. This can be added within a the MySQL config file from the remote server.
For more information in how to edit the config file, here are detailed instructions.
http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
The firewall needs to port-forward to the server:port on the respective system,
which requires a static or dhcp address reservation for the mysql server machine

MySQL Connection String Using PHP

I am trying to connect to the database that is hosted on my server through my local machine.
my server has cPanel 11, and it is a typical shared server, powered by CentOS, PHP and MySQL installed.
to be precise i am holding the reseller account in the same server. i want to access the database between different accounts or domains.
in MySQL connection String i tried defining the domain name, and it isn't working. here is what i tried.
<?php
$link = mysql_connect('mydomain.com', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
this is giving 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://bhatkalnews.com:3306) in C:\wamp\www\test\conn.php on line 4
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:\wamp\www\test\conn.php on line 4
Fatal error: Maximum execution time of 60 seconds exceeded in C:\wamp\www\test\conn.php on line 4
what do i have to define in connection string to make it work?
Firewall ?
try
telnet mydomain.com 3306
on the command line.
If that doesn't work, the connection is blocked by a firewall, most likely
Did you try adding the port explicitly:
mysql_connect('mydomain.com:3306', 'mysql_user', 'mysql_password');
or the port youre running on if not the default 3306. This of course assumes your server allows remote connections.
Also make sure the user in quest has access from the IP address youre connecting from. This isnt youre issue right now, but it may be the next question you have after you get the server to respond :-)
Please refer,
http://forums.mysql.com/read.php?52,294772
You need to add your IP address/range on Access Host list in Remote MySQL in cPanel.

Categories