I have a network of websites hosted on two different servers, server1 and server2.
I need to be able to access the data stored in a MySQL DataBase on server1 from a PHP script in a site hosted on server2.
Say a user registers on site1 on server1 and his data is inserted into the DataBase on server1. Now, that user would be able to log in on site2 on server2 with the same registration.
That would mean the PHP script on server2 needs access to the MySQL DB on server1. I don't want to copy the user data over to a DB on server2, because the user might also edit his or her user information on site1.
I'd like to be able to share this data without editing the server's data (as in http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html).
So far, I haven't been able to figure out a secure way of achieving this, does anyone know how to do this using PHP (or JavaScript, but I'd prefer the system to work for everyone, regardless if JS is enabled)?
Thank you!
You can achieve this by allowing your other servers hostname to connect to your database.
In a MySQL command prompt or phpmyadmin execute the query:
GRANT ALL ON databasename.* TO username#'ipaddress' IDENTIFIED BY 'somepassword';
http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
On server1 the script1 looks something like this:
$host = "localhost";
$user="dbuser";
$pass="dbpw";
$connection=mysql_connect($host,$user,$pass);
and on server2 the script2 looks something like:
$host = "server1.domain.com";
$user="dbuser";
$pass="dbpw";
$connection=mysql_connect($host,$user,$pass);
So you just have to enter a different host name to connect to.
Related
I know that you can access a database from a domain name with a user like this user#example.com but will this user also be able to access the database if they are trying to connect via another page on that domain i.e. example.com/page?
I am asking this as I am trying to connect from a domain name that will be establishing connections from different pages and sub folders i.e. example.com/admin/page, will setting it up with just example.com as the ending will all variables of that domain be accepted from the database connection or would each page need to be set up as seperate users on the database?
EDIT:
when I say "I know that you can access a database from a domain name with a user like this user#example.com" I mean that you can create a remote mysql user that can access from a domain name, the statement in mysql would look like GRANT ALL PRIVILEGES ON *.* TO user#example.com IDENTIFIED BY 'password' WITH GRANT OPTION; what im asking is, can this user access from all variables of example.com?
I think you are confusing a few concepts here.
The "page" as in webpage, or domain as in www.example.com are of little concern when connecting to a database.
You seem to be using mysql, so what you need to do is make sure your process that handles some web request (eg your apache server, or IIS, or whatever) can connect to the database.
eg:
If you apacheserver is running on 1.2.3.4, make sure your mysql allows connections from 1.2.3.4
The webpages themself are not important.
Yes users from different pages will be able to connect.
No you do not need to create different users for each url in your application.
The domain part of the user name is determined by MySQL doing a DNS resolution on the connecting IP. It has nothing to do with the page request to your PHP script.
is it possible to connect to another server's database with php?
Like I have Website A thats got Database A and on this website I want to load something from a Database B of Website B. Of course I got the connection data!
its simple all thise above techniques are quite complicated
suppose you have database on server B and website on server A(say it has IP 192.234.12.1)
on cpanel whitelist the IP of server B
and create a new user having sufficient privileges in database (say this user is test)
then create this user as test#192.234.12.1
Gotten from how to connect to database on another server
I have a MYSQL database with users table, and I want to make a python application which allows me to login to that database with the IP, pass, username and everything hidden. The thing is, the only IP which is allowed to connect to that mysql database, is the server itself (localhost).
How do I make a connection to that database from a user's computer, and also be able to retrieve data from it securely? Can I build some PHP script on the server that is able to take parameters and retrieve data to that user?
You should not make a connection from the user's computer. By default, most database configurations are done to allow only requests from the same server (localhost) to access the database.
What you will need is this:
A server side script such as Python, PHP, Perl, Ruby, etc to access the database. The script will be on the server, and as such, it will access the database locally
Send a web request from the user's computer using Python, Perl, or any programming language to the server side script as described above.
So, the application on the user's computer sends a request to the script on the server. The script connects to the database locally, accesses the data, and sends it back to the application. The application can then use the data as needed.
That is basically, what you are trying to achieve.
Hope the explanation is clear and it helps.
You can use below code into your python script to connect your application with remote mysql database.
import MySQLdb
myDB = MySQLdb.connect(host="x.x.x.x", port=3306, user="**********", passwd="*****************")
You need to install mysql-python to connect your python application with mysql database.
As i understood you are able to connect only with "server itself (localhost)" so to connect from any ip do this:
mysql> CREATE USER 'myname'#'%.mydomain.com' IDENTIFIED BY 'mypass';
I agree with #Daniel no PHP script needed...
Hi I'm having problem with accessing database from one of my site to another..
I have one site say a.com and I want to replicate the database from this site to my another site say b.com...
I'm using Cpanel in both the sites. In site A.com, I have created a user with required privileges and also added the other website (b.com) in the access hosts..
Now to access a.com database from b.com, I can use the username and password I have created on a.com but I'm not sure what hostname should I use..
Website a.com use localhost as the hostname..what should I use with b.com..
if you can't connect to the database then it's better to develop some kind of api. Try this
search api to use in external websites you might get some help.
Assuming:
the database is configured to be exposed on the network (if it is designed to be used only from the same machine it shouldn't be, UNIX sockets are preferred)
there is no firewall in the way
the database permissions table grants rights to users on the other server
… then it would be the ip address of the machine the database server is running on.
You should use a.com, or a decent IP address/domain name that will correctly direct the connection to the database.
However, do note you will need to configure the incoming server to accept the connection (i.e. port forwarding, firewall etc).
we can get data from another site by using $.getJSON function of jQuery
You can get detail information from
http://api.jquery.com/jQuery.getJSON sites....
So here is the situation.
I have a database on a remote server that gives out quizzes and scores for individual students...
I also have a local database which contains the students names and their respective groups.
What I want to do is to display the list of students per group then show the results of their scores from the remote database.. Is this possible??
Im currently running the script from my local computer using XAMMP like so
mysql_connect("REMOTE SERVER ADDRESS","USERNAME", "PASSWORD") or die("Could not connect to MySQL server!");
But all I get is Could not connect to MySql Server..
GRANT ALL ON database.* TO user#ipaddress IDENTIFIED BY 'password';
You should force a reload of the grant tables using:
FLUSH PRIVILEGES;
https://documentation.cpanel.net/display/ALD/Remote+MySQL# I found useful this one myselt. When you haven't define access permissions to remote db using it's cpanel, then you cannot connect with it via the localhost from your PC.
You will find something like 'Add Access Host' in your hosting server cpanel and there you can add which IPs are you going to use to access from outside. There I used as "%.%.%.%" and that means I allowed every v4 IPs to access my database. Instead of "%.%.%.%" you can put your PC's IP(for example 51.254.230.178). As PC's IP gets changed frequently, then I used this because I cannot change it manually all the time by checking my IP every time I connected to the Internet.