mysql_connect() failed - Error (4) - php

I developed a PHP application with MySQL and node.js implementation.
I am trying to connect to my production DB like the following:
mysql_connect('54.212.117.38', 'root', '********');
This is the Error I get :
Could not connect: Can't connect to MySQL server on '54.212.117.38' (4)
I must mention that I can reach the PHPmyAdmin directly through the browser.
Moreover, On local host the application is connecting to MySQL with no problem.
I also tried restarting the Apache server, shutting down the Node.js procedure...
BTW - My production server is running Ubuntu.
Does anyone has an idea on what's the reason that it doesn't work?

There is more than one possibility. Some advice:
Make sure the mysql server behind this IP address is running and accepting connects.
Make sure the user can access from your clients ip.
Check out if perhaps the connection process takes too long (in this case, you can try increasing the connect_timeout).
If nothing else helps, trace the traffic between client and server. Maybe a firewall is slightly too aggressive (I've had that once, user root was triggering an IDS rule...).
A sidenote, the mysql extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

the problem is the firewall rules.
When you connect via Phpmyadmin, you are connecting through the web interface and internally Phpmyadmin is connecting to localhost.
Check your local ip or the ip of the server that you want to connect from.
Make sure you change the parameter bind-address to:
bind-address = 0.0.0.0 = 0.0.0.0

Related

err_connection_timed_out and PHP Warning: mysqli_connect():(HY000/2002):Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

Recently, from few weeks ago I got err_connection_timed_out just sometimes while working with my website.
and i am sure, my users get this error too.
When i see at error log, there are a few of message like below
PHP Warning:mysqli_connect(): (HY000/2002): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock
I changed 'localhost' to '127.0.0.1' for TCP connection instead of Unix socket, but no success.
My server host admin do not accept any problem at server side (like always!).
I asked them to restart Mysql, but they do not accept because it is a share server.
It is a Directadmin server and they recommend switch to Cpanel as the last chance.
Do you have any experience and solution for this problem?
EDIT: The main problem is err_connection_timed_out which i get in browser and i don't know if it relates to Mysql connection error in log file.
Switching might have positive effects because it's differently setup.
But if it happens sometimes it is probably the MYSQL server is burping.
Lately it's normal that the services has limit per user; so you can try to enable cache (of that is possible); to see whether it's less frequent (might be some queries that is running long).
do you have any cronjobs running or other processes around the time you receive those errors?
Normally this problem is caused due to MySQL server timing-out database connections is faster than the Stash connection pool can notice. This exhausts the Stash connection pool as it will keep its connections open while the MySQL side of the connections is already closed.
Easiest solution.
You can fix it by simply change this parameter (wait_timeout) at the Mysql config.
More difficult but better
Also you can close all connections in your code after you got the values. And finally you can change your connection to your database in order to reuse an existing connections.
And last but not least
Try a vps you got out there too many oferts, even for free. You wouldn't be able to change anything in a shared host sorry. :(

How to fix "#2003 - Can't connect to MySQL server on '127.0.0.1' (61) the server is not responding."

I am having issues connecting to MySQL server. I go to localhost:8888/phpMyAdmin/?lang=en and get the error:
"#2003 - Can't connect to MySQL server on '127.0.0.1' (61) the server
is not responding."
I've tried many different solutions however, none have worked. If anyone has any advice or ways to fix this, it would be a great help.
Ensure that MySQL is enabled in MAMP, and that you have also enabled "Allow network access to MySQL" in MAMP's MySQL settings page.
I'd recommend restarting your system, and confirming that mysql daemon start with your mamp server properly. You may also try logging into the command line mysql, and observe the configuration. You seem to suggest that you have tried many things, and I assume that you tried restarting. I know it seems ridiculous to ask you to repeat this... but, you want a clean entry point to observe the issue. There may be another mysql server running as well, or it may not be serving where you think it is. By logging into MySql as root, you should be able to determine some of the mis-configuration issues.
I went to /Applications/MAMP/bin/phpMyAdmin/config.inc.php and updated the line $cfg['Servers'][$i]['host'] = '127.0.0.1';
from 127.0.0.1 to localhost.

MySQL: 10061 error but only via PHP

I'm finding myself unable to connect to a MySQL server on my LAN using PHP, and only using PHP. Here's a breakdown of the situation.
My server is at 192.168.0.999 (well, except the 999 is in the 0-255 range of course). It's an Ubuntu server, where I have MySQL 5.5 running that I installed via apt-get.
I am using PHP 4 (no, unfortunately I cannot switch to 5 at this point) on Windows 7
I am connecting using mysql_connect() (and again, no switching to another API is not an option at this point). mysql_error() says: Can't connect to MySQL server on '192.168.0.999' (10061)
I can telnet to 192.168.0.999 port 3306 and although PuTTY gives me an error about packet order, there's quite clearly a MySQL server listening on that port.
I can connect by starting cmd and typing mysql -h192.168.0.999 -uusername -p, (of course my username is not actually 'username') and after I type the password I can run queries.
I've triple-checked the credentials in my PHP script for typos, so I am now quite completely stumped. I understand that error 10061 is a network error and that either MySQL or the box it runs on, is refusing my connection, but I cannot reconcile this with the fact that I can telnet to the very same host, and can connect easily with the standard mysql client on my machine.
Before anyone brings it up, AFAICT I've followed the troubleshooting steps in the MySQL manual to a T and the PHP manual does not seem very helpful. Copying the database to my local machine is very logical, and very desirable, but also very impractical.
Can anyone help me?
You've stated that you are running this on windows, and that you can access MySQL through command line but not PHP.
This sounds like permissions problems - are your table InnoDB by any chance?
If so, check the permissions on the InnoDB database files.
As it turns out, I was connecting to the right host, but to the wrong port. For reasons that are not really relevant to the question, my MySQL instance runs on port 3307 instead of 3306, so I've set mysql.default_port to 3307 in my php.ini file.
Mystery solved: PEBKAC!

Phpcloud and Mysql Workbench connection

I have been struggling how to connect Mysql Workbench with Phpcloud.com Mysql instance since friday ? Below there is error which I get image
Anyone has did it working properly ?
The error message about the initial package usually indicates there was no server found at that address and port. Keep in mind that due to the SSH tunnel the remote server appears like it is a local one. The remote end of the tunnel sees the server locally too, since it is on the same machine as the MySQL server (I assume). So what you need is the localhost address for the MySQL server. The other thing to check are the ports: 1) Does the SSH connection really work with port 22? 2) Is your firewall blocking this port? 3) Is the remote MySQL server really listening on port 3306?
As a side node: on Windows you can usually just press Ctrl+C in error messages to copy the message to the clipboard.
Please look at the second section "Managing your Database Instance" at http://www.phpcloud.com/help/accessing-the-db
You need to establish a tunnel using SSH client (putty will be best in your case) and then access the server on port 13306 (local).
I don't use Workbench but I assume it tries to establish a regular SSH connection and not a tunnel and this will not work.

ADO and msqli connections very slow

I am experiencing a very slow ADO and mysqli connection for my production web server. The current software setup is windows 2008 server R2 Standard Edition SP1, Apache 2.4, PHP 5.3.10, MySql 5.5.24, Pear 1.94, Zend Engine version 2.30.
I've profiled the code using XDEBUG and it shows the initial connections taking around 1200ms each (regardless of page being visited), whereas on my local development machine and another test server the connections only takes around 8ms. The code for the website is all in sync through SVN except for the php, pear, mysql, and apache ini and conf files. I've done diffs on these to check for differences and there aren't any. The DB contents are a complete copy as well. Everything for the production server is hosted on the same machine so there aren't any firewall or internet issues.
The first connection profile has the following call stack:
ADOConnecton->Connect
ADODB_mysql->_connect
php::mysql_connect
The second one:
php::mysqli->mysqli
Any suggestions?
Usually the slowness in (first) connection depend on DNS resolution.
May be:
client to resolve the server name
server to resolve the client name to match an access rule
let the client/server let know the server/client address using the host file:
http://en.wikipedia.org/wiki/Hosts_(file)
I edited the Mysql my.ini to change the mysql service to only bind to the IPV4 loopback adapter.
[mysqld]
...
bind=127.0.0.1
I also changed the \public_html\conf\face.ini to use the IPV4 loopback address instead of local host. (Changed "localhost" to "127.0.0.1")
After that all issues went away. I'm not sure if it is because the machine has a half dozen IP addresses or its trying to decide whether to use IPV6 or IPV4.

Categories