Connect database from online PHP site hosted in Heroku - php

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).

Related

How can I access the localhost/phpmyadmin because all day I got This site can’t be reached?

I am using windows and trying to develop an app in php where I connect a database mySQL created on mySQL workbench. I connect with root and a saved pass. But it seems I cannot connect to the page afterwards.
I use Windows 10.
<?php
define('db_user', 'what is the user?');
define('db_pass', 'mypass');
define('db_host', 'localhost');
define('db_name', 'movies');
&db_conn = mysqli_connect(db_host, db_user, db_pass, db_name);
if(!&db_conn){
die('error connecting to database');
}
echo 'you have connected succesfully';
?>
My database is called movies. I don't know what is the user?
phpMyAdmin is not a database; it is just one of the tools you use to manage what is inside. Your database is MySQL, which should be started and listening on a particular port. The default is usually 3306 or for MariaDB 3307. When MySQL server is located on the same machine as your web server, you can connect via localhost or 127.0.0.1.
If you haven't set up any users in you MySQL server, then the default usually is root with no password. Check your users using this command:
SELECT * FROM mysql.user;
The user must also be authorized for an access via either localhost or IP, whichever you use.
Then to open a connection to MySQL in PHP using the mysqli class, you would usually use this code:
<?php
// your connection code
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'root', '', 'movies');
$mysqli->set_charset('utf8mb4');

Connecting to multiple databases on different servers

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.

mysql database connects only if i run my local wamp server even though all the files are in server site

Its not connecting to my web hosting server database even though all the files are in server and all the credentials( username, password, database and host-name ) are updated! why its still connecting to my local machine database? even though i changed the username and password, its connecting to my local database itself! how's that possible?
I created another new database and tables in my server database. everything is done! but connecting to my local database!!
<?php
header("Access-Control-Allow-Origin: *");
define('DB_NAME', 'asknow');
define('DB_USER', 'myself');
define('DB_PASSWORD', '*****');
define('DB_HOST', '134.345.**');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
?>
That is because you set your host as localhost, which means your computer here.
define('DB_HOST', 'localhost');
That is why your data still goes to your local database.
Instead, you should set your database's ip on your shared hosting as your host.
---- Edited ---- (because the question is edited)
You should use your dns like www.yourdns/connect.php to link to your connect.php file in your shared hosting.

Error when connecting in phpMyAdmin

This may be a stupid question, but I've been following a tutorial online for learning php and I'm using an exercise file from it that is a sql file. The instructor set up a username and password when the database was set up in the MySQL console in WAMP. I know it works this way, but now I'm trying to test it out in a different way by importing it into phpMyAdmin on my server, but I get an error when trying to connect.
It says the database connection failed because I don't have access to the "local MySQL server", and mentions a socket that it's trying to connect through (I'm not sure what that means). I'm wondering if it has something to do with the username and password I am using. Would that carry over in the file I uploaded from the lesson? I tried using the username and password the instructor used, and then I tried using the one I use for logging on to the phpMyAdmin page, but I got the same error both times. Is there another way to set a username and password or I am completely off and the problem lies somewhere else?
Here's the code I use to establish the connection:
<?php
define("DB_SERVER", "localhost");
define("DB_USER", "xxxx");
define("DB_PASS", "xxxx");
define("DB_NAME", "tester");
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
?>
Thanks for your help!!
Edit: the exact error I get is
"Database connection failed: Can't connect to local MySQL server
through socket '/tmp/mysql.sock' (46) (2002)"

Cannot connect to MySQL database, socket error

I'm new to php, and while trying to make a connection on one of my pages to the database I set up on the phpMyAdmin page of my site.
I get this error:
"Database connection failed: Can't connect to local MySQL server
through socket '/tmp/mysql.sock' (46) (2002)"
I don't know what a socket is, or why it's trying to go to what looks like a temp file, so I don't even know where to being to troubleshoot this.
The code I'm using to make the initial connection is this:
<?php
define("DB_SERVER", "localhost");
define("DB_USER", "xxxx");
define("DB_PASS", "xxxx");
define("DB_NAME", "tester");
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
// Test if connection occurred.
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
I know I should have access to the MySQL databases, since I logged on and made them myself.
I previously set the page up by using WAMP on my computer, and everything worked fine.
It's just when I tried making it live on the site that I ran into this error.
Any help would be awesome!!
Try using 127.0.0.1 instead of localhost.
If that does not solve it, and you have root access to your server, try the following command
service mysql restart
To restart the mysql server.
The first option will probably work. Again, if you have root access to your server, you should change the mysql config to support sockets, since it's better than the TCP-ip connection.
After contacting my hosting service several times, it seems the error occurred because my hosting service recently changed their specifications and now uses "mysql" instead of "localhost" in the host and server fields. I had used "localhost" before with another host service, so I didn't think to change this, and the latest help articles on my host's website had not updated to reflect this.

Categories