PHP: Save data from hosted site to localhost - php

I am running on a hosted site where all my files and databases are stored in my hosting service.
Problem is, whenever a user saves data, it stores that data in my online database. Is there a way for me to save a copy of that data on my local server in order for my local server to access the new data for other purposes like sending sms?

Are you using MySql? If so, you may take a look at MySql Replication:
http://dev.mysql.com/doc/refman/5.0/en/replication.html

If you can access your hosted database (you have FTP to your service, and check out the db configuration), you could create a PHP, which connects to your hosted database, retrieves your data, then connects to your local database (if it has public IP/hostname, and the database port is open (firewall?), and put all your data there.

You should just use remote MySQL connection. You should be able to connect to your server database directly from your local environment by simply providing the hosted DB IP address in your PDO Connection settings.
<?php
$serverIp='123.123.123.123'
$dsn = 'mysql:dbname=testdb;host='.$serverIp;
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}

Related

How to access mysql server within a server?

I am running mysql on a linux server. I have the linux server IP and mysql runs on localhost on that linux server. Now how do I access mysql running on localhost server remotely?
I can do this by ssh into linux server then running mysql, but how would I do it with in a php script? For example...
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username,
$password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
(this script only connects to mysql directly which is not possible remotely)
In my script I would need to access linux server then mysql server unless there is a way to access mysql directly (remotely). How is this achievable?
You need change bind address in my.cnf and restart your mysql service.
Official documentation:
https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_bind_address
If the address is 0.0.0.0, the server accepts TCP/IP connections on all server host IPv4 interfaces.
By default all connections restricted localhost.
In order to access your mysql instance from your server; you need to install workbench (or some other mySql explorer) on your development machine.
Create a connection to mysql instance using server ip address as host name and provide username and password to open connection. If every thing goes well you will see your databases in explorer.

Errors connecting to MySQL database from alternate domain

I have two domains on two separate servers, foo.com and bar.com.
I have a website and MySQL database setup on foo that I want to migrate to bar, but bar doesn't have MySQL.
As a solution, I'm moving all the files across but leaving the database on foo, and connecting to it remotely.
I'm currently connecting like so:
connect.php
$hostname = 'database.foo.com';
$username = 'username';
$password = 'password';
$dbname = 'database';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
}
catch(PDOException $e){
echo($e->getMessage());
}
This works fine on foo.com but when I migrate this file to bar.com I get this error:
SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'database.foo.com' (110)
I can have the two sites, foo.com and bar.com open in two windows, and one will work while the other doesn't - despite the fact that they are both connecting (or trying to connect) to the same database.
Why is this happening and how can this be rectified?
The database server is not configured to listen on a network port accessible to the other server.
There are several reasons why this might be.
It is typical to only listen on local sockets and not use TCP/IP at all since that is more efficient and secure.
The port might be firewalled off from the other machine.
The server might only be accessible on the local network and foo.com might be a different network with no route from bar.com to database.foo.com.
suppose you have database on server B and website on server A(say it has IP 192.234.23.12)
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.23.12
You need to enable the Remote MySQL access on foo.com by going to,
foo's cpanel > databases > remote mysql, and adding the bar's domain to white list it.
That should work.

Using IP address instead of 'localhost' as hostname in mysqli connection

I am perhaps being a bit overly cautious asking this and apologies if it has been but I want to be as secure as possible.
Is it just as secure to use my hosting accounts IP address instead of localhost when I connect to the database via my mysqli connection below? Would the speed of connection be dramatically affected at all?
$hostname = "localhost";
$database = "wwwcapco_crm";
$username = "wwwcapco_user";
$password = "cKUsaf#&^0";
$connect = new MySQLi($hostname, $username, $password, $database);
if ($connect->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli- >connect_errno . ") " . $mysqli->connect_error; }
The main reason I ask is because at the moment I am exporting my live website database and importing it into my local website testing enviroment (xampp). This takes ages and I just thought it may be a better idea to connect directly to the live database.
Thanks
Is it just as secure to use my hosting accounts IP address instead of localhost when I connect to the database via my mysqli connection below?
No. See the documentation:
All other information is transferred as text, and can be read by anyone who is able to watch the connection. If the connection between the client and the server goes through an untrusted network
Would the speed of connection be dramatically affected at all?
Since you are getting the data from a computer at a remote site instead of locally — yes, it will.
local website testing enviroment (xampp). This takes ages and I just thought it may be a better idea to connect directly to the live database
Risking your live data by exposing it to your test code is a very bad idea.

MySQL remote access trouble?

I have a database set up on a godaddy server. It is configured to allow remote access, and there are a couple of websites I'm running which need to access this data. It works when accessed from another godaddy site, and I can connect from my development environment both at work and home. We recently set up hosting with mydomain.com.
Here is the code block that triggered it:
function connect(){
$servername = "XX.XX.XXX.XX";
$dbusername = "databaseusername";
$dbpassword = "mahpassword";
$dbname = "databasename";
try{
$newMysql = new PDO("mysql:host=".$servername.";dbname=".$dbname, $dbusername, $dbpassword);
}
catch(PDOException $e){
echo 'connection Failed: '. $e->getMessage();
die;
}
}
and now I'm getting this error message on the new site:
connection Failed: SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'XX.XX.XXX.XX' (111)
The only problems I can think of is that either for some reason there are a limited number of IP addresses the MySQL database will connect to by default (which seems squirrely), I'm getting blocked by a firewall on the MySQL server (again.. doesn't make sense to me), or there is some setting on the mydomain hosting server disallowing remote requests (?)
I'm new to this kind of thing, so I'm open to any suggestions. I could probably just set up another database on the new site, but I don't want the hassle of keeping them synchronized if I don't need to. What might be wrong? Are there any workarounds?
[edit]
connected to remote database via console (mysql -h XX.XX.XXX.XX ...), the privileges were found under the information_schema database, a quick select * from SCHEMA_PRIVILEGES and select * from USER_PRIVILEGES shows that 'databaseusername'#'%' has sufficient privileges. Not that it helped me any, but maybe it'll help someone down the road.
[/edit]
As it has been more than a year since I asked this question, I suppose I need to answer it just to close it.
It turns out that godaddy had blocked mydomain.com servers via firewall ("Remote access" was limited). so in order to accomplish what I wanted to do, I had to copy and store the database on both sites.

how to connect to database on another server

Could I have my php scripts on server A and connect to the MySQL database on server B?
If yes, how it would be done? Thanks in advance
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
Yes.
The same way you access the localhost on the same server, you change the database host to the external one. This is more a configuration issue, you need to grant your database user remote access to your MySQL, you also need to make sure your firewall allows connections on the MySQL port.
Example on Debian: http://www.debianhelp.co.uk/remotemysql.htm
Yes it can be done.
Find out the IP address of the server A where your scripts will be uploaded. Do not forget to change the localhost to the ip address of the Server B in mysql_connect() or mysqli_connect() method.
Now go the control panel of the Server B where your Database is.
In the control panel's Homepage go the databases section and click the Remote MYSQL option.
Then add the Ip address of the Server A and click on add host.
Now you can access to the database in Server B while your scripts are running in Server A.
Mind you the fetched result will be slow cause it is getting data from database that is located on another server.
Your welcome
Just don't the hostname of the other box for the connection. Details depend on the extension you're using:
$mysql = mysql_connect($host, $user, $pass);
$mysqli = new mysqli($host, $user, $password, $schema);
$pdo = new PDO("mysql:host=$host", $user, $pass);
Make sure that the user is allowed to access by the MySQL server (CREATE USER) and check that there's no firewall in the way.
That is all what you need .
(Even you can have your scripts on server A, your web server on server B and your database on server C ...)
Have a look here:
http://us2.php.net/manual/en/function.mysql-connect.php
You can either pass in the server hostname as an argument, or configure in php.ini.
I was having similar challenges but here is what work for me:
To connect to server B from server A, First, you need to allow remote MySQL access hosts in cPanel (Server B), Home -> Databases -> Remote MySQL and also whitelist the IP in the firewall (That is IP Address of B server). Then the following php db connection should work.
$db_connect = mysqli_connect("serverB.com", "dbuser", "dbpassword", "dbname");
// Evaluate the connection
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
exit();
}else{
//successful connection
echo "Yes, successful";
}
Its a perfect solution for connecting another database from other servers.
$dbserverName = "191.238.0.2";
$dbUsername = "lauranco_L2L";
$dbPassword = "SL92TIW5T96L";
$dbName = "lauranco_siteBits";
Good old thread.
Still - of all the answers appearing here, nothing addresses about the security.
It is highly insecure to open up the mysql port to outside the server.
The most secure option is to keep the mysql port open to one and only localhost in all servers.
And have another php running inside the second server, make it create the desired output and deliver the same to your php (running in the first server).

Categories