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

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.

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!

remote access to mysql on Ec2

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.

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.

Why is my php code not connecting to my remote MySql database?

I'm trying to connect to a remote MySql database and I get this error message:
Warning: mysqli_connect(): (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:\myLocalDiectory\RemoteConn.php on line 9 "Resource not found" error 404.
Here's my php file that I'm trying to use:
<?php
define("DB_SERVER", "serverName");
define("DB_USER", "userName");
define("DB_PASS", "password");
define("DB_NAME", "dbName");
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
Am I missing something? Thanks for any help!
The first thing that I would check (if you haven't done so) is that you can in fact connect to the database from the computer that runs your PHP script. This to rule out a network or firewall problem.
The first thing would be pinging the server. In a DOS prompt run:
ping servername
Where "servername" is the same string that you put in your PHP script above. If this does not reply with a string similar to the one below, specifically, the first word is not "Reply":
Reply from 192.168.239.132: bytes=32 time=101ms TTL=124
This means that there is most likely no connectivity between the computer running the PHP script an the mysql server. I would then check if the server and the computer are properly connected to the network, if the server is up, an if there is not firewall in your computer running the PHP script or on the server.
Now, if your test above shows "Reply" to the ping, you can test if you can connect to the Mysql service from your php server. For this you can use Mysql workbench (http://dev.mysql.com/downloads/workbench/) and from there create a connection with the database parameters that you are giving to your script. If you cannot connect with Mysql workbench, you might need to disable a firewall in your Mysql server, a firewall in your computer running PHP, or enable the Mysql server to accept remote connections for the database and username that you use in your PHP script (some distributions Mysql server are installed to only accept local connections for safety).
If the problem is a permission in the server (the user can only connect locally but not from a remote computer for instance), you can enable the permission in the Mysql server with the GRANT command: http://dev.mysql.com/doc/refman/5.1/en/grant.html
define("DB_USER", "userName");
Try root instead of userName and define DB_HOST.

Cannot connect to MySQL database, socket error

I'm new to php, and while trying to make a connection on one of my pages to the database I set up on the phpMyAdmin page of my site.
I get this error:
"Database connection failed: Can't connect to local MySQL server
through socket '/tmp/mysql.sock' (46) (2002)"
I don't know what a socket is, or why it's trying to go to what looks like a temp file, so I don't even know where to being to troubleshoot this.
The code I'm using to make the initial connection is this:
<?php
define("DB_SERVER", "localhost");
define("DB_USER", "xxxx");
define("DB_PASS", "xxxx");
define("DB_NAME", "tester");
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
// Test if connection occurred.
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
I know I should have access to the MySQL databases, since I logged on and made them myself.
I previously set the page up by using WAMP on my computer, and everything worked fine.
It's just when I tried making it live on the site that I ran into this error.
Any help would be awesome!!
Try using 127.0.0.1 instead of localhost.
If that does not solve it, and you have root access to your server, try the following command
service mysql restart
To restart the mysql server.
The first option will probably work. Again, if you have root access to your server, you should change the mysql config to support sockets, since it's better than the TCP-ip connection.
After contacting my hosting service several times, it seems the error occurred because my hosting service recently changed their specifications and now uses "mysql" instead of "localhost" in the host and server fields. I had used "localhost" before with another host service, so I didn't think to change this, and the latest help articles on my host's website had not updated to reflect this.

Categories