Remote mySQL connection error - php

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.

Related

PHP - Connect to different servers database

I'm developing on 'server1'.
I need to save the data processed on this server to the 'server1' database.
However, I also need to save some data to an external 'server2' database.
The problem is that when I connect to the database of the other server I get a fairly weird error.
Error code: 1045
Error message: Access denied for user 'server2-username'#'server1-name' (using password: YES)
Why does 'server1' appear in the error message?
To me, the message says this: "hey, I can't find the 'server2-username' on 'server1' database". Am I correct? Why does say that, when I'm connecting to 'server2'?
$connection = new mysqli('server2-ip', 'server2-username', 'server2-password', 'server2-database');
if($connection->connect_errno) {
echo $connection->connect_errno;
echo $connection->connect_error;
}
Any idea how should I connect to 'server2'? Or what should I check?
This is the first time I want to connect to another server database. I haven't done this yet, and I don't know what's wrong.
This error mean 'server2-username' try to connect from 'server1-name' and not succeed.
Please check permissions user2 on server2 for connect from remote host
It demands an account which matches the connecting user#hostname, which is server2-username#server1-name... it's just that the web-server runs on the same host server1-name as the mySQLd on server1-name. This may appear confusing, but the hostname comes from where the script runs.
You'd need to add user server2-username#server1-name to the mySQLd on server2-name... and if you can't get that account set up, most commonly there's a JSON API available as a web-service; exporting/importing data to any format would also be an option, if they wish to import themselves.
SSL tunneling could even connect through local loop-back interface 127.0.0.1 on :3307 (the problem isn't bind-address = 127.0.0.1, but that there is no such user#hostname available):
shell_exec("ssh -fNg -L 3307:server2-ip:3306 server2-username#server2-ip");
$connection = new mysqli('server2-ip', 'server2-username', 'server2-password', 'server2-database', 3307);
However, either getting that remote user account setup or using an API might rather be the suggested options, because the SSL tunnel created with shell_exec() might be closed at any time, which all needs to be tested & handled accordingly. The error message would at least be server2-username#server2-name or server2-username#localhost, when tunneling into it.

MySQL database target machine actively refuses connection

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.

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_connect doesn't work on a certain host

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.

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