Failing to access a Godaddy hosted MySQL server from AWS EC2 - php

I have a MySQL server hosted in my Godaddy server. I want to access that database from my EC2 instance. However, I am unable to do so. I have added the public IP of my EC2 instance to the remote MySQL access hosts.
Things I have tried:
By using my Godaddy hosted website name as my server name.
Using my Godaddy hosted website IP address as my server name.
Every time I try to connect, It gives me the error:
Connection failed: Unknown MySQL server host '<hostname>:3306' (11)
Script I used:
<?php
$servername = "<hostname>:3306";
$username = "<username>";
$password = "<password>";
$dbname = "<dbname>";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
PS: I can successfully connect my MySQL workbench and a locally hosted server to my Godaddy database.

Try removing :3306 from $servername
Make sure the arguments are correct. mysqli() needs port as separate argument as described here

According to godaddy you need to enable Allow Direct Database Access for remotely connections.
Link here!

Related

How to fix MySQL State [2002] Database "Connection Refused "

I am setting a new live server for my Laravel application its work perfectly on localhost but not on live server. Now its showing SQL State [2002] connection refused. I also try with mysqli_connect and PDO but the error remains the same. Is possible a problem with Hosting Provider?
<?php
$servername = "examrunner.com";
$database = "XXXXXXXX";
$username = "XXXXXXX";
$password = "XXXXXXXXXX";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Use server name as localhost. Because cPanel hosts your databases locally, use localhost as the database's hostname.
$servername = "localhost";
Please check below mentioned points to resolve this issue :
Check database and user exists
Make sure that Database user and Database connected to each other and you have given sufficient privileges to user.
If site hosted on current serve user hostname = 'localhost'
Try to print detailed error.

Connecting to RDS from PHP does not work, but works from console

I recently set up RDS server using MySQL dataset and using EC2 beanstalk as a server.
I have been able to connect to dataset from
console of local machine using endpoint url, username, password
from PHP on local machine, using following code:
$servername = "xxxxx.xxxxxxxxxxxxxx.us-east-1.rds.amazonaws.com";
$username = "xxxxx";
$password = "xxxxxxx";
$dbname = "xxxxxxx";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname, 3306);
// Check connection
if (! $conn) {
echo "not connecting..!!";
die("Connection failed: " . mysqli_error($conn));
}
This works correctly, inserts data into the database in the RDS.
I ssh to EC2, check connect from console (using mysql -h -u -p). This too, work correctly. I can change the database from here and it is indeed reflected.
Next, I deploy the exact same code on EC2, and hope it works via PHP as well, as that code is already tested. But it does not.
I checked SELinux status is disabled
What else could be a problem?
I should mention: I am using Amazon Linux, I installed 'httpd' on local it.

Connecting to a MySQL db created in an Ubuntu AWS instance

Ok so I created an ubuntu instance from AWS and downloaded LAMP on it as you would in a regular Ubuntu OS. When I try to connect to the db from my php script it's rejecting the connection. saying "Connection failed: Connection refused". So I guess my question is this: Does amazon not allow you to connect to a db without using their RDS database service or am I putting something wrong here? (I've hidden some of the data for security purposes, the ... are numbers of my instance). When I put "ec2-34-...-..-64.compute-1.amazonaws.com" into the browser the apache message comes up so I don't see why this is not working as a server name?
$servername = "ec2-34-...-..-64.compute-1.amazonaws.com";
$username = "root";
$password = “hidden";
$dbname = "questions87";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
You need to change your EC2 Security Group Inbound Rules related with 3306, you can find that on the AWS FAQ. Then, you need to be sure that your user#YOURIP have all permissions on your MySQL database. For your Inbound Rules it is recommended to use a Custom Rule to your IP, not All Traffic.
Reference and further reading:
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html

not allowed to connect to this MariaDB server in php

I try connect my remote server database in php but it's give bellow error
Host 'xx.xxx.xx.xx' is not allowed to connect to this MariaDB server in
My connection code like this
$servername = "my_server_address";
$username = "my_username";
$password = "my_password";
$dbname = "my_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
If your credentials are valid, you most likely need to configure MariaDB for remote client access.
See Configuring MariaDB for Remote Client Access
I had a similar issue after installing "xampp-windows-x64-7.3.6". After looking around and testing several solutions without any changes I did the following:
Uninstall xampp
Restart machine
Reinstall xampp
Restart machine
Importing old databases via phpmyadmin
All works fine for me now.
I did initially add a new user account. That might have caused the problem but I'm not testing this again.
You need to add new user in mariadb, goto privileges and add that can be accessed remotely

How to access information from a SQL database to another server?

I have two servers, for the sake for my questions, lets name the first server A, and the second server B. Server A is a magento server that has a lot of user data, and server B is a basic web page.
I want to create an analytics dashboard in server B, that uses the SQL databases of server A.
Now the problem is having a remote connection from server B to server A.
I try to connect to server A, using this code in server B:
$servername = "104.2*****"; // Server A
$username = "root";
$password = "******";
$dbname = "magento";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
I get this error:
Connection failed: Can't connect to MySQL server on '104.2******'
Can someone please tell me what I'm doing wrong?
You may need to create a ssh tunnel to connect to a remote server hosted database if the settings on the database server do now allow unsecure connections which is likely why you're getting that error.
http://kb.mediatemple.net/questions/133/Tunnel+local+MySQL+server+through+SSH#gs
It might be that there is a firewall restriction on the server you want to connect to. The MySQL port connection might be restricted.
Try to connect from the server you run this script (or another server) to the ip/port of the server you want to connect to.
If you have access to a linux/windows console, try running:
telnet 104.2***** 3306
3306 is the default port for MySQL and telnet should work.

Categories