Connecting to multiple databases on different servers - php

I want to connect to localhost and a remote server at the same time.
The goal is to synchronize data on the two servers, as an INSERT is being
done on the local server,it is also being done on a similar table on the remote server.
The remote server is given by an ip address, lets's call it "12.345.678.999" .
In my database config file, I have something like this:
<?php
// for local server
$DB_NAME = 'dbname1';
$DB_HOST = 'localhost';
$DB_USER = 'dbuser1';
$DB_PASS = 'dbpass1';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno())
{
printf("Database connection failed: %s\n", mysqli_connect_error());
exit();
}
// for remote server
$DB_NAME2 = 'dbname2';
$DB_HOST2 = '12.345.678.999';
$DB_USER2 = 'dbuser2';
$DB_PASS2 = 'dbpass2';
$mysqli2 = new mysqli($DB_HOST2, $DB_USER2, $DB_PASS2, $DB_NAME2);
if (mysqli_connect_errno()) {
printf("Database connection failed: %s\n", mysqli_connect_error());
exit();
}
?>
So I have two different mysqli objects to use in my scripts to run queries for local and remote server.
On loading the local site, I get the following error :
Database connection failed: php_network_getaddresses: getaddrinfo
failed: Name or service not known
What is the proper way to achieve my objective of connecting to two servers at the same time?

This looks an improper way to do this. You should create the Replication between this two servers.
If you insert data in Master server, It will automatically be reflected in the slave.

The error message indicates that the DB Server cannot be found. This either means that the IP-address (the real one you are using for the remote server) is incorrect, or that the DB configuration files on the remote server are not correct. I am guessing, but if the IP-Address is correct, then have a look at:
remote_server://application/config/database.php
Ensure that the server is specified as 'localhost' rather than something else.

Related

Connect database from online PHP site hosted in Heroku

I created a php website and host it online by using Heroku recently. However, I couldn't connect my database because I use localhost phpmyadmin database from my online site. How should I connect my localhost database from my online site? Or how to make my host my database online, and then make the connection? Here is my db.php file to make the connection with mysqli, in case if there's any need to tweak the db connection PHP codes.
//step 1: Establish database connection
DEFINE("DB_HOST", 'localhost');
DEFINE("DB_USER", 'root');
DEFINE("DB_PASS", '');
DEFINE("DB_NAME", 'my_db_name');
// Create connection
$con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// Set charset to UFT8
mysqli_set_charset($con, "utf8");
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
If you are hosting your database on your local machine, whilst having a webserver that needs to connect to your database, you will have to make sure your database can be accessed by another machine. The host is only ‘localhost’ if the Local IP is identical for the machine and database. Your db.php is correct and will function properly, as long as you ensure that the connection details are correct, since localhost is not correct there will be errors.
You might want to look into how to portforward depending on what database software/installation you are using. Or look into whether your host offers database solutions sch as or similar to PhpMyAdmin (in case of most webhosts).

How to make db connection in php of on other server A to sever B

How to make db connection my php scripts on server A and connect to the MySQL database on server B?
$conn = new mysqli($servername, $username, $password);
This should work just fine.
$conn = new mysqli(223.122.212.2:90, 'root', 'root');
where 223.122.212.2 is ip to server B, running mysql server and listening on port 90. Make sure that server B is running mysql on port 90 (or any other port you'd like) and firewall, if any, configured in a way that allows remote connection.
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
You need to do few steps:
On server B, Make enable remote access to database
On server A, db connection file make host => IP of server b

Connecting to a MySQL db created in an Ubuntu AWS instance

Ok so I created an ubuntu instance from AWS and downloaded LAMP on it as you would in a regular Ubuntu OS. When I try to connect to the db from my php script it's rejecting the connection. saying "Connection failed: Connection refused". So I guess my question is this: Does amazon not allow you to connect to a db without using their RDS database service or am I putting something wrong here? (I've hidden some of the data for security purposes, the ... are numbers of my instance). When I put "ec2-34-...-..-64.compute-1.amazonaws.com" into the browser the apache message comes up so I don't see why this is not working as a server name?
$servername = "ec2-34-...-..-64.compute-1.amazonaws.com";
$username = "root";
$password = “hidden";
$dbname = "questions87";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
You need to change your EC2 Security Group Inbound Rules related with 3306, you can find that on the AWS FAQ. Then, you need to be sure that your user#YOURIP have all permissions on your MySQL database. For your Inbound Rules it is recommended to use a Custom Rule to your IP, not All Traffic.
Reference and further reading:
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html

Failing to access a Godaddy hosted MySQL server from AWS EC2

I have a MySQL server hosted in my Godaddy server. I want to access that database from my EC2 instance. However, I am unable to do so. I have added the public IP of my EC2 instance to the remote MySQL access hosts.
Things I have tried:
By using my Godaddy hosted website name as my server name.
Using my Godaddy hosted website IP address as my server name.
Every time I try to connect, It gives me the error:
Connection failed: Unknown MySQL server host '<hostname>:3306' (11)
Script I used:
<?php
$servername = "<hostname>:3306";
$username = "<username>";
$password = "<password>";
$dbname = "<dbname>";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
PS: I can successfully connect my MySQL workbench and a locally hosted server to my Godaddy database.
Try removing :3306 from $servername
Make sure the arguments are correct. mysqli() needs port as separate argument as described here
According to godaddy you need to enable Allow Direct Database Access for remotely connections.
Link here!

How to access information from a SQL database to another server?

I have two servers, for the sake for my questions, lets name the first server A, and the second server B. Server A is a magento server that has a lot of user data, and server B is a basic web page.
I want to create an analytics dashboard in server B, that uses the SQL databases of server A.
Now the problem is having a remote connection from server B to server A.
I try to connect to server A, using this code in server B:
$servername = "104.2*****"; // Server A
$username = "root";
$password = "******";
$dbname = "magento";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
I get this error:
Connection failed: Can't connect to MySQL server on '104.2******'
Can someone please tell me what I'm doing wrong?
You may need to create a ssh tunnel to connect to a remote server hosted database if the settings on the database server do now allow unsecure connections which is likely why you're getting that error.
http://kb.mediatemple.net/questions/133/Tunnel+local+MySQL+server+through+SSH#gs
It might be that there is a firewall restriction on the server you want to connect to. The MySQL port connection might be restricted.
Try to connect from the server you run this script (or another server) to the ip/port of the server you want to connect to.
If you have access to a linux/windows console, try running:
telnet 104.2***** 3306
3306 is the default port for MySQL and telnet should work.

Categories