im trying to connect to google cloudsql from appengine, but im facing this error:
No connection could be made because the target machine actively refused it
my php file, that i have uploaded to appengine:
$sql = new mysqli(null,
'myuser', // username
'mypass'
'mydb',
null,
'/cloudsql/**:**'
);
if ($mysqli->connect_error) {
echo 'no';
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
else {
echo 'yes\n';
}
the appengine is allowed from the cloud sql.
what should i do ?
I have been trying to replicate the error you are getting but am unable to reproduce it when using code sample you provided.
Also your code sample has a few mistakes, you create your connection as $sql but then check $mysqli for connection errors, you should be checking $sql. So I have changed all occurances of $mysqli to $sql and you the more conventional way to check for a connection error is to check connect_errno and then read the message from connect_error
$sql = new mysqli(null,
'your-username', // username
'your_password_or_blank_if_using_root',
'database_name',
null,
'/cloudsql/project_id:instance_name'
);
if ($sql->connect_errno) {
echo 'no';
die('Connect Error (' . $sql->connect_errno . ') '. $sql->connect_error);
} else {
echo 'yes\n';
}
The socket file is actually /cloudsql/project_id:region:instance_id (i.e myproject-prod:us-central1:instance-1)
If you are on Flexible VM you might want to turn on debugging on one of your instances, then SSH to it, you can then verify the socket file indeed exists and try to connect over to it:
sudo apt-get update
sudo apt-get install mysql-client
mysql -u your-username -S /cloud/sql/your-socket-file -p
Related
DSN is:
mysql:dbname='MyDB';unix_socket='/cloudsql/ethereal-accord-123456789:us-central1:dev-Instance'
Connection failed: SQLSTATE[HY000] [2002] No such file or directory
PDO Connection Code:
try {
$conn = new PDO($dsn, $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
return $conn;
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage().' <BR>';
}
I Cannot seem to find detail information on how to setup the connections correctly.
I follow all documents and activated the permissions for the VM IP, the Client SQL API, and Admin API, and nothing..
Any Help will be appreciated.
You don't run the cloudsql proxy or you have defined another location for the socket file. You should check this.
./cloud_sql_proxy -instances=INSTANCE_CONNECTION_NAME=tcp:3306 &
$dsn = 'mysql:host=127.0.0.1;port=3306;dbname=DATABASE_NAME';
https://cloud.google.com/sql/docs/mysql/connect-external-app#pdo-tcp
I reverted my code back to MYSqli instead of PDO using these lines
$host_name = "35.222.111.111";
$database = "myDB123";
$user_name = "dbuser123";
$password = "pass12345";
$GCSocket ="/cloudsql/ethereal-accord-2000000:us-central1:dev-instance-1"; $GCPort='3306';
$connect = mysqli_connect($host_name, $user_name,$password,$database,$GCPort,$GCSocket )
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error() ."<br>";
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL ."<br>";
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL ."<br><br>";
} else {
echo 'DB is connected..! <BR>';
}
return $connect;
The issue was not with the above code, it worked all along, the issue was with permission.
I whitelisted my IP from my Compute Engine VM in the Cloud SQL Server
give permission to your Compute Engine Service Account to access the Cloud SQL
Cloud SQL Client or Cloud SQL Admin
Execute this in the Compute Engine VM:
setsebool -P httpd_can_network_connect_db 1
and voila...
here is where i found the answer!
This question already has answers here:
SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES) (SQL: select * from `table`)
(2 answers)
Closed 2 years ago.
I tried connecting my PHP file to MySQli database but when i ran the code it displays
But when i logon to phpmyadmin it works fine no errors i can login to my account with no problems
Credentials in the database:
Connection Code:
<?php
$con = mysqli_connect('localhost','sotomango', '1234', 'mysql');
if(!$con) {
echo 'noooo';
}
?>
I double checked the database usernames, passwords, and privileges. Everything seems in place and correct, but the php file wont allow me to connect to the database.
#Dharman had a nice post but I couldnt find it.
Try this for debug :
$con = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$con) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is
great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($con) . PHP_EOL;
mysqli_close($con);
mysqli_connect
if its a new instalation or new upgrade, You need to include your port into connection, mariadb comes as default port so, when you try to connect mysql its redirecting to mariadb.
you need to include your correct port into connection it might be 3306, 3307 or 3308, and use ip instead of localhost.
your final codes should look like this :
$host = '127.0.0.1';
$db = 'test';
$user = 'root';
$pass = '';
$port = '3308';
$charset = 'utf8mb4';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
$mysqli = mysqli_connect($host, $user, $pass, $port, $db);
mysqli_set_charset($mysqli, $charset);
} catch (\mysqli_sql_exception $e) {
throw new \mysqli_sql_exception($e->getMessage(), $e->getCode());
}
For The People Wondering
I solved this by just adding a port to the host name like localhost:3308
I have 2 amazon ec2 instances.I want to connect from one to another. One has a Mysql database (instance 1). When I run the php code below on instance 2. I get a
ec2-x-x-x-x.us-west-2.compute.amazonaws.com is currently unable to handle this request.
But when I run the same code on http://phpfiddle.org/ the code works. Why does it not work on my server?
<?php
// On WEB_SERVER
$host="public Ip of instance 1"; // Host name
$username="test"; // Mysql usernam
$password="pass"; // Mysql password
$db_name="mydab"; // Database name
$db_port="3306";
// Create connection
$conn = new mysqli($host, $username, $password, $db_name,$db_port);
Check connection
if ($conn->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}else
{
echo "connected";
}
?>
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I connected successfully to my database and the I tried to print my data but I can only see the "Connected succesfully" line in the browser. Here is my code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$db=mysqli_connect("host","root","pass","dbase");
if (!$db) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Connected successfully";
$sql = "SELECT username FROM students";
$result = $db->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "username: " . $row["username"]. "<br>";
}
} else {
echo "0 results";
}
$db->close();
?>
So the issue was that SELinux was blocking remote connections from PHP scripts executed by Apache web server.
The setsebool -P httpd_can_network_connect=1 in the terminal did the trick.
Edit: Note to future readers. The OP edited their question with the corrected syntax after answers were posted and this answer was based on their original post:
https://stackoverflow.com/revisions/36189996/2
and added:
"So the issue was that SELinux was blocking remote connections from PHP scripts executed by Apache web server. The setsebool -P httpd_can_network_connect=1 in the terminal did the trick."
Firstly, you didn't select a database.
and if (!db) needs to be changed to if (!conn) and if ($db->connect_error) to if ($conn->connect_error). The same variable needs to be used throughout your code.
You're also using the wrong variable $db it's $conn (or whatever you want to use, but as I said above, you need to use the same variable for your connection and for the query.
Error reporting http://php.net/manual/en/function.error-reporting.php would have told you about an undefined variable.
Therefore:
$conn = #mysqli_connect("host","root","pass", "your_database");
Sidenote: host I take it is only representative of the host you're using. If you're on your own PC, you would use localhost and accessing that file as http://localhost/file.php instead of file///file.php if that is what you're doing and will not work. The web browser will not parse PHP directives like that and you need to install a webserver/PHP in order for it to work, including MySQL.
Read the manual on connecting:
http://php.net/manual/en/function.mysqli-connect.php
and remove the # symbols while testing. They are error suppressors.
Check for errors with http://php.net/manual/en/mysqli.error.php
Example from the manual:
Sidenote: 127.0.0.1 or localhost or if hosted, use the setting they gave you to use.
<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Then the rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
You use another variable than you connected to the database!
You connected to $db but perform the query with $conn.
Also, in the check if the database connection could be established you do not have the $ before db.
It always helps a lot when you have in a terminal window the PHP error log open. 99% of errors can be fixed by your self.
I'm trying to connect to a remote WAMP server using a simple php script. I'm able to successfully execute scripts like this one:
<?php
$counter = 0;
while($counter < 20) {
$counter++;
echo $counter;
echo "\n";
}
echo "Done! Counter: ";
echo $counter;
echo "\n";
?>
But, unable to execute basic scripts on my laptop that try to connect to my remote server, such as below. The following line just freezes and the php script never stops running. I am executing it by cd'ing into the directory with the file and running php test.php.
$link = mysqli_connect($db_host, $db_user, $db_pass, $db_database, $db_port) or die(mysqli_error($link));
This prints bool(true):
var_dump(function_exists('mysqli_connect'));
Do I need to install anything to get this to work? I've done all the usual fixes, changed the .conf files on my local server and have an Android app that connects successfully, but now I just want to do operations on a MySQL table from Terminal but am unable to.
The correct call to see any errors generated by a mysqli_connect() is mysqli_connect_error() and not mysqli_error($link));
So change the connection call to
$link = mysqli_connect($db_host, $db_user,
$db_pass, $db_database,
$db_port)
or die(mysqli_connect_error());
Or as suggested in the manual
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
You will also need to be using a Userid/password that is configured to allow access from a remote connection. The default root will not allow remote connections, and should not be amended to allow remote connections.
You may find this page in the manual useful to create a new user account in MYSQL