Syed Jahanzaib Personal Blog to Share Knowledge !

November 13, 2016

mysql unable to connect with remote mysql host/database

Filed under: Linux Related — Tags: , , — Syed Jahanzaib / Pinochio~:) @ 8:30 PM

If you are trying to connect with remote mysql database to perform remote management or connectivity with some web app to add data into the db , and sees the following error

Connection failed: Access denied for user ‘zaib’@’x.x.x.x’ (using password: YES)

OR

Host ‘xxx.xx.xxx.xxx’ is not allowed to connect to this MySQL server

Then you need to grant access to the user from any hostname. This is how you add new privilege from mysql command line.

NOTE: Please beware that this is for just example purpose only , and you MUST take good security measures like Strong Password, and allow only specific IP address to access etc.

 

CREATE USER 'zaib'@'localhost' IDENTIFIED BY 'zaib1234';

GRANT ALL PRIVILEGES ON *.* TO 'zaib'@'localhost' WITH GRANT OPTION;

CREATE USER 'zaib'@'%' IDENTIFIED BY 'zaipassword';

GRANT ALL PRIVILEGES ON *.* TO 'zaib'@'%' WITH GRANT OPTION;

FLUSH PRIVILEGES;


 

Sample PHP page to test Remote Mysql connection

Now you can test it with sample php page. change the host name of remote mysql server, and the id , password we just created in above steps.

 

<?php
$servername = "remote.mysql.host.ip.or.name";
$username = "zaib";
$password = "zaibpass";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully. Hurraahhhhh/Alhamdolillah";
?>

 


Regard’s
Syed Jahanzaib

 

1 Comment »

  1. MashAllah g
    Issue is resolved,
    Good Work🙂

    Comment by WayLink — November 19, 2016 @ 6:53 AM


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: