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'
Related
This is my setup (the IP numbers are fictional of course):
Server A (10.0.0.1)
hosts a database called database1 with the user db_user and the password db_pass. This user has access to the database and remote connections from any host are permitted (I know it's a leak and I will fix it once it works)
Update 1: This server shows no signs of receiving the connection (like connection refused or something like that) Port 3306 is open
Server B (20.0.20.0)
hosts a PHP script which connects to the database with the following command:
$connection = mysqli_connect("10.0.0.1","db_user","db_pass","database1",3306);
My log on Server B says:
Access denied for user 'db_user'#'20.0.20.0' (using password: YES) in <path-to-php-file> in line 42
The line number matches the statement, so it is indeed the statement above which fails.
Why? I explicitly specified the IP of Server A (also tried server-a.com instead of 10.0.0.1)
Update 2:
I ran the following query via commandline as MySQL-Root and this is the output:
mysql> SHOW GRANTS FOR 'db_user';
GRANT USAGE ON *.* TO 'db_user'#'%' IDENTIFIED BY PASSWORD '<password hash>'
GRANT ALL PRIVILEGES ON `database1`.* TO 'db_user1'#'%'
Seems valid to me. What strikes me as odd is that in the log of B it shows it own address (B's address) instead of A's where the Database is located. My idea is it tries to connect to a database on server B where no MySQL user db_user exists.
Update 3:
I connected via SSH to server B and ran mysql --host=10.0.0.1 -udb_user -p and typed in the password => it worked. SHOW GRANTS FOR current_user; returned the same like on server A.
If you can't connect using mysqli, try using PDO instead. I'm not sure why, but apparently in this case PDO works.
Personally, I like PDO better than mysqli, because of named parameters instead of ?, and the ability to provide an array of values when calling PDOStatement::execute(). You may find you like it as well.
I see the command to set up a user account on mySQL is:
CREATE USER 'userName'#'localhost' IDENTIFIED BY 'some_pass';
For the localhost, do I keep that local host if I want the user to be able to insert from another ip adress that the mySQL DB is not on?
Also if I was creating a connecting class to match the above, would it look like this:
<?php
class myConnect extends mysqli{
public function __construct($hostname='localhost',
$user='userName',
$password='some_pass',
$dbname='dbName'){
parent::__construct($hostname, $user, $password, $dbname);
}
}
?>
Again I am concerned about the localhost part in the php class above. Basically the php is not goign to be on the same server as the database.
Update:
Tried the answer below but am getting this php error still:
Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2003): Can't connect to MySQL server on 'mySQLIP' (111) in /home4/m133414/public_html/myDigitalOcean.php on line 12
If php is installed on a different server than MySQL, you need to change localhost to whatever the IP address is that the MySQL sees the php server as. That's in the user creation. If you want it avaiable from anywhere, change localhost to '%'
In php, in place of localhost, put the IP address of the MySQL server.
If your PHP server has a separate IP address or host name than the MySQL server, use the PHP server's IP/hostname in the grant statement
CREATE USER 'userName'#'php.server.ip.goes.here' IDENTIFIED BY 'some_pass';
If you want users to be able to connect to your server from anywhere, you can use a wildcard:
CREATE USER 'userName'#'%' IDENTIFIED BY 'some_pass';
though this is generally insecure.
You should use 'localhost' if you want users to be able to connect to your server only from the same IP/host as your MySQL server. This would be appropriate in situations where your webserver is on the same host as the MySQL server.
Mysql is very picky about user account string formatting, and in particular treats the hostname 'localhost' as special (and exactly how depends on the version).
If the hostname is 'localhost' then many mysql versions will use a local Unix Socket (and not TCP/IP) to connect. If you use a DNS name such as mysql.server.example.org, be aware that you need to include exactly the string mysql sees on connect: it's not 'intelligent' in saying 'mysql' is the same thing, for example.
Be aware also that creating a user does not give it permission to do anything. You will normally need to use GRANT to do that as well. You can grant permission to the whole server (not recommended!) or to all tables in a database, or even to individual tables. I would strongly recommend testing using 'phpmyadmin' to investigate this if needed.
Finally, I'm just slightly worried that you are conflating user-of-mysql and user-of-application. Normally, the mysql user table is not used for application level users (e.g. website profiles). I say this because DB user creation is often a one-off thing and so doesn't need application code to do it...
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.
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.
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;