remote access to mysql on Ec2 - php

I have two amazon ec2 instances. One I am using as a webserver and the other one has the mysql DB. I have the below php script that I run from the web Server. I cant seem to connect to my Database. When I run the php script on the mysql server with $host= localhost, it works. How can I access the mysql server?
<?php
$host="ipaddress"; // Host name
$username="root"; // Mysql username
$password="mypassword"; // Mysql password
$db_name="myDB"; // Database name
$port=3306;
// Create connection
$conn = new mysqli($host, $username, $password, $db_name,$port);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else
{
echo "Connected";
}
?>

Have the user/host combo as a legitimate user in mysql.user
select user,host from mysql.user;
Have the aws ec2 security group open (the aws Firewall)
There are three choices here: my IP, custom, or anywhere for the ip addr CIDR
Have the mysql server configured to allow remote connections.
This is the bind-address and skip-networking setup. See the nixcraft document for those few changes if needed.

Related

Failing to access a Godaddy hosted MySQL server from AWS EC2

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!

Can't connect to remote MySQL database with MySQLI - timeout

I have created two servers on Linux and installed LAMP on both, one I called web and the other is called app.
I would like to save a log from the web server on the app server - but when I try to connect to the app server, I get timeout.
My code is:
$mysqli = mysqli_connect("66:66:666:66", "username", "pass", "DB");
if (!$mysqli) {
die("Connection failed: " . mysqli_connect_error());
}
Where 66:66:666:66 is the ip to the app server.
I have in the app server under PhpMyAdmin added user privileges to a user with the name "username" and then the host is "%".
Have i forgotten something?
Edit: I have already tried to add port number 3306 to the connection.

Unable to connect mysql database from php

<?php
$servername = "192.168.179.130";
$username = "root";
$password = "";
$dbname = "rawcdr";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
The above code gives a warning Warning: mysqli::mysqli(): (HY000/2002) A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\xampp\htdocs\new_table\index.php on line 59.
The host is a different server (CentOS 7), and i have checked its reach ability.
I have also provided special privilege in mysql server by:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'192.168.%' IDENTIFIED BY PASSWORD '';
Also started the firewall service by:
service firewalld start
Checked the port 3306 is open by following command:
netstat -nlp | grep 3306
Of course the first thing to check is that the firewall on the remote machine is not blocking access on port 3306.
But:
The root user account on a MYSQL Server is by default configured so that this account can only be used from the machine running the MySQL Server instance. So by default you cannot use root to access the server remotely.
And that is as it should be. It is afterall the SuperUser account.
EG
`root`#`localhost`
So you are going to have to create a new user on the remote MYSQL Server instance that is allowed to connect from either any remote host dangerous or a specific remote host (your PC's ip address). This new account should also be setup to be allowed access with the required privilages on the database you are trying to access.
CREATE USER 'ayax'#'192.168.1.200' IDENTIFIED BY 'new_password'
Or if its 5.7 server dont forget the new password expire
CREATE USER 'ayax'#'your-ip-address' IDENTIFIED BY 'new_password'
PASSWORD EXPIRE NEVER;
If your ip address is allocated by a DHCP server and can change from day to day something like this would be more flexible
CREATE USER 'ayax'#'192.168.1.%' IDENTIFIED BY 'new_password'
Try this :
$conn = mysqli_connect('192.168.179.130', 'my_user', 'my_password', 'my_db');
if (!$conn) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}

Having difficulties connecting to Amazon RDS MySql server with PHP

I'm just trying a basic connection with a PHP script and running into some issues, my PHP code is simply:
$host="endpoint:3306"; // Host name
$username="user"; // Mysql username
$password="pw"; // Mysql password
$db_name="RawData"; // Database name
$conn = new mysqli($host, $username, $password, $db_name, 3306);
//Check connection
if($conn->connect_errno > 0){
die('Unable to connect to database [' . $conn->connect_error . ']');
}
And getting an error Access denied for user 'user'#'cpe-174-109-76-241.nc.res.rr.com.' If it is to the point that is is an Access Denied error, is that based on the password? My password works because I can connect to the instance with MySQL Workbench on this same machine. Do I need to make an update to my security group for the PHP code to connect through localhost? Let me know if there is any more information I need to provide, I'm very new to PHP and very lost with this basic setup. Thanks!
You may have a security group preventing access.
Examine the allowable IP addr , adding yours as a test. Consider adding 0.0.0.0 and CIDR in the security group at play with port 3306
http://whatismyipaddress.com if needed

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