I've done a bit of research on this today, but nothing seems to address this issue. I recently upgraded to PHP 5.3.3 from 5.1.6, as well as upgraded MySQL to 5.5 from 5.0. Afterwards, the following code generates an error saying "Can't connect to mysql database":
$connection = mysql_pconnect($dbhost, $dbusername, $dbpassword);
if (!$connection) {
//Can't connect
die('Could not connect: ' . mysql_error());
return;
}
And get the following error:
Warning: mysql_pconnect(): Can't connect to MySQL server on '199.59.157.103' (13) in /var/www/html/ws/Cust/customerWS_1_1.php on line 19 Could not connect: Can't connect to MySQL server on '199.59.157.103' (13)
I am able to connect to the remote host via the command line, and have tried everything from resetting the password to shutting down IP Tables. I'm kind of at a loss - so any help would be appreciated.
We had a similar problem with the same error message "Can't connect to mysql database" and maybe the following points will be also usefull:
check mysql user privileges (in our case and with external connections we had the old server IP address (allow access) in the table) GRANT ALL PRIVILEGES ON tablename.* TO 'username'#'145.1.1.2';
-> reload privileges after changes (FLUSH PRIVILEGES;)
disable the SELinux parameter. In new Plesk versions (>10?) this would be set enabled automatically...
--> in /etc/selinux/config change the line that says:
SELINUX=enforcing to SELINUX=disabled
See further details: http://googolflex.com/?p=482
There is a PHP 5.3 mysql driver restriction related to old authentication scheme hash stored in the database by previous mysql server. This could cause a problem with stored passwords so the databases will be unaccessible with old passwords.
To solve the problem you should recreate the mysql users with the same login and same password.
When you say "I am able to connect to the remote host via the command line" do you mean, you are connecting to the MySQL server REMOTELY? Or that you are SSHing to your MySQL server and connecting to it from the MySQL server's command line? If the latter, it's possible that when you upgraded to MySQL 5.5 you accidentally stopped MySQL from binding / listening on a public IP. Check your my.cnf.
Otherwise: is your PHP code being run on the same server as your MySQL server? It's possible that your permissions could be off. MySQL permissions are very particular about connecting to the MySQL server via it's IP address if the client only has access to connect from 'localhost', and vice-versa.
First you should make sure that the connect parameters $dbhost, $dbusername and $dbpassword are correct. Assuming these are correct, you should check your mysql ini settings. I would assume that the bind-address is set to 127.0.0.1 or localhost, but not to a public IP address.
Also note that it's a security risk if you can connect to mysql from a public IP address. You may want to look into a solution that does not require such connection.
Related
Welcome to phpMyAdmin
Error
MySQL said: Documentation
Cannot connect: invalid settings.
Connection for controluser as defined in your configuration failed.
phpMyAdmin tried to connect to the MySQL server, and the server rejected the connection. You should check the host, username and password in your configuration and make sure that they correspond to the information given by the administrator of the MySQL server.
if you use xampp you should turn on the mysql and apache
I have allowed remote connections to mysql. I can successully connect from the console of another machine.
I have some php files on another server but I cant connect to the same mysql db from php.
This is the way I'm doing it:
mysql_pconnect("theipaddress","username","password")
or die("Unable to connect to db server");
Now bear in mind I am using the same creds which I successfully used in the console. I even tried putting the port after the ip but no joy.
Any ideas?
You must have a user in MySQL who is allowed to connect from % (any host) (see manual for details).
Have you selinux installed? Maybe selinux is not allowing apache to make remote connections? If yes, type:
setsebool -P httpd_can_network_connect=1
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
How to log with a diferent user in a MYSQL remote database?
Here's what I've done:
Logged as root in the MYSQL:
'create user 'user'#'%' IDENTIFIED BY 'password';
'grant select on *.* to 'user'#'%';
Then I setted a PHP script which connection is this one:
$con = mysql_pconnect("xxx.xx.xxx.xxx","user","password");
$selected = mysql_select_db("database",$con);
Aaaand it isn't working:
I'm using LAMP on a cloud server, by the way;
Warning: mysql_pconnect() [function.mysql-pconnect]: Can't connect to MySQL server on 'xxx.xx.xxx.xx' (10061) in D:\path\index.php on line 21
What am I doing wrong?
EDIT: Not a firewell issue;
You might want to check the MySQL documentation on this specific problem. If I had to guess, I would say that your MySQL server may be bound only to the local (127.0.0.1) address. To troubleshoot you should probably try connecting to the server using the command line MySQL client in order to get a better idea of why exactly the connection isn't being made.
Seems to me like a firewall issue. You should check if the machine hosting the MySQL server allows connection on the port 3306 from external IPs as well.
Check if the server you have to connect to has the firewall open on the port you are trying to connect... default port is 3306
you can use
mysqladmin -h localhost
to see check what is the port mysql is using
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.