How do I connect to external mysql database on heroku? - php

I created a FB app hosted on Heroku.
Instead of Heroku's database I use a mysql database. I made the chage using this code:
heroku config:add DATABASE_URL=mysql://user:pass#server:port/database_name
So far everything is ok but now I have an issue connecting to the database in my index.php file.
I don't know ho to do.
I tried to connect to the DB as I do on my website
try {
$bdd = new PDO('mysql:host=host;dbname=database_name', 'user', 'pass');
}
catch (Exception $e) {
die('Erreur : ' . $e->getMessage());
}
But I have an error in my app:
Application Error
An error occurred in the application and your page could not be served. Please try again in a few moments.
Thank you

Check whether your remote database port is opened for external connection. Look for bind-address configuration of your remote mysql server.

Related

Constructor failed on creating a new PDO connection

I'm trying to access a remote MySQL server. I created a user in the remote server and granted all the remote privileges to all hosts.
This is my code for connecting to database:
<?php
try{
$dsn = "mysql:host=MY_REMOTE_SERVER_IP;dbname=DB_NAME;port=3306";
$db = new PDO($dsn, 'USER_NAME','PASSWORD');
}catch(Exception $e){
echo $e->getMessage();
}
The code is working fine on my localhost. I even tried the code on some Playgrounds and it's working fine. But on my website, it's not working. The website does not connect to the database and always returns Constructor failed error.
The PHP version is 7.4 on my website and it supports PDO. But I don't know why it does not make the connection?
This is a picture of the error:
Just guessing: you maybe should not use the remote ip address, but localhost or 127.0.0.1 for your database host.
According to the pdo source code, this error message means the connection failed, which is not really helpful, but could mean one of these things (but not limited to):
hostname is wrong
database name is wrong
credentials are wrong
some networking issue
you name it :)
My prime suspect is the hostname, but just make sure all parameters are correct.

Connection to the database fails

I'm using PDO's as PHP extension. My project works fine in local server, but when I upload it on a live server... it throws a connection error..
Here goes my connection.php file
<?php
try {
$pdo = new PDO('mysql:host=localhost;dbname=cms','root','');
}
catch(PDOException $e) {
exit('Database Error');
}
?>
Check if the MySQL database ist hosted on the same server as the script is. Otherwise you cannot use localhost.
In addition you may add a password for the root user, which you should have configured already.
$pdo = new PDO('mysql:host=localhost;dbname=cms', 'root', 'yourpwhere');

Php Remote access to Ec2 Mysql server

Im trying to acess an EC2 mysql server from another EC2 instance using PHP.
$con = mysqli_connect('elastic_ip_host','user','pass','database');
if(!$con){
echo mysqli_connect_error(); echo mysql_error();
}
//Also trying PDO
try
{
$dbcon = new PDO('mysql:host=elastic_ip_host;dbname=database','user','pass');
$dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connected!';
}
catch(PDOException $e)
{
echo 'ERROR! '.$e->getMessage();
die();
}
Can't connect to MySQL server on 'elastic_ip_host' (13)ERROR! SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'elastic_ip_host' (13)
Using MySQLWorkbench app in my computer, Im connecting pretty fine using Elastic IP (number). Just PHP is not connecting. Checking phpinfo I see it has pdo and mysqli all ok.
Do you have any idea?
(Also Im using percona and I run that command first install that ask if I will not be able to connect remotly with root. But I created a new user as %. Thats what I am using in MySqlWorkbench program from my computer to access the database fine)
Running:
setsebool -P httpd_can_network_connect=1
As root in ssh fixed my problem :)

Can't access database server with remote connection

I'm a beginner and I'm trying to connect remotely to my MySQL database server, but still don't succeed.
I have a dedicated server and my provider doesn't want to help me.
-CentOS 6 with Parallels Plesk 12 (64-bit)-
I'm accessing my server trough Parallels Plesk. I set up a administrator and database user with "allow remote connection from any host".
I'm able to access my server with FTP and the MySQL database server locally, but not from my computer.
I have this error displaying when trying to connect with php:
Warning: PDO::__construct(): MySQL server has gone away in /Users/X/Sites/connexionBDD.php on line 31
Here is the way I connect:
define("DB_SERVER","mywebsite.com:8443");
define("DB_NAME","databasName");
define("DB_USER","MyUser");
define("DB_PWD","MyPassword");
try {
//line 31
$bdd = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME, DB_USER, DB_PWD);
}
catch (Exception $e) {
die($e->getMessage());
}
echo 'You made it';
For DB_SERVER, if using IP address, do I need to give V4 + port?
The host and port should not be specified in the same variable, split them up and define your DSN like below. Additionally, check the port is the correct one, MySQL by default uses port 3306.
define("DB_SERVER","mywebsite.com");
define("DB_PORT","8443");
define("DB_NAME","databasName");
define("DB_USER","MyUser");
define("DB_PWD","MyPassword");
try {
//line 31
$bdd = new PDO('mysql:host='.DB_SERVER.';port='.DB_PORT.';dbname='.DB_NAME, DB_USER, DB_PWD);
}
catch (Exception $e) {
die($e->getMessage());
}
echo 'You made it';

PHP: Save data from hosted site to localhost

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();
}

Categories