Connection to database remotely from a server to another differently server - php

I have webhost from ABC Incorporate.
I bought a mysql database from XYZ Incorporate.
When I try to acces via PHP, the MySQL server from XYZ Incorporate, using the ABC Incorporate's server, it gives me this error:
connection refused in
I tried to talk with those two incorporates, and they said that.
No IP restriction was given, so I could've easly connect.
My question is, why is connecting?
My config.php
<?php
DEFINE ('DB_USER', 'yes');
DEFINE ('DB_PASSWD', 'somepassword');
DEFINE ('DB_HOST', 'IPv4');
DEFINE ('DB_NAME', 'dbname');
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWD, DB_NAME);
if(!$con){
die('Database connection error');
}
?>

allow database host to connect remotely.It will be accessible.

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 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');

Connect to Cloud SQL in Google App Engine using mysqli_connect

I am trying to configure my Wordpress site in the Google App Engine standard environment. I have configured a Cloud SQL for MySQL Second Generation instance and can access it using Cloud SQL Proxy.
The problem I have is connected to the Cloud SQL instance after the app has been deployed to the Google App Engine (GAE) environment. Here are two different connection strings; one for the GAE environment and another for the local environment.
if (isset($_SERVER['GAE_ENV'])) {
$dbConn = mysqli_connect (null, DB_USER, DB_PASSWORD, DB_NAME, 3306, DB_SOCK);
} else { // local environment
$dbConn = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
}
Local environment
The connection string for the local environment works perfectly when I use these 4 parameters: DB_HOST, DB_USER, DB_PASSWORD, DB_NAME. When I try to use the same connection string with four parameters in the GAE environment, it throws an error:
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known
GAE Environment
In this question, I was told to call mysqli_connect using all 6 of the optional parameters, with null for parameter 1 and 3306 for parameter 5, being the port. When I try to make the connection using the GAE connection string with these 6 parameters: null, DB_USER, DB_PASSWORD, DB_NAME, 3306, DB_SOCK, I get a slightly different error:
Fatal error: Uncaught mysqli_sql_exception: No such file or directory
Here is the code from wp-config.php which sets the connection string variables depending on the environment:
if ($onGae) {
/** GAE Environment */
define('DB_NAME', 'database');
define('DB_HOST', ':/cloudsql/project_id:region:instance_id');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
} else {
/** Local environment */
define('DB_NAME', 'database');
define('DB_HOST', '127.0.0.1');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
}
No matter what I try, I can't seem to get mysqli_connect to connect to CloudSQL from GAE. Can someone please tell me what I am doing wrong? Thank you.
I suspect the problem is the colon in the path of your socket is preventing it from recognizing it is an absolute path. Try define('DB_HOST', '/cloudsql/<project_id:region:instance_id>'); instead.
A couple of other tips: You can use the Cloud SQL proxy to create a local unix socket at /cloudsql/<CONNECTION_NAME>, which means you can test the same code deployed and locally.
Also as general programming advice, I would try to avoid using DB_HOST for different types of values (because they really aren't the same). This will help you from inadvertently using $DB_HOST somewhere else and expecting an IP address. Try something like this instead:
define('DB_NAME', 'database');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
if ($onGae) {
/** GAE Environment */
define('DB_HOST', null);
define('DB_SOCKET', '/cloudsql/project_id:region:instance_id');
} else {
/** Local environment */
define('DB_HOST', '127.0.0.1');
define('DB_SOCKET', null);
}

php - mysql database connection

I'm trying to connect to my database to retrieve the table from sql workbench, but when I do it comes up with the following error message
could not connect to MySQL Unknown MySQL server host "Guada"
The php script is as follows:
<?php
//Using details to establish database connection
DEFINE ('DB_USER', 'i7421760');
DEFINE ('DB PASSWORD', '*********');
DEFINE ('DB_HOST', 'guada');
DEFINE ('DB_NAME', 'i7421760');
$dbc = #mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die('could not connect to MySQL ' .
mysqli_connect_error());
/*
If the connection couldn't be established it
will print a display message and the error report
*/
?>
If it is the case of just getting the host name wrong, where can I find it? The table I want to retrieve is on sql workbench.
Any help is appreciated
thanks in advance
Update your code:
//Using details to establish database connection
DEFINE ('DB_USER', 'i7421760');
DEFINE ('DB PASSWORD', '*********');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'i7421760');
$dbc = #mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die('could not connect to MySQL ' .
mysqli_connect_error());
/*
If the connection couldn't be established it
will print a display message and the error report
Is it possible your php and MySQL both run on a hosting service somewhere?
Many hosting services configure their internal networks so MySQL can be reached from php running on their internal servers, but not directly from customers' machines outside the hosting service networks. If you must access your MySQL directly from your home or office, ask your hosting service tech support for help.
But keep in mind that you're running a security risk by opening up your MySQL database to access by the broader internet.

I get connection error when I add my database name in connection

Warning: mysqli_connect(): (42000/1044): Access denied for user '-----'#'localhost' to database '----' in ---.php on line 4
Unable to connect to MySQL
$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die("Unable to connect to MySQL");
This is how I connect. If I remove DB_NAME from there, I don't get an error and connect database but whenever I add DB_NAME, I get this error. DB_NAME is definitely correct but I don't see why? By the way for DB_HOST, I use localhost.
---Solved---
I finally solved the problem. So, for those of you who have the same problem, I am gonna explain everything. Actually, I decided to use table name before my query and than mysqli gave me another error saying something like "INSERT command denied to user ..." So, I opened up phpMyAdmin and changed my DB_USER to the name that is written in Database Server section in phpMyAdmin. You will see something like this;
Server: Localhost via UNIX socket
Server type: MySQL
Server version: 5.5.42-cll - MySQL Community Server (GPL)
Protocol version: 10
User: -HERE-
Server charset: UTF-8 Unicode (utf8)
So you need to use use the username, which is written in User part. After I changed my DB_USER I was able to add DB_NAME. Basically all the problems I was having (not being able to use DB_NAME and not being able to use sql commands) caused my permission.
The key part is; I was using DB_NAME, which I used to create database in my server and it also let me connect to the database but As I can see it is not the root. So, if you are having "denied" error, make sure that you check the values you typed in mysqli_connect!
You can use
# mysqli_connect(hostname, username, password, database)
or
#mysqli_connect(hostname,username, password)
# mysqli_select_db(true, database);
or
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno())
{
echo "Failed : mysqli_connect_error();
}
mysqli_select_db($con,"db");
if you are using localhost then try this
<?php
$con = mysqli_connect("localhost", "root", "", "YOUR_DB_NAME");
if (mysqli_connect_error()) {
die ("Could not Connect");
}
?>
and if you are doing this online then replace localhost with host_name root with username "" with password and at last your DB_Name. I hope this would solve your problem :)

Categories