I have a MySQL database set up on 000webhost.com. I want to access the data in my database via PHP. I tried:
<?php
include("connect.php");
mysql_select_db("XXXXXXX_users", $con) or die(mysql_error());
$result = mysql_query("SELECT * FROM data ORDER BY id DESC");
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
$user = $row['usrname'];
}
echo "$user";
?>
But it always returns as: "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."
What do I do?
I do not have root access
I'm trying to run the script on Apache
When debugging this sort of thing, it's always good to make sure you can connect using the command line mysql tool first, to rule out issues related to PHP itself.
$ mysql -u myuser -h mysql.example.com -p
You'll get a more descriptive error message as well, e.g. ERROR 1045 (28000): Access denied for user 'root'#'remote.example.com' (using password: YES)
If the mysql port is even open on a public interface to begin with (which is usually a terrible idea), mysql itself has its own layer of access control based on the connecting host. You may need to have an account created specifically with a wildcard hostname, like so:
CREATE USER 'myuser'#'%' IDENTIFIED BY 'mypassword';
The #'%' part, specifically, allows connection from any host, local or remote.
Once you can successfully connect from the command line, it's then a simple issue of replicating your command line arguments as arguments to mysql_connect()
A lot(if not all?) of hosting providers restrict remote access to the mysql database. Meaning that you can only connect via localhost. There is probably a mysql config section on the hosting provider that will allow you to config an ip address from which a certain user can connect from. Good luck.
Most of hosts doesn't provide a remote a access to MySQL server's as it a security risk, may you should try another free MySQL server's that allow remote access, try this but its a bit slow.
Related
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.
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.
I am trying to create a simple web app that prints out database info from MariaDB/MySQL using PHP, im not even at that stage yet... I have trouble when connecting to the database and get the following error:
Host '10.155.237.62' is not allowed to connect to this MariaDB server
Now,
I have not done a secure installation so my Mariadb Server allows access from wildcard host and infact any host.I have also tried the code with the root account but received the same error. Can anyone tell me if its my code or whether its a procedure im not aware of that disallows connectors to access the information?
Here is my code:
<?php
$con=mysqli_connect("vm-007.server.com","testuser","testpass","my_db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MariaDB: " . mysqli_connect_error();
}
?>
Thanks in advance!
i would test your mysql database access from command line from remote machine
mysql -h vm-007.server.com -u testuser my_db -p
it will ask you for a password and type that in.
If it fails then you are probably dealing with a mysql setup problem.
likely you are dealing with something like the mysql daemon only listening to the localhost interface i.e. 127.0.0.1 . Even if it allows access from any host if it isn't listening to a give interface like eth0 it doesn't matter.
If you have not created a database, do it from a cmd prompt:
CREATE DATABASE blobblablob;
Now use the USE keyword for that name like so:
USE blobblablob;
Now give yourself permission to use your database:
GRANT ALL ON 'blobblablob' TO 'me'#'localhost' IDENTIFIED BY 'mypassword';
Don't forget to end all your statements with a ; (semicolon).
Now get out of the cmd prompt by typing exit.
You can check in your browser your PHP scripts. They should start working for that database now
since you have the controluser permissions.
Go to user account->add user account->create account with user name and password... let host name be %...click ok
click privileges on created user and grant global permission.
then change ur localhost name as ip:192.168.3.2 and username as newcreatedusername and password .... save it.
open your application from any other remote system along with ip:port ---192.168.1.2:9090/dashboard/working-files/welcome.php-------press enter
I am trying to connect to a MYSQL database on server A from server B. The hosting company that we are working with, owns both server A and server B. This is my first attempt at an external DB connection.
I have written the following PHP code to try to connect to the MYSQL database on server A from server B. The code looks like most other code I have Googled in regards to connecting to external MYSQL databases..
$IPAddress_O_fServer_A = 'XXX.XX.XX.XXX';
$Server_A_DB_Pass = 'P-WORD';
$Server_A_DB_User = 'U-NAME';
$con = mysql_connect($IPAddress_Of_Server_A, $Server_A_DB_User, $Server_A_DB_Pass);
Now when this code executes on Server B, I get this error:
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'XXX.XX.XX.XXX' (4) in /var/www/web12/web/dev/front-end.php on line 10
Could not connect: Can't connect to MySQL server on XXX.XX.XX.XXX' (4)
...where line 10 is the mysql_connect() call.
Does anyone see anything wrong with this code?
If the connection is not working would it be safe to say that there must be an issue with either the servers external connection permissions or some other settings?
Thanks!
First off I'd look at this and make sure that you're system is configured to allow remote access.
Secondly the user name "U-NAME" will actually be "U-NAME#localhost" and will have permissions set up as such. For example consider the following GRANT statement.
GRANT SELECT *
ON foo.bar
TO 'U-NAME'#'localhost'
The user name "U-NAME" has permission to select entries from the database foo, table bar, but it is exclusively localhost access. In order to allow remote you'd have to grant permissions similarly to the following.
GRANT SELECT *
ON foo.bar
TO 'U-NAME'#'xx.xx.xx.xx'
Help!
I have a PHP (PHP 5.2.5) script on HOST1 trying to connect to an MySql database HOST2. Both hosts are in Shared Host environments controlled through CPanel.
HOST2 is set to allow remote database connections from HOST1.
The PHP connect I'm using is:-
$h2 = IPADDRESS;
$dbu = DBUSER;
$dbp = DBPASS;
$DBlink = mysql_connect($h2, $dbu, $dbp);
This always fails with:-
Access denied for user '<dbusername>'#'***SOMESTRING***' (using password: YES)
nb: SOMESTRING looks like it could be something to do with the shared host environment.
Any ideas???
BTW: I can make remote connections to HOST2 from my laptop using OpenOffice via ODBC, and SQLyog. The SQLyog and ODBC settings are exactly the same as the PHP script is trying to use.
somestring is probably the reverse-lookup for your web-server.
Can you modify privileges from your cPanel? Have you done anything to allow access from your workstation (ODBC)?
The error-message seems to indicate that you have network-access to the mysql-server, but not privileges for your username from that specific host.
If you're allowed to grant privileges for your database, invoking:
GRANT SELECT ON database.* TO username#ip.address.of.host1 IDENTIFIED BY 'password'
might work for you. I just wrote this out of my head, you might want to doublecheck the syntax in mysql-docs.
Have you read the MySQL documentation on Causes of Access denied Errors?
Have you contacted support for your hosting provider? They should have access to troubleshoot the database connection. People on the internet do not have access.
Do you need to specify the database name? Your account might have access to connect only to a specific database. The mysql_connect() function does not allow you do specify the database, but new mysqli() does. I'm not sure if this is relevant -- it might allow you to connect but give you errors when you try to query tables that aren't in your database.
Are you sure you're using the right password? MySQL allows each account to have a different password per client host. Admittedly, this is not a common configuration, but it's possible. Your hosting provider should be able to tell you.
Just some ideas:
HOST1 does not have remote access to HOST2 (shared host is disallowing)
MySQL account does not have access from HOST1 (IP address specified on account creation, or wildcard)
Edit:
In response to your comment, I meant that HOST1 cannot get to the MySQL port on HOST2. Web services will work, of course, because port 80 is open to the public. As another user pointed out though, you are getting a response, so you are reaching it. I would try specifying the DB, and double checking the account creation command you ran.
For the second piece, I meant this: http://dev.mysql.com/doc/refman/5.0/en/adding-users.html
You can specify what host the username can connect from. If it isn't set to HOST2's IP or the wildcard, HOST2 can't log in with those credentials.
The error message means that you can contact the mySql server, but the user you are trying to log in as, does not have access.
Either the user does not have access at all, or it has access locally, but not from the host you are connecting from.
You should try to use the hostname and port like $h2 = IPADDRESS:3307;