CodeIgniter: How to synchronize local and remote databases - php

I have a Hotel monitoring web application developed in CodeIgniter.
It monitors each room of a hotel for activities and Sends live data about rooms occupancy, status of electrical switches etc.
It hosted in cpanel web server. I have the same copy of it in my local computer.
I want to synchronize the 2 databases (ie. in localhost and in remote server).
Every change in any database should be automaticaly updated to other database.
First I tried to access my remote database from localhost by putting host name as my cpanel Shared IP Address as follos
$db['default']['hostname'] = 'xx.xx.xx.xx';
where xx.xx.xx.xx is the shared IP of cpanel. but no use.
how should I proceed?
Thanks in advance

Option 1: The Codeigniter Way
You can set up a remote access MySQL server:
Tutorial for cpanel here: https://www.liquidweb.com/kb/enable-remote-mysql-
connections-in-cpanel/
Then just set up a new connection in your database.php changing the to $db['remote']['hostname'] = 'xxx.xxx.xxx.xxx';
Do note that you'll have to make a method to pull in this new data and replace it in your local database, so a script that can be run via cronjob.
Option 2: The Master/Slave Way
You can set up a Master database, being the hotel, and a slave being your local database. I don't know of a method to do this straight cpanel, but you can do it in phpmyadmin, there's a video on how to do it here: https://www.youtube.com/watch?v=nfsmnx24gxU
This method would be the most comprehensive/automatic way to do it. It will overwrite any changes in your database though.
Note that any outside access to a database is potentially insecure. Be aware of this when choosing.

Related

Filemaker PHP Web Publishing - Synchronising not working with new host address

We have a windows server setup in our premises where we host our FileMaker server (FMS 15).
We are thinking of moving the host into a cloud server (FMS19).
We have taken the latest backup of our databases (from FMS 15) and uploaded it into the cloud server (FMS 19).
The extension of the backup file is .fm12 so it was uploaded easily from FMS15 to FMS 19. Database structure remains exactly the same and the connection from FileMaker pro is also working fine.
The Issue
One of these databases (database db_fm for example) is connected to our website. A cron job runs every 5 minutes on our website that syncs information from the website's database (db_website) to that of FileMaker's databases (db_fm). The sync uses FileMakers “PHP Web Publishing”
After uploading the latest backup in the cloud server (FMS 19) I have changed the host address in the config file
i.e; I have changed the host address from
//host for our internal server
$FileMakerHost = 'http://77.**.***.40:8080';
to
//host for cloud server
$FileMakerHost = 'https://a*****2.fmphost.com/ ;
Database structure and the password for the Filemaker database remained the same. So have not changed anything else other than the host address.
The synchronization works with the current database (hosted in our windows server FMS15) but it is not working with the new host (hosted in cloud server FMS19).
After changing the host address we have created a test subscriber. That record should be added to the FileMaker database (i.e the one hosted in cloud server). But there is no new record created.
Can you please assist me with what can be the issue? I cannot think of anything else other than changing the host address. What can I do to make it work with both servers?
FYI
The sync uses filemakers “PHP Web Publishing” The cloud server
(FMS19) is also in windows and PHP publishing engine is enabled
Our site is built with laravel.
The main problem was the new host address was https. The sync was working for HTTP address not https.
Following are things that we have done to make it work
We added an outbound rule in our webserver (where our website is hosted) on port 443.
Then we have reset the cache via ssh. systemd-resolve --flush-caches
. Because automatic redirect was cached
FMPHOST (The hosting company) suggested us, to test from PHP so that it can get any response from https://a******.fmphost.com/ or http://a******.fmphost.com/ (Use curl commands to prove port is not blocked between the web server and FileMaker Server)
Thank you

Find the Web Address for MySQL Database

I have got a cPanel hosting and I can create and log in to the phpmyadmin and change databases after I log in to the cPanel. What I need to do is log in to the same database even if my files are not on the same server, so I need to specify the exact database URL instead of "localhost". How do I get that URL?
The most secure way to access a remote database server is to setup SSH access on your database server and a SSH Tunnel on the web server that is hosting your files.
See: Connect to a MySQL server over SSH in PHP
This way you don't need to expose your database server to the internet, and instead can access it from the localhost of your web server.
If the database is exposed to the internet, you can assign a domain name to your database server and use that domain name, or subdomain, as the host and create a wildcard user like root#'%' but that is far less secure.

Problems connecting to a remote server and performing a simple db query

I have set up a website with basic HTML/CSS files and would like to take that one step further and implement a database with some PHP to perform simple queries and whatnot.
My problem arose when I noticed while setting up the database and creating the PHP files that connect to the database (on my local machine), I used 'localhost' as an argument for mysql_connect. When I then went to drag and drop my newly created HTML/PHP files along with my database into FileZilla to upload the pages onto my remote server, there was a problem connecting to the database. I have a feeling that it is unhappy with the whole 'localhost' notion - yet I don't know what to change it to.
Currently, I am using this line of PHP code to connect to my remote database (which is all located in FileZilla-land).
$link = mysql_connect('localhost','Tommy','pass')
Also, as another quick question: does my remote server have its own phpMyAdmin page? And if so... how would one go about finding it? =D
I appreciate anyone who is able to assist me in my endeavors.
Thank you!!
Log on to your database server, check the details and obtain the correct server information such as server, username and password.
Once you change the server details you shoudl be ready to rock n roll. Edit post with the host name/server name and we may be able to find it for you.
You'll have a phpMyAdmin page where you're hosting the site. Check the membership area or cPanel.
You are right in that 'localhost' in mysql_connect needs to be different. 'localhost' is a textual representation of a loopback address AFAIK - it connects to the computer it is hosted on. As you have uploaded it to your server (a different computer) it is now trying to connect to a mysql server on the server host, instead of your home computer (which holds all the data).
To make this work you'd need to get your database onto the server you are now working from, OR depending on your home setup, point it to your local database.
Whether your host has MySQL/PHPMyAdmin available to you is another matter altogether. If they have MySQL it is probable but not definite that they have a phpmyadmin interface.
Contacting the server host will yield more information. If they have MySQL, it is probably as simple as getting the required details from them, and 'backing up' your SQL on your local server and uploading it to the new server (easily done with PHPMyAdmin)
TL;DR: The database you are trying to connect to doesn't exist on the server you're connecting from.

How to update remote server application from local server application

I have a PHP/MySQL application running with WAMP on a local server. This application contains sensitive and confidential data that can only be accessed from devices on the network in the office.
However, this application generates reports that clients should be able to access from the web from an entirely separate application running on a LAMP stack.
Currently, I have the reports transferring via SFTP from the local server to the web based server.
My question is, how can I update the remote database from the local application securely, and so that the MySQL db can only be modified by the localhost of the remote application and the server running the local application?
I'm thinking about creating some kind of API that only accepts data from the IP of the local app, but I do not know the best practices for this, nor do I know how to start going about it.
MySQL provides a USER > FROM HOST > PER DATABASE > PER TABLE > PER COLUMN grant system.
Meaning that you can specify which user can connect from which host to which database,... Make usage of the FROM HOST feature.

How can I connect to MySQL database on a dedicated Windows 2003 server from EC2 based app server with PHP

I have developed a web application that uses PHP and MySQL and has all been running fine from a single development server. I now want to separate things a bit and place an app server within Amazons cloud that will receive and process uploaded files. The database is going to remain on the dedicated server but will need to be accessed by the EC2 instance.
Heres what I have tried so far...
In phpMyAdmin I added a new user to the privileges table. I gave it the elastic IP address from AWS as the host name, a new password and granted privileges to INSERT, SELECT and UPDATE.
In the PHP scripts on my EC2 based app server I included this username, password and the domain name of the location of the mysql server (example.domain.net) when connecting with mysql_connect
I have a simple test page that tries to SELECT and echo some results from the database but get the following error -
Could not connect to MySQL: Can't connect to MySQL server on 'example.domain.net' (4)
What could this mean, what have I missed, are there any other issues such as trying to do this from within EC2 that will cause other problems?
cheers all
Turns out the only other thing I needed to do was to open port 3306 on the database server to allow access. Did this by going to Control Panel -> Windows Firewall -> Exceptions -> Add Port ["SQL", 3306]
Now it works great!

Categories