0

I am trying to access a database which is in root@localhost from a website hosted by godaddy.

i created a php file in the project folder and trying to access the local mysql database

here is the code

//actionpage.php

<?php

$db=mysqli_connect("127.0.0.1","cancan","canpass","test");


if (!$db) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";

?>

it gives me the error on the browser

Connection failed: Access denied for user 'cancan'@'localhost' (using password: YES)

The website i created is just static html without a back end..

I am pretty new to back end developing , Please help me

  • Possible duplicate of ["Connect failed: Access denied for user 'root'@'localhost' (using password: YES)" from php function](https://stackoverflow.com/questions/6445917/connect-failed-access-denied-for-user-rootlocalhost-using-password-yes) – Anurag Srivastava Mar 15 '19 at 16:40
  • 1
    When you say "local" do you mean "On the same server as the HTTP service" or "On your development machine"? – Quentin Mar 15 '19 at 16:41
  • 127.0.0.1 is a **local** loopback; essentially it's an alias for *this server* so, no, by definition that won't connect to a remote server - you'd need its WAN IP plus the access and permissions set up correctly. – CD001 Mar 15 '19 at 16:42
  • "Connect failed: Access denied for user 'root'@'localhost' (using password: YES)" from php function – Anurag Srivastava this doesnt solve my problem i have granted all permissions through myphpadmin – Neena Susan Mar 15 '19 at 16:53
  • @Quentin on my local machine, is it possible to access a local database from a remote server – Neena Susan Mar 15 '19 at 16:54
  • @CD001 i have granted all permissions for user databae and table ,, i dont have a backend for my website i am trying to create one using a simple login form – Neena Susan Mar 15 '19 at 16:56
  • @NeenaSusan - but it *sounds* like you're trying to access a database on a **different machine** (e.g. your local one, not the one on GoDaddy) ... which isn't a great idea tbh, but it can be done. – CD001 Mar 15 '19 at 16:59

2 Answers2

0

So long as Godaddy doesn't block outgoing MySQL connections and your ISP doesn't block incoming MySQL connections, this is technically possible.

You would need to:

  1. Configure MySQL to listen on your computer's external network interface (i.e. not on a UNIX socket nor on the loopback IP address).
  2. Ensure that the MySQL service is available on the Internet (which will probably mean configuring your Internet router to perform port forwarding for the MySQL port from the Internet to your development machine)
  3. Change the connection string in your PHP so it connects to the hostname or IP address of your computer on the Internet (probably your router's external IP address).

You'd probably want to ensure you have a static IP address on the Internet so that you don't need to update the PHP every time your IP changes.


This, however, will be slow (because your MySQL traffic has to go back and forth across the Internet) and risky from a security perspective.

So don't do it. Get a MySQL service from Godaddy.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • if i get a mysql service from godaddy , i am i going to create a database in there panel ? can i use the same query to connect it with my server or how does it work ? – Neena Susan Mar 15 '19 at 17:20
  • Is it ok to save my php files in the project folder?? i saved the php file with html files and accessing the php file from one of the html forms – Neena Susan Mar 15 '19 at 17:27
0

GoDaddy can't access database which is hosted on your computer. For that to be possible your computer must have a Public IP address, It's only that way you can connect website hosted on GoDaddy to your local database and for that you must reference you local computer by their Public IP address and not as localhost within your phone script.

When you use localhost in script host on GoDaddy It will not reference your computer but the server on which that script is hosted

Yves Kipondo
  • 5,289
  • 1
  • 18
  • 31