81735

Connect to an online MySQL database from localhost

Question:

I am trying to connect to my MySQL database on my online server via PHP on my localhost.

$con = mysql_connect("$hostname", "$username", "$password"); if (!$con) {die('Could not connect: ' . mysql_error());} mysql_select_db($databasename, $con);

When I upload the script to the server it connects to the database fine. However, when I load the script on my localhost it will not connect to the online database. I shut off my firewall to make sure it was not blocking it.

Is the connection above formatted correctly to do this? Any other suggestions?

Answer1:

Your MySQL server needs to have your IP address (or % as wild card) specified in the allowed hosts.

Answer2:

Does the host allow this? Many won't let external clients connect.

Answer3:

As the others say you need to allow that particular host (in this case your IP) to connect. This is in your user table in the mysql database. If you don't have access to this database (or don't have grant privileges) then you can't change this.

Answer4:

Your code is ok.

As others stated the MySQL server has to allow connections from remote machines. Also make sure your provider's firewall isn't blocking the request and double check the hostname, because it may differ from the one you use on your server.

If you didn't set up your server yourself, you'll probably have to speak with th user support of your hoster, because they have the permissions to change settings and more accurate info on this topic.

Answer5:

It is time to move to mysqli_* or PDO

<a href="http://php.net/manual/en/book.mysqli.php" rel="nofollow">Documentation</a>

Recommend

  • JOIN same table twice with aliases on SQLAlchemy
  • How to solve CORS problem of my Django API?
  • Why no memory leak?
  • Django: copy data from one database to another
  • jquery FullCalendar: changing the format of the day title in weekview
  • Load JavaScript and CSS files in folders in AngularJS
  • The Current .Net SDK does not support targeting .Net Core 2.2 Target .Net Core 2.1 or Lower [duplica
  • Server Name Indication from C#
  • Subtotals for Pandas pivot table index and column
  • ObjectDisposedException on a ASP.NET Core 2.0 MVC custom database initializer class
  • Spring UTF-8 message resource from external jar issue
  • SSRS BIDS SUMMING A SUM TO PRODUCE A SUM
  • Rotate links along a circle
  • Grails Acegi manual login
  • Distributed Cache feature in YARN
  • Catch SqlException when Attempting NHibernate Transaction
  • Creating Word or XML document with VBA
  • Is there a built-in shortcut key for selecting the owner of the currently active form?
  • hover link to change *PAGE* background color with css
  • Constructor-Prototype cyclic reference JavaScript
  • android 3G signal strength
  • How do I get the standard Vim FTP command to work?
  • Semi-local Levenshtein distance
  • UIImage to UIColor array of pixel colors
  • Enable CORS on Tomcat 8.0.30
  • Hide legend for a single geom in ggplot2
  • Generate a runnable jar and include libraries in it with Maven
  • Use PHP to Replace HTML with HTML
  • How to get the Owner of the ContextMenu (from Silverlight 4 toolkit)?
  • Admob in ListView not clickable
  • Unable to start a WebView from an AsyncTask
  • 'Edit' function for forum posts and such
  • How to redirect into different page by user type in php and mysql
  • time column in sqlite using gorm
  • JQuery: Infinite input select
  • Geokit in Ruby on Rails, problem with acts_as_mappable
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • How to decleare char *const argv[] in swift [duplicate]
  • Access to a Matlab gui from the web
  • How to check if object is null in Java?