local server connection to remote mysql - php

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

Related

Unable to connect to SQL server using mysqli_connect, but MSSMS connects fine

I've read many questions similar to this but none of the answers have been relevant to me. I'm trying to access a SQL server using PHP. Both the SQL server and PHP are running on my Windows 10 machine. Here is my PHP code (the username and password are arbitrary):
$connection = mysqli_connect('127.0.0.1', 'hmuuser', 'password');
This causes the following error:
PHP Warning: mysqli_connect(): (HY000/2002): No connection could be made because the target machine actively refused it.
Here is my connection using MSSMS (which is successful):
EDIT:
I was able to solve my problem using this link: http://blog.citrix24.com/configure-sql-express-to-accept-remote-connections/
Using this link I enable SQL Browser, and set a static port to connect to SQL server with. However, I'm presented with a new error:
PHP Warning: mysqli_connect(): MySQL server has gone away in C:\HMUAPI\Index.php on line 7
PHP Warning: mysqli_connect(): Error while reading greeting packet. PID=9920 in C:\HMUAPI\Index.php on line 7
PHP Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in C:\HMUAPI\Index.php on line 7
My code has been changed to the following to cause this error: $connection = mysqli_connect('127.0.0.1:1434', 'hmuuser', 'password');
Add a database name
$connection = mysqli_connect('127.0.0.1', 'hmuuser', 'password','dbname');
Also here is suggest - Actively refused it" means that the host sent a reset instead of an ack when you tried to connect. It is therefore not a problem in your code. Either there is a firewall blocking the connection or the process that is hosting the service is not listening on that port. This may be because it is not running at all or because it is listening on a different port.
I guess the problem is, that you use MySQLi to connect to a MSSQL server.
You should use the correct MSSQL extension instead.
Microsoft published their SQL-Server for PHP driver on GitHub: https://github.com/Microsoft/msphpsql
Or you use the older extension which is documented in the PHP manual: http://de2.php.net/manual/en/book.mssql.php
And at least you can set up ODBC.
One of them should resolve your problem.

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.

Remote MySQL Connection in PHP

I'm beginning to migrate a software project from being a desktop application to a web application. Currently I am using a local PHP/MySQL connection that is associated with the desktop it is installed on.
I'm hoping to untangle this and am trying to create a MySQL database through my 1and1 account. I had no trouble creating a database and recorded my account information. I'd like to be able to edit this database using PHP scripts on my system. However, I haven't been able to get a working connection string going. When I run the following PHP script:
$hostname="db*********.db.1and1.com";
$database="db*********";
$username="dbo*********";
$password="*********";
$link = mysql_connect($hostname, $username, $password);
I get the following error when I run the script in my webbrowser:
Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'db*********.db.1and1.com' (11001) in C:\Program Files\xampp\htdocs\Remote_MySQL\rightbid_database_initialize.php on line 74
Connection failed: Unknown MySQL server host 'db*********.db.1and1.com' (11001)
How do I get my local system to recognize the 1and1 server host? Am I structuring my connection string correctly? Is it possible to access a remote server from a locally hosted Windows PHP connection?
Thanks!
I assume what's happening if that 1&1 are blocking any connections coming from outside of their network.
Try connecting using
mysql -h HOST -u USERNAME -p DATABASENAME
Good luck
Make sure the MySQL server on 1and1 will accept connections from remote clients. Not just on localhost, and also make sure port 3306 is open to the server.

PHP 5.3 Upgrade - Can't Connect to MySQL Server

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.

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.

Categories