PHP - Connect to different servers database - php

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.

Related

MySQL - access denied until I connect via Workbench or CLI

I'm having issues with intermittent connection issues from my localhost PHP site to my localhost MySQL DB. It gives me back Access Denied for 'user'#'localhost' (using password: YES) as is typical for a wrong password. I tested to make sure it wasn't anything particular, and I could get it with root, so I was confused. I even set up a 127.0.0.1, a localhost and a % account with the same username and password. None of them worked, but root worked just fine. That's been my biggest confusion - 'root' working with no issues, but this new user not working at all.
I've looked through so many of the problems (see below) and issues laid out on SE and other forums relating to Error 1045, Access Denied and all variants, and most boil down to a misspelling, or a typo, or something of that nature. I've copy and pasted every relevant string around all of my code, so that's not the issue. I don't have an anonymous user, so that's not the issue. There's no DNS issues, it's localhost and I've tried every iteration of that.
The issue comes from what solves the issue for me. If I open a connection to the DB using the new account via MySQL Workbench or CLI, suddenly the webpage will work with no issues, connecting via the new account with no problems. But if I run the script that changes the new account information whatsoever, even if I change nothing, and reload the page, it will deny the access again until I open that connection up again via MySQL Workbench or the CLI.
It works without fail, every single time. I have no idea what's going on - does the account need to be logged into via CLI or Workbench before they can be used via PHP? Or is there something more I am missing here?
PHP Code
$servername='localhost';
$username='website';
$password='password';
$database='db_1';
$conn=new mysqli($servername, $username, $password, $database);
if($conn->connect_error) die("connection failed");
MySQL Credentials Creation
create user website#'localhost' identified by 'password';
grant SELECT, INSERT, DELETE on db_1.* to 'website'#'localhost';
A few of the SO questions that didn't help:
MySQL - Access denied for user
access denied for user # 'localhost' to database ''
"Connect failed: Access denied for user 'root'#'localhost' (using password: YES)" from php function
EDIT 1:
I am running a WAMP stack on a local computer. I have my firewall open for port 3306 (the port MySQL is listening on). I am not using CPanel (I think that's what it's called anyways). And I just tested and it doesn't work with root either until I go ahead and recreate a new connection to the DB via the CLI or Workbench.
The error it's giving back also includes this, but I've never had this come up as I've never tried to install OpenSSL on either before: mysqli::__construct(): PHP was built without openssl extension, can't send password encrypted in [file]. But this goes away as soon as I make the connection via the CLI (no fancy tags or anything, so I don't think I'm using SSL) or via Workbench.
Edit 2: The rabbit hole went deeper and then stopped but I'm still confused as to how to configure my set up properly to avoid it in the future/fix it to be more secure. The solution (which I can't put up as my own, so if one of you wants to explain and then claim this, go for it - I'll accept it) was to go back to my MySQL server and reconfigure the Authentication Method to not use the Strong Password Encryption and instead opt for the Legacy Authentication Method. If someone could explain how to make my PHP compatible with the newer authentication method (I haven't looked yet but will be after I post this) I would be grateful.
The solution was to go back to my MySQL server and reconfigure the Authentication Method to not use the Strong Password Encryption and instead opt for the Legacy Authentication Method.

Remote SQL connection: mysqli_connect(): (HY000/1045): Access denied for user

I have a database that I want to transfer to my remote server from localhost. The file being too big, I wrote the code to copy the table. But the problem the connection is not successful. My code looks like this.
$server_conn=mysqli_connect($db_host,$db_user,$db_pass,$db_name);
if (!$server_conn) {
die("Server Connection Fail: " . mysqli_connect_error());
}
Where $db_host is the ip address of my server.
When I load the script the error I get is
Server Connection Fail: Access denied for user db_user#'117.202.126.83' (using password: YES)
The credentials are correct, the host is correct, and the weirdest part is 117.202.126.83 is my IP address.
I don't know how is it ignoring the host and taking my connection IP address for host. Even when I give the db_host as the IP address of my remote server.
Also, I am running the script using Easy PHP 14.1 running APACHE 2.4.7,MYSQL 5.6.15 and PHP 5.4.24.
What am I missing?
UPDATE: I am not sure if I was stupid or if the hostgator instructions were.
They ask us to add the cpanel username followed by an underscore before the database name and database username.
In the cpanel, the cpanel username is prefixed by force to all user and db names. So in my case, I had the variables as cpanelUsername_cpanelUsername_databaseName and so on.
Thank you guys for your replies
Go to the cpanel where your phpMyAdmin has the access.
Click on Remote MySQL
Add the access host (Your local IP which is running the script)
Then you can get access to remote mysql server.
You can then test the connection using MySQL Workbench ()
are you sure you use the proper external database hostname/ip?

Access MySql Databases from same Network or Remotely access from Different Servers

I have an application in which I have to access all pc connected to same network and their MySql Databases and I also want want to connect to remotely a server.
Actually I have list of drop down services and each service holding a database name. when I select a service then I want to build connection to database either it lies on same network or any remote server.
Remember, I know the hostname, username, password and dbname. and I am using mysqli_connect function.
I have try multiple options given on web, but all in vain. e.g grant host and user access. But not found any solution can help to solve my problem.
I have try bind-address option in my.conf file but no solution.
Here is my code
For remote Server
$con = new mysqli_connect('xxx xxx xxx:3306', 'username', 'pass', 'dbname');
For local Network
$con = new mysqli_connect('xxxx xxx xxx:3306', 'localhost', '', 'talent');
Error
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. and also get this one earlier Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'#'localhost' (using password: YES).
I am using windows platform. WIndow 8.1
I guess you want to access mysql database installed on different computers on same network as your computer and also on some remote server.
What you need is IP Addresses of all the computers on which the mysql server is installed including the remote server. And on each such computer mysql server remote access should be enabled for your computer's IP Address and mysql user you are using in 'new mysqli_connect()'. See this tutorial.
Also as fvu said check php documentation for 'mysqli_connect' function.
UPDATED
What is SSH?
1. SSH is SecureSHell.
2. Its nothing but a remote login tool or program(like telnet but in secured way).
3. Remote login is logging in to some other computer(known as remote computer) as a user of that computer from your computer.
4. After you successfully logged in to the remote computer via ssh, you can type commands on that remote computer on behalf of the user you are loggen in with.
5. Consider this as if you are sitting in front of your computer and watching the command prompt(terminal in linux and mac) screen of the remote computer.
6. Whataver you will type here will reflect there.
For your knowledge to make you understand the problem:
1. When you install XAMPP or WAMP on your computer it also installs MySQL Server with it.
2. MySQL Server is a process running in the background to which we can request to do
database operations like SELECT, UPDATE, etc.
3. This thing we generally do using 'mysql_connect' or 'mysqli_connect' in php.
4. MySQL Server can have many users and different users have different previledges/permissions. So that MySQL admin user(i.e. root) can control what things are allowed and not allowed for a user.
5. Now while connecting to the mysql server process we need to specify the user credentials. That you specify in mysqli_connect function.
Now let me explain you why you are getting that error:
1. The user credentials you are using either does not exist or not correct or the user has no access to connect to MySQL Server process remotely. i.e. from other computer that the one has MySQL Server installed on. In your case from your computer to computer A or to computer B or remote server.
2. The other reason may be the firewall settings of remote computers.
Solution:
1. For computer A and computer B you don't need ssh you can directly go to the computer and open command prompt on it and type commands.
2. But for remote computer you need to use ssh.
3. Now another problem is you are using Windows so ssh program will not be available to you.
4. You will need to download putty program. Its ssh implementation for windows. Same as ssh only name is different.
5. Take a putty tutorial to connect to remote server.
6. Then you can use this tutorial for granting remote login access to mysql user you are using in mysqli_connect function.
If the server is a local hosted database and you can connect to it from local host and i you are not able to connect to it from a remote machine consider the following options.
There is a firewall in ubuntu that you need to open, you do this by granting access in IPtables.
/sbin/iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT And now we should be able to login to our server from our local machine:
mysql -h255.112.324.12 -uroot -pMyPASSWORD
You need
to grant access to mysql: https://askubuntu.com/questions/159053/mysql-server-not-accessible-from-remote-machine
as root, open your /etc/mysql/my.cnf with your favorite editor look for the [mysqld] section, and in there for the bind-address keyword. This usually is set to 127.0.0.1 -- change that to match your "normal" IP-address save the file, and reload the service (e.g. using service mysql restart)
Last but not least you need to give remote access in mysql:
GRANT ALL ON mydb.* TO root#'%' IDENTIFIED BY 'MyPASSWORD';
Find more information here: http://web.archive.org/web/20120930214828/http://chosencollective.com/technology/how-to-enable-remote-access-to-mysql
Right now you seem to be mixing object-oriented and procedural approaches. If you want to use object-orented approach, mysqli object should be instantiated as follows:
$con = new mysqli($host, $username, $password, $dbname, $socket); // all but first parameter are optional
If you want to use procedural approach, mysqli connection resource should be created as follows:
$con = mysqli_connect($host, $username, $password, $dbname, $socket); // all but first parameter are optional

Access remote database MySQL via PHP

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.

php access to remote database

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;

Categories