PHP times out connecting to Cloud SQL instance - php

Here is my code for PHP for connecting to the database, a Cloud SQL instance for MySQL:
define('DB_SERVER', '192.0.0.1');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'db');
$db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME) or die(mysqli_connect_error());
Obviously the DB_SERVER is not the actual IP address, nor are the credentials, but I did put in a dummy one to see if I needed to add anything like cloudsql:/ or jdbc:// like you do in Java.
I did authorize the IP address for the webserver on Cloud SQL, but all it returns is a Connection Timed Out error; What can I do to fix this?
I'm certain that the credentials are correct, and that I can connect from other authorized IP addresses, as I have been able to access from another account & multiple IP addresses through the MySQL workbench.

Solved:
For anyone using Siteground's services, make sure to add 0.0.0.0/0 as the accepted IP addresses if you're using public IP authorization. Then find what IP's are connected when you refresh the php page by running this right after the page is refreshed:
SELECT host FROM information_schema.processlist WHERE ID=connection_id();
It should give you multiple IPs, and check them online using an IP locator to see if they match the location of the Siteground's services. Remove 0.0.0.0/0 and add that IP address to Google Cloud SQL.

Related

MySQL in XAMPP not allowing remote connections

After following some online tutorials, allowing wildcard (%) connections to the user, assuring that the port and firewall allow connections, and comparing the login info, I'm at a complete loss recving MySQL error 111.
The script I'm using to connect [PHP]
$connection = mysqli_connect('remotehost', 'user', 'pass') or die(mysqli_connect_error());
mysqli_select_db($connection, 'db');
I believe everything is in order, isn't it?
What should my next step be?

Unable to connect to phpmyadmin on other server

I have a website hosting on www.host1free.com. Earlier I had connected to MySQL database of the host.
Now I am having my own VPS which is accessed by ip address 5.231.36.181. I have also installed phpmyadmin. All works fine within the server.
Now I want to connect my website database to the MySQL server on 5.231.36.181.
The database on the server is accessed by 5.231.36.181/phpmyadmin
and the database on host is accessed by sql12.1freehosting.com/phpmyadmin
How to connect the database to my VPS phpmyadmin ?
whatever the details you are using for logging in to your phpmyadmin use the same usename and password for connecting to database
secondly host can be localhost or your VPS IP Address
The rectangle marked in red contains your IP ,
Use the IP Address of the server and Username and password that you used to login if you are still not getting it post us what you have tried and what error you are getting
connecting to mysql database
// Define database connection constants
define('DB_HOST', 'your IP');
define('DB_USER', 'Your DB USER');
define('DB_PASSWORD', 'db user Password');
define('DB_NAME', 'your dbname');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die('cannot connect to MYSQL');
if you are confused you can create a new database user assign the database to the particular user and can use the details to connect to your DB Cheers
You connect your website to MySql database, not to PhpMyAdmin. Enter Host as localhost, username and password. If localhost is not working, try entering IP of database. There must be statet somewhere what params to use to connect to database
Use Your Mysql Details and connect to your dataBase like this:
mysqli_connect('5.231.36.181',username,password,dbname);

Connect to Amazon RDS with PHP

I am trying to connect my RDS Instance with my PHP connection file.
This is what I have in my file:
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'User Name');
define('DB_PASSWORD', 'Password');
define('DB_DATABASE', 'DATABASE');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
I replaced localhost with my endpoint (rds thing) url, username and password with my RDS Instance user and pass and database name the one I've set when I created the instance. But it doesn't seem to work.
Is there anything else I have to do that I am not aware of or should it work?
RDS instances do not have a static IP address attached to them, you always use the endpoint for the host. For example:
database1.jlsdfjhgsdf.us-east-1.rds.amazonaws.com
If you are connecting to the instance with a username other than the root database account, you will need to grant the user privileges.
Check security group settings. If you are connecting from another EC2 instance on Amazon you will need to attach that security group. If you are trying to connect from outside AWS, you will need to whitelist the IP address you are accessing from.
Some ideas:
Try using the actual IP of the instance, then it should work.
Did you authorized access to your DB instance?
You may want to have a look at Get Started with Amazon RDS to properly setup your RDS instance
I was facing a similar issue whilst trying to connect an EC2 Apache server using PHP to the RDS MySQL instance.
Weirdly I could establish a connection via CLI - once in mysql running status will tell you which user youre logged in with, plus the port, server name etc.
Turned out some AMI images have SELinux enforcement - meaning the apache server cant send network requests as pointed out by this gentlemen (http://www.filonov.com/2009/08/07/sqlstatehy000-2003-cant-connect-to-mysql-server-on-xxx-xxx-xxx-xxx-13/)
Other points:
Make sure inbound ports are set for your RDS DB
In MySQL make sure the host is set to '%' as opposed to localhost
Always use the endpoint string to connect as the RDS IP changes
I was recently having a lot of trouble with this also but was able to fix it. I made sure my security groups (for the RDS and for EC2) were allowing each other. I was able to run my script from the terminal and connect to my database also from the terminal, but I couldn't get the script to run/connect to MySQL from a browser. It turns out I did not have mysql-server installed-- once I installed that and restarted httpd and mysqld it worked like a charm.
This article is what led me to installing mysql-server and the service starts/restarts. Hope it helps! -- http://www.rndmr.com/amazon-aws-ec2-easy-web-serverset-up-guide-with-a-mac-and-coda-2-256/
Just accepts all incoming connections.
I also had the connection problem between the ec2 (apache + php server) and the RDS (Mysql server) when following the tutorial at http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Tutorials.WebServerDB.CreateDBInstance.html.
I solved it by using the double quote when specifying the connection value while the guideline is using single quote.
define('DB_SERVER', "localhost");
define('DB_USERNAME', "User Name");
define('DB_PASSWORD', "Password");
define('DB_DATABASE', "DATABASE");
I was trying to connect to my DB instance using node-mysql. I found that I the endpoint that RDS provided me with did a DNS lookup. Followed that up and changed the URL to that one. I was only able to connect with mysql via command line until then. When I changed it to the resulting endpoint after the lookup, node-mysql was finally able to connect.

How do I connect MySQL server

I have created three EC2 instances, two of instances are web servers and one instance is a MySQL server. I would like to connect to the MySQL server and retrieve data. I was wondering how I can send a SQL query to the MySQL server.
<?php
$server = "localhost";
$username = "username";
$password = "password";
$database_name = "dbname";
$dbconnect = mysql_connect($server, $username, $password);
if(!$dbconnect)
{
//connection failed to the host
echo "-1";
exit;
}
if(!mysql_select_db($database_name))
{
//cannot connect to database
echo "-2";
exit;
}
?>
I used this php script to connect to the local mysql server. If I want to connect to the remote MySQL server on EC2 instance then do I just simply replace the server address to the IP address (elastic IP address) of the EC2 instance that running MySQL server?
Instead of using the Elastic IP address, use the DNS name associated with the Elastic IP address. It will resolve to the internal IP address associated with the current instance mapped to the Elastic IP address. This saves you in latency and cost.
Here's an article I wrote that describes this approach:
Using Elastic IP to Identify Internal Instances on Amazon EC2
http://alestic.com/2009/06/ec2-elastic-ip-internal
You'll also need to make sure that your MySQL database is listening on 0.0.0.0 instead of 127.0.0.1. Check that your MySQL database server is not accessible from the Internet.
not sure what programming language you want to use, but here is a Getting Started Guide for PHP, but it is just a matter of setting the IP

Connect database from other computer

I'm using PHP with MySQL database. The PCs are having a network to each other. My problem is I want to connect to the MySQL database on another computer. I want to store data on that MySQL database from another computer. How could i possibly do on this? Thanks so much for any suggestions.
The MySQL server must be configured to accept connections externally, and its firewall must be configured to allow incoming connections on that port (TCP port 3306). This may or may not already be set up.
You must also account for this in the MySQL permissions as follows.
Often, when setting up your MySQL permissions, you'll set user access rights only for #'localhost'. You'll need to make sure that both the user account and its granted permissions are set for the appropriate hostname or IP address you will be connection from. For example, you could create a new authorised user with:
GRANT ALL PRIVILEGES ON somedatabase.* TO someuser#'somehostname' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
You have to do all of this before you can connect to that server remotely with PHP, using something like this:
mysql_connect('mysqlservername', 'someuser', 'password');
Point mysql_connect() to use the other computer's name / IP address:
$server = '192.168.0.3';
$user = "foo";
$password = "bar";
$conn = mysql_connect($server, $user, $password);
You'll need to make sure the DB in the other PC has enough rights to connect from a different host - i.e. your computer.
Set up MySQL as normal on that computer. Then, simply:
<?php mysql_connect('IP of 2nd computer', 'username', 'password'); ?>

Categories