0

I have got a cPanel hosting and I can create and log in to the phpmyadmin and change databases after I log in to the cPanel. What I need to do is log in to the same database even if my files are not on the same server, so I need to specify the exact database URL instead of "localhost". How do I get that URL?

Kumar Priyansh
  • 68
  • 2
  • 10
  • If your PHP scripts connect via `localhost`, then the host is the same as your web server. You will need to configure the MySQL user to be able to connect remotely though. See https://stackoverflow.com/questions/14779104/how-to-allow-remote-connection-to-mysql – Phil Aug 27 '18 at 01:50
  • But It's a hosting from a third party provider. It's not my local machine. After logging in the web address of phpmyadmin looks like this https://subdomain.hostingprovider.com:2083/cpsess6259331642/3rdparty/phpMyAdmin/index.php – Kumar Priyansh Aug 27 '18 at 01:54
  • If you were to put some `.php` files on your CPanel host and they connected to MySQL, what hostname would you use? `localhost` or something else? – Phil Aug 27 '18 at 01:59
  • Does this help? https://au.godaddy.com/help/view-your-database-details-for-cpanel-hosting-16021 – Phil Aug 27 '18 at 02:00
  • 'locahost' means local to the server, most hosts will have that working. your host may not allow for external db access - some cheap ones do that. Cpanel itself has a section for allowing remote db connections its name and location depends on the theme and version. you should check your hosts documentation aat this point, its not really possible to give you a definitive answer here –  Aug 27 '18 at 02:18

1 Answers1

1

The most secure way to access a remote database server is to setup SSH access on your database server and a SSH Tunnel on the web server that is hosting your files.

See: Connect to a MySQL server over SSH in PHP

This way you don't need to expose your database server to the internet, and instead can access it from the localhost of your web server.

If the database is exposed to the internet, you can assign a domain name to your database server and use that domain name, or subdomain, as the host and create a wildcard user like root@'%' but that is far less secure.

Ralph Ritoch
  • 3,260
  • 27
  • 37