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
Related
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?
I have some troubles to connect to a remote mysql server.
This is the situation :
I have 1 MySql server which is working (Installed phpMyAdmin, and can do actions on it).
I have 1 webserver
I try to access to my mysql server with the mysql_connect(); function :
$host = 'my_ip:ext_port';
$user = 'leUser';
$pass = 'lePassword';
$mydb = 'leDb';
$db = mysql_connect($host, $user, $pass);
Where ext_port is the port I open in my MySql server and which redirect to the port 3306.
I also give to my user the access with :
grant all privileges on *.* to 'leUser'#'%' IDENTIFIED BY PASSWORD '*lePasswordEncrypted' WITH GRANT OPTION
Tried a lot of things in vain (As comment the line bind-address in my.cnf, etc ...) but still get the error :
Can't connect to MySQL server on 'xxx.xxx.xx.xx' (4)
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
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP - connecting to mysql database from different server
global $host, $username, $password, $database;
$host = "meshtelecom.com";
$username = "mesh_project";
$password = "password";
$database = "mesh_database";
$link = mysql_connect($host, $username, $password);
if(!$link)echo "Connection failed.";
$db = mysql_select_db($database)or die(mysql_error());
I made a database with phpmyadmin in meshtelecom.com. I can't access this database from another server.. it's giving me:
Connection failed.Access denied for user ''#'localhost' to database 'mesh_database'
Maybe your host must set to localhost, you can check this in your web panel of your hoster.
You have to create a new account for the user for accessing the host from other servers.
You currently have user 'mesh_project'#'localhost' but you also need to have 'mesh_project'#'%'.
Or if you want to restrict which hosts can be used to connect to the database, you can use hostname or IP instead of %.
Here's some documentation http://dev.mysql.com/doc/refman/5.5/en//adding-users.html
If you can log in from command line, run these commands:
CREATE USER 'mesh_project'#'%' IDENTIFIED BY 'password';
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON mesh_database.* TO 'mesh_project'#'%';
Change 'password' to something else and add only privileges you want the user to be able to perform. Giving too much power to be accessible from any host increases security risk.
Double check your host, in most cases websites and databases are on the same machine try localhost or ask your host provider for the database connection info.
the script of connection =
<?
$server = "192.168.0.167";
$username = "root";
$password = "";
$database = "dbbook";
mysql_connect($server,$username,$password) or die("Koneksi gagal");
mysql_select_db($database) or die("Database tidak bisa dibuka");
?>
and i had add this script on my.cnf file after [mysqld] =
bind-address=192.168.0.167
but it didnt work with the following caption
mycomputer.mshome.net is not allowed to connect to this Mysql server
Please help me.How to remote database mysql XAMPP from another computer ?
You will have to create a new user in MySQL that is allowed to connect from a remote host.
By default, root can only connect from localhost.
You could try running the following commands from the MySQL server (make sure to substitute 192.168.0.99 with the IP or hostname of the PC that will be connecting) In your case, try 'php'#'mycomputer.mshome.net' for the user:
CREATE USER 'php'#'192.168.0.99' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON dbbook.* TO 'php'#'192.168.0.99';
It is possible to specify a wildcard host ('php'#'%'), but then if anyone got your MySQL password they could connect to the DB. You could also wildcard your subnet ('php'#'192.168.0.%') which is a little safer.