Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to an online MySQL database from localhost

Tags:

php

mysql

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?

like image 637
Marcus Avatar asked Jan 17 '10 23:01

Marcus


People also ask

How do I access my localhost MySQL database?

This can be done with the mysql_connect PHP function: $mysqli = new mysqli("localhost", $username, $password, $database); With this line PHP connects to the MySQL database server at localhost with the provided username and password. After the connection is established you should select the database you wish to use.

How do I connect to a MySQL IP address?

Select Connections from the SQL navigation menu. In the Authorized networks section, click Add network and enter the IP address of the machine where the client is installed. Note: The IP address of the instance and the mysql client IP address you authorize must be the same IP version: either IPv4 or IPv6. Click Done.


1 Answers

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

like image 90
Alix Axel Avatar answered Oct 21 '22 14:10

Alix Axel