Connecting to MySQL on a different server - php

I have two servers (virtual machines - I can remotely connect to these) - server 1, and server 2.
On server 1 I keep my webpages, and on server 2, I keep the databases.
I am currently trying to connect to a database on server 2 from server 1.
Here is my php code:
<?php
$dbhost = 'xxx.xx.xx.xx:xxxx';
$dbuser = 'xxxxxx';
$dbpass = 'xxxxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql' . mysql_error());
$dbname = 'database';
mysql_select_db($dbname);
?>
This is the error message I get when I try to connect:
Access denied for user 'xxxxxx'#'server1' (using password: YES)
I found it a bit puzzling that it says #server1 considering I'm trying to connect to server 2. Can anyone offer any insights?
Thanks
PS: They're both on windows 2008

MySQL factors in the host name of the connecting client when determining whether or not to grant access. A given username may be allowed to log in from the same machine that is hosting the database (i.e. localhost) but not allowed to connect when used from a remote system.
In your case, it sounds like user 'xxxxxx' is not authorized to connect from 'server1'. You could grant user 'xxxxxx' login rights from all hosts ('%'). However, it would be more secure to limit your that account's login rights to the specific host ('server1') or range of hosts ('%.mydomain.com' or '144.155.166.0/255.255.255.0') from which it needs to be used.
For more information on this aspect of MySql's authentication process, see http://dev.mysql.com/doc/refman/5.0/en/connection-access.html.

"Access denied for user 'xxxxxx'#'server1'" means that server1 don't have access to server2. You need to add new user on server2 which can connect from server1 (hostname: server1);

You need to give rights for that machine
Check it here

Remove this user and create a new one with 'xxxxxx'#'%'. Do this on both servers.
#'%' indicates that the user can connect from anywhere.

Related

Cannot connect to MySQL database on remote server using php mysqli

I am trying to use mysqli to insert some data into a MySQL database (let's call the schema myDatabase), but cannot successfully connect. Here's the code snippet to connect:
...
$config = parse_ini_file('../includes/config.ini');
$username = $config['username'];
$password = $config['password'];
$dbname = $config['dbname'];
$server = $config['server'];
$conn = new mysqli($server, $username, $password, $dbname);
if (!$conn || $conn->connect_error) {
die( 'Connection Failed: ('.$conn->connect_errno.') '.$conn->connect_error);
}
...
I get the following result:
Connection Failed: (1045) Access denied for user 'myUser'#'my.laptop.ip.address' (using password: YES)
Here's some details on the set-up, in case they are relevant:
The code is on my laptop running Windows 7 and using PHP 5.3.5 that came with xammpp.
The database is hosted on a remote server with MySQL5.1.52. I created a user to which I granted all privileges on myDatabase.*. No host was specified for the user (e.g. 'myUser'#'%'), as I am still in development and don't know the ip address where the code for the live application will be hosted.
If I ssh onto the database server, I can connect to mysql using the credentials for myUser and access the tables in myDatabase. I have another schema on this same server which is accessed by a different user, and have been able to use mysqli to connect without any problems.
Just to be sure it wasn't a typo, I dropped the user, and created it again, copying and pasting the username and password from the config.ini file used in my php code (and flushed privileges, of course). I did this again, except this time the host was specified, e.g. CREATE USER 'myUser'#'my.laptop.ip.address' IDENTIFIED BY 'myPassword'. I keep getting the same error and now I'm completely stumped.
Help, please.
On your mysql machine hit:
GRANT ALL PRIVILEGES ON database.* TO 'myUser'#'%' IDENTIFIED BY 'newpassword';
FLUSH PRIVILEGES;
This will allow the user to connect from any host. Once it works, you can limit it to just a specific host and database.
Okay, this is strange, but it appears the problem had to do with the password I was using. The original one contained some special characters ($, & +). When I changed it so that it only contained numbers, letters and underscore, it worked.
Is this real, or did I accidentally do something else without realizing that turned out to be the actual solution?

How do I properly connect to a mysql database?

Here is my code:
<?php
require_once('config.php');
$link = mysqli_connect($db_host, $db_user, $db_pass, $db_name) or die ('Your DB connection is misconfigured. Enter the correct values and try again.');
?>
I have stored my host, username, password and database name in the separate file config.php. I know that the information is correct because I can connect to my database via putty, but I keep getting the error:
Warning: mysqli_connect(): (HY000/1045): Access denied for user
'blank'#'T9AF3.WPA.blank.Ca' (using password: YES) in
C:\xampp\htdocs\temp2\index.php on line 3 Your DB connection is
misconfigured. Enter the correct values and try again.
Note: 'blank' is there to protect my identity and is not a typing error.
Edit: I only used putty to test that my login information was correct. Putty has very little to do with my actual question.
Edit2:
<?php
$db_host = 'host';
$db_user = 'user';
$db_pass = 'pass';
$db_name = 'dbname';
?>
These are filers.
if you can login from putty, by that i think you mean localhost, and you can't from php file, which is i think trying to connect remotely, the problem most probably lies on your user not defined for your servers ip. your server, which you are trying to connect from is 'T9AF3.WPA.blank.Ca'
in mysql a username has a password and a location and mysql uses both to authenticate the user. you can have permission to connect locally but not remotely i.e. from another server. check with your system admin to make sure you have proper access defined from 'T9AF3.WPA.blank.Ca'.
Or you have not specified an IP Address for the server to listen to. It may only be listening on localhost of your server, instead of allowing remote connections. Check your mysql configuration and which IP address it is listening on.
Your mysql user must have proper rights on the database you are using

PHP Localhost Application connect to server database

I want to ask about how to connect my localhost application (C:xampp/htdocs/myproject) to database at my server host (www.someweb.somedomain)?
Am I possible to do that? If it is, how to connect it at my php config? Right now my config (server.php) is:
<?php
$host = "my web IP public:3306";
$user = "root";
$pass = "";
$db = "dispatcherDB";
$conn = mysql_connect($host, $user, $pass) or die ("Cant connect to mySQL");
mysql_select_db($db);
?>
what I got:
Warning: mysql_connect(): No connection could be made because the target machine actively refused it. in C:\xampp\htdocs\XMS\server.php on line 7
So, what must I filled for $host? I'm trying using website IP, it still can't connect. Maybe there's someone here have experience at this problem?
Sorry for my bad English
If you have cPanel access to the remote server then you need to mention that from which ip addresses it should allow access to MySQL..
In cPanel you will get Remote MySQL under heading Databases:
Clicking Remote MySQL will give you the option to add hosts from where you want to allow connections to your MySQL server:
Also, you can check localhost without specifying port number as value of $host..
Probably Mysql server is not allowed root access from remote servers.
you could check this post

mysql_connect() using http protocol

I am trying to connect to a MySql server (not localhost) from my computer using the code below. It is giving this error:
Warning: mysql_connect() [function.mysql-connect]: [2002] Connection refused (trying to connect via tcp://10.6.3.6:3306) in on line 7
I wonder if we can use the http protocol to connect instead of tcp that is being used by default? I searched quite a bit on how to change the protocol, but most of the answers were describing how to connect to localhost, and not much about how to connect to another server. Please help.
PS: I am able to connect to the server by going to http://10.6.3.6/phpmyadmin/...). So I am sure the server is up.
My Code
<?php
$db_hostname = '10.6.3.6';
$db_database = 'db_user11';
$db_username = 'db_user11';
$db_password = '########';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database, $db_server)
or die("Unable to select database: " . mysql_error());
?>
You can't. HTTP is not a protocol suitable for this.
You are talking about 2 processes communicating here (MySQL Server and your app) and they either do it via shared memory, pipes or sockets. Those are the 3 ways that processes communicate with each other.
First of all, HTTP is layered on top of TCP/IP.
In order to connect to something via HTTP, it needs to run an HTTP server. MySQL does not run an HTTP server and there's no current/realistic/supported way to tunnel an SQL connection through HTTP. Even if there was, HTTP is not exactly the best protocol for this.
In short: no. You're trying to solve the wrong problem. You need to configure your MySQL server to allow connections from other machines over the network, give the user you're trying to connect with appropriate permissions to connect from other machines and make sure the MySQL server is reachable from other machines.
Just because the phpmyadmin is hosted on that server, does not neccesarily mean that the database server is in the same IP address. Please ask the domain administrator to give you the details of database server.
Maybe just check and make sure the mysql user account you are connecting with has proper permissions set for remote access.
In other words, this may be obvious but make sure the user has valid remote credentials.
On the command-line ( if you can relate to this ) , one can do this in mysql to add a user allowed to connect from remote connexion:
mysql> CREATE USER 'monty'#'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'#'%'
-> WITH GRANT OPTION;
This would create a user, monty with password some_pass, with FULL privileges on all databases, all tables from anywhere. The % is the key here, and is needed for remote access.
MySQL users created through gui tools often set the host to 'localhost' and this would not allow a remote connexion.
Hope this helps. Good-luck.

Creating a Secure Connection to a Server using PHP

I am trying to query a database that's on a server that I have a Remote Desktop connection to. What is required to be able to query a database on this server using my php web application.
You need:
Database server application configured to allow connections from external hosts
Server-side network configuration tuned to allow connections from external hosts (firewall, NAT, ...)
Database user, which is granted access to database you are going to use
PHP application, which connects to your database server under appropriate user
Details depend on what database server are you using.
$link = mysql_connect($host, $username, $password);
There is nothing secure at all by the way.
I use something like this, but it's far from secure. I keep it in a seperate "connection.php" that are required once by other files.
$db_host = 'INSERT IP HERE';
$db_user = 'User';
//My sql password for testing purposes
$db_pass = 'PASSWORD';
//name of the database
$db_name = 'dbname';
//connection
$conn = new mysqli($db_host, $db_user, $db_pass ,$db_name);
if (mysqli_connect_errno())
{
printf("Error connecting to DB, Errorcode: %s\n", mysqli_connect_error());
exit;
}
use answer by Merch member - the php code above .
You need to grant privileges and to create user on server you are retrieving the database data from.
Connect to mysql :
in shell type (if you are root type root or whatever username):
mysql -u root -p
enter password,
when you are logged in :
create user someusername#your-client-ip identified by 'password';
grant all privileges on *.* to someusername#your-client-ip with grant option;
now you can use php code above to connect to remote server database (ip you just used to create mysql user your-client-ip)
When you are creating php variable for mysql connect - host use mysql port in the variable if just ip not connects your-client-ip:3306
good luck

Categories