This is my code to connect to Azure:
<?php
$servername='myproject.database.windows.net';
$username='somename';
$password='somepassword';
$database='some_db';
$conn=New mysqli($servername,$username,$password,$database);
//Connection error handling:
// echo #mysqli_ping($conn) ? 'true' : 'false';
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
} else {
// echo "Connection success";
}
?>
I get the error:
Connection failed: 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.
I have tried launching this php from a WAMP server and from azurewebsites but both do not seem to work. It works with PDO but I have some pages already setup with mysqli_fetch_assoc queries and would like to preserve them.
Any suggestions?
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!
I have Apache (with PHP) and MySQL installed and running on my Raspberry Pi. I've done some simple tests with PHP, and it seems to be working. And MySQL is working perfectly from the terminal, and even another computer.
I made this PHP file:
<?php
echo("Connecting");
$connection = new mysqli("127.0.0.1", "admin", "password", "test");
if ($connection->connect_error) {
die("Connection error");
}
echo("Connection successful");
?>
Yet when I go to this page in my web browser, all I see is "Connecting". If I comment out the connection command, I see "Connecting Connection successful" in the browser.
It seems as if the PHP code stops running or hangs at the connection command.
Any ideas why I'm having this strange behavior?
Try adding these lines at the top: error_reporting(E_ALL);
ini_set('display_errors',1);
I hope You should do following
if ($connection->connect_error) {
echo "Connection error";
}else{
echo "Connection successful";
}
Try this instead
<?php
$link = mysqli_connect("127.0.0.1", "admin", "password", "test");
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);
?>
<?php
$con = mysqli_connect("localhost","root","password","shoutit", "80");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
When I try to run this I get the error message of
Unknown database.
How come something off the official documentation doesn't work? I couldn't find a solution for this. Adding an "80" port makes it load forever.
CREATE DATABASE 'some database name' ;
Works as expecting using mysql's client.
The same query (different database name) from php/mysqli - fails kind of.
It writes to the INFORMATION_SCHEMA.SCHEMATA
But fails to write to the mysql.db table?
This is actually an issue of a larger problem.
What is going on. Why does my mysqli fail?
code:
$mysqli = new mysqli("p:127.0.0.1", "root", "showtech123", "mysql");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
if(!$mysqli->query( "CREATE DATABASE $database_name;"))
{echo "DATABASE encountered error:" . $mysqli->error . "<br /";}
Errors:
db_name: terarydatabase3333, un:teraryuser5999
Warning: mysqli::query(): MySQL server has gone away in /www/admin.showtechllc.com/public_html/adddb.php on line 21
Warning: mysqli::query(): Error reading result set's header in /www/admin.showtechllc.com/public_html/adddb.php on line 21
USER encountered error:MySQL server has gone away
the fourth parameter in the constructor is the database you want to use.
you're actually trying to create a new database inside an existing one.
try this :
$mysqli = new mysqli("p:127.0.0.1", "root", "showtech123");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}else{
echo "connected";
}
if(!$mysqli->query( "CREATE DATABASE $database_name;")){
echo "DATABASE encountered error:" . $mysqli->error . "<br /";
}else{
echo $database_name . "created";
}
I'm using cPanel server and I'm getting this error.
Lost connection to MySQL server at 'reading initial communication packet', system error: 0
Although i read his question Lost connection to MySQL server at 'reading initial communication packet', system error: 0 , I still didn't get the solution. when i run my php code, it returns
"Connection successfulLost connection to MySQL server at 'reading initial communication packet', system error: 0"
here's the code:
$con = mysql_connect('mysite.com:2082', 'me#mysite.com', 'passwordhere', 'dbname');
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "Connection successful";
}
if ($selected = mysql_select_db("dbname",$con) or die(mysql_error())){
echo "Your database name is dbname";
}
mysqli_close($con);
thanks for answering.
I was able to sort it out. here's the code:
$con = mysql_connect('localhost', 'serverusername', 'serverpassword');
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$selected = mysql_select_db("db_name",$con) or die(mysql_error());