MySQL database target machine actively refuses connection - php

I've been trying, for the past few days, to get a script on one server to connect to a MySQL database on another server. I've looked all over to see if I could find a solution and I've made little progress. What I'm doing is reading from a local database, then using that information to write to the desired database (which has a table of the same name). The code is as follows.
require_once($documentRoot."/Classes/DBHandler.php");
$db_controller1 = new DBHandler();
$query = "SELECT ...";
$temp = $db_controller1->select($query);
//Convert temporary database contents into usable result
while($rowObject=mysql_fetch_object($temp)){
$result[]=$rowObject;
}
//Create database handler to handle target database
//Server actively refuses this connection. Wrong IP address or user?
$db_controller2 = new mysqli("ip address:3306", "user", "password", "DB name");
if($db_controller2->connect_errno){
$errorMessage = sprintf("'%s'", $db_controller2->connect_error);
echo $errorMessage;
}
else{
$db_controller2->query("DELETE ...");
//Processing of data goes here
foreach($result as $item){
//Create query to insert individual records
$start = "INSERT INTO ...";
$end = sprintf("remainder of insertion query here");
$start .= $end;
//Database handler uses $start to insert record
$db_controller2->query($start);
}
$db_controller2->close();
}
Everyone I've talked to has agreed that the IP address itself is correct. What I find weird is that when I view the database after logging in through cPanel, the URL shows a port number of 2082 but checking database variables gives me a port number of 3306. All of the SQL queries are correct and there's nothing wrong with the PHP code. The errors I've been receiving differ depending on which port number I use. For 3306:
Warning: mysqli::mysqli(): (HY000/2002): No connection could be made because the target machine actively refused it. in C:\etc.\script.php on line 30
'No connection could be made because the target machine actively refused it. '
When I use 2082, I get a different error message:
Warning: mysqli::mysqli(): MySQL server has gone away in C:\etc.\script.php on line 28
Warning: mysqli::mysqli(): Error while reading greeting packet. PID=[PID here] in C:\etc.\script.php on line 28
Warning: mysqli::mysqli(): (HY000/2006): MySQL server has gone away in C:\etc.\script.php on line 28
I'm completely at a loss of what to do. Most of what I've seen seems to indicate that this is a server-related problem and I'd need to either create a new user with remote access permissions or modify an existing user to permit remote access, but for whatever reason, I can't do either of those things because there is no option for remote access.
Does anyone have suggestions? Connecting to the target database is no problem for any files on its same server.
Note: The "line X" messages are incorrect since I slightly modified what I posted here for confidentiality reasons. However, they're referring to the line where I create the new mysqli object.

On the machine running the target MySQL instance....
What is in your my.cnf file?
Probably here:
# vi /etc/my.cnf
Make sure that skip-networking is commented:
# skip-networking
Make sure bind-address is bound to the correct IP address:
bind-address 65.55.55.2
Lastly, check your firewall.

Related

Issue connecting to MySQL with mysqli in PHP [duplicate]

This question already has answers here:
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'#'localhost' (using password: YES)
(25 answers)
Closed 6 years ago.
Been going through tons of answers but none have exactly matched my issue.
I have a MySQL database running and am attempting to connect in PHP with mysqli. The connection code is as follows:
<?php
if(!isset($_COOKIE["uid"])) {
$servername = "localhost";
$username = "study";
$password = "somepassword";
$dbname = "user_study";
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error){
die("aww");
}
}
However every time I get the error
mysqli::mysqli(): (28000/1045): Access denied for user
'study'#'localhost' (using password: YES)
I know that the user 'study'#'localhost' has permissions to access this database, because I am able to access it through the MySQL command line just fine. I have tried other accounts such as roots with the same result.
Is there anything else I should be checking?
New information (2/23)
It seems that even when I shut the database down I'm getting an access denied result, meaning it seems to be trying to connect to some other database on the server. How would I ensure it is connecting to the correct one?
I think the error message indicates that PHP was able to contact the MySQL Server using the socket file. (It would have been a different error otherwise.)
There's a couple of reasons you could get this error. If we can successfully connect to MySQL Server with a the mysql command line client like this:
> mysql --no-defaults -h localhost -u study -psomepassword user_study
That's going to rule out a lot of the possible reasons for the failure.
The most reasonable explanation for the error message from PHP is that the password being provided in the connection attempt from PHP does not match the password MySQL is expecting.
Some ideas we should be able to rule out. Privileges on the user_study database have been granted to 'study'#'localhost', e.g.
GRANT SELECT ON user_study.* TO 'user'#'localhost'
On a totally different tack, given the assignment statement:
$password = "somepassword";
And assuming that you wouldn't be supplying the actual password in the question... we're left wondering if the actual password contains characters that are subject to PHP string interpretation, such as backslash character, or a dollar sign.
For debugging, I suggest doing an echo $password; following the assignment, and verify that the string emitted is what is expected.
Another possibility is that there isn't an exact match in the mysql.user table, and the user 'study' is actually matching to a different mysql.user... an entry with an empty user ''#'localhost'.
I'd be taking a look at all of the entries in the mysql.user table where user='study' and user=''.
I also want to rule out the possibility that the mysql command line client using a .mylogin.cnf file.
I'm also tempted to suggest that changes were applied to the mysql.user table and a FLUSH PRIVILEGES statement wasn't executed... but that doesn't jive with the behavior (successful connection) observed in the mysql command line client.
We're assuming obviously that the MySQL Server is running local, on the same machine that PHP is executing on. And we're expecting to connect via the local socket file.
As a test, I'd suggest connecting via TCP. Specifying host as 127.0.0.1. That would require a different entry in the mysql.user table. We'd test connection from the mysql command line client:
> mysql --no-defaults -h 127.0.0.1 -u study -psomepassword user_study
But this gets into a whole host of other configuration issues with the MySQL Server, networking enabled, bind address, DNS name resolution, listening port, iptables, firewall, et al.
--
If the problem was an unsupported authentication protocol, I'd expect a different error. If the problem was the inability to connect to the socket file, I'd also expect a different error.
All of the usual causes for this error seem to be ruled out by a successful connection from the command line client, running on the local machine, connecting to using the same credentials.

Remote mySQL connection error

I posted this question earlier but only provided a link to the output instead of posting the output here. With the additional details, I trust there will be enough info.
This script works locally on my server but when loaded on a remote server the error shown after the code below is given... Can someone tell me why it works locally and not remotely. A % wildcard has been set for allowed hosts.
<?php
$version_link = mysql_connect('gjinternetsolutions.com', 'gj_Guest', 'Password1');
mysql_select_db("gj_Software", $version_link);
if (mysql_errno())
{
$error = "<p>MySQL error ".mysql_errno().": ".mysql_error()."\n</p>";
die($error);
}
$version_query = "SELECT * FROM `VersionCheck` where `Software`='RedemptionFee'";
$version_result = mysql_query($version_query);
$version_row = mysql_fetch_array($version_result);
if (mysql_errno())
{
$error = "<p>MySQL error ".mysql_errno().": ".mysql_error()."\n<br>When executing:<br>\n$version_query\n</p>";
die($error);
}
$new_version=$version_row['Version'];
$new_URL=$version_row['URL'];
echo "The latest version of the Redemption Fee Module is: ".$new_version;
?>
This is the error that is given from the above script...
Warning: mysql_connect(): Can't connect to MySQL server on 'gjinternetsolutions.com' (4) in /home/nicedayh/public_html/CheckRemoteMySQL.php on line 3
Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/nicedayh/public_html/CheckRemoteMySQL.php on line 4
MySQL error 2003: Can't connect to MySQL server on 'gjinternetsolutions.com' (4)
At first I thought something was wrong with my script but after seeing it work perfectly on the local machine, I am not certain. Several people have tried loading it remotely and had no success so it is not just one remote machine that has an issue.
I think you have to put :3306 after your database location or whatever port your MySQL is on
Are you able to ping gjinternetsolutions.com?
My server in New Zealand recognises this host name, however my server in the US does not. Your A records may have not propagated to your computer yet, or your name servers NS1.GJINTERNETSOLUTIONS.COM and NS2.GJINTERNETSOLUTIONS.COM might be having issues.
A. Is your mysql database listening on the same server/IP that gjinternetsolutions.com is hosted on since that is what you are using to connect to?
B. If it is, is mysql service running on the db server?
C. If it is, is port 3306 open on the firewall to let traffic through?
D. If it is, is your my.cnf file on the db server set to localhost only using bind-address = 127.0.0.1? If so, comment that out.
E. If it's not, do you have mysql database setup to accept connections from a remote server i.e. not localhost?
http://www.rackspace.com/knowledge_center/article/mysql-connect-to-your-database-remotely
or if you use a cpanel
http://my.kualo.com/knowledgebase/1059-how-to-set-your-whm-server-to-use-a-remote-mysql-server.html
One of these is probably the issue. This at least should get somewhere to look.

mysqli::mysqli(): (HY000/2002) localhost not working

I am trying to connect to my data base and i get an error when I use 127.0.0.1 instead of localhost.
Warning: mysqli::mysqli(): (HY000/2002): 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.
If I use localhost instead of 127.0.0.1 it works fine. Anyone have any insight into this?
the code used to connect is
<?php
$db = new mysqli('127.0.0.1', 'root', '', 'dbname');
?>
Check the sql configurations. I'm going to list a few that should be checked that could cause this option. I do recommend sharing what version of php and sql you are using. Are you using LAMP? It is the same problem listed in the above comments. Newer versions of LAMP have changed the default to 127.0.0.1 for localhost. This is why it comes across as a different problem and you do need a different approach to fix it.
1) if my.cnf has a line reading skip-networking comment it out.
2) check the ect/hosts.allow file make sure there is a line that says
mysqld: ALL
3) check the ect/hosts.deny file and make sure there is only 1 line stating
ALL:ALL
Let us know if that fixed the problem.

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

PHP/MySQL simple connection won't work

I'm hosting a website on Zymic (free host) that utilizes MySQL. I opened an account, and wrote the SIMPLEST function to connect to the DB. It looks like this:
<?php
$conn = mysql_connect("uuuq.com","paulasplace_sudo","mypassword");
if(!$con)
{
die("Could not connect: " . mysql_error());
}
else
{
echo("mysql connected successfully!");
}
?>
but it throws this error:
Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 111 in /www/uuuq.com/p/a/u/paulasplace/htdocs/index.php on line 9
Could not connect: Lost connection to MySQL server at 'reading initial communication packet', system error: 111
Any ideas what might be wrong?
This may be just from when you copied the code into your post, but you store the results of mysql_connect() into $conn, but the if statement checks a different variably $con...
For those like me that had this same problem with a virtual machine or where you have all the rights (priviliges as read in phpmyadmin) to make users, the problem for me was that my user for the database was created incorrectly . While creating my DB user i set incorrectly one the parametres right after the name there is an selector where you can choose the server: chosen as default was my error, that says "any server" and is set as % , i had to change it , infact , i had to create a new user with the same name and different server, there i set it to localhost. My problem got solved , i deleted my other user, restarted the server and it worked fine.
I suspect the hostname is "localhost" and not uuuq.com.
Often times these free hosts have their MySQL servers at different addresses altogether as opposed to simply localhost or the site address itself.
If the host provides access to phpMyAdmin, open that then look at the top of the page and you should see something along the lines of
Server: s1.mysqlserver.com
That is the address you want.

Categories