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";
}
?>
Related
I am working on a payroll system. Everything is working fine on localhost (xampp) but when I upload it to ionos hosting, it is loading the admin login page fine but when i enter the username & password it is giving "Connection failed: No such file or directory".
DB name: dbs4868780
and here is the code:
<?php
$conn = new mysqli('localhost', 'root', '', 'xxxxxxxxx');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
EDIT:
I tried it but it is still giving the same error.
I also tried to connect DB with PHP code provided by ionos but it is still giving "Connection failed. File or Directory not found"
Here is the php code from ionos:
<?php
$host_name = 'db5005797255.hosting-data.io';
$database = 'dbs4868780';
$user_name = 'xxxxxxxxx';
$password = 'xxxxxxxxxxxxxxxxxxxx';
$link = new mysqli($host_name, $user_name, $password, $database);
if ($link->connect_error) {
die('<p>Failed to connect to MySQL: '. $link->connect_error .'</p>');
} else {
echo '<p>Connection to MySQL server successfully established.</p>';
}
?>
See attached fake database example to get your data
I have two database servers, MySQL and MariaDB, when I'm trying to connect to my local database on MySQL server, I always get "Connectionfailed: SQLSTATE[HY000] [1049] Unknown database 'ruff'". I found out, that my code is connecting into MariaDB server instead of MySQL. I can CREATE and CONNECT to any database on MariaDB.
How can I connect to MySQL server databases?
My code is:
// Creating a connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Creating a database named $dBName
$sql = "CREATE DATABASE $dBName";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully with the name newDB";
} else {
echo "Error creating database: " . $conn->error;
}
// Connecting to database named $dBName
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);
if (!$conn)
{
die("Connection fail: ".mysqli_connect_error());
}
// closing connection
$conn->close();
Adding port number after the localhost solved the problem.
$servername = "localhost:3308"; //MySQL
$servername2 = "localhost:3306"; //MariaDB
I get this error when I try to connect to the mysql database using php mysqli class.
I am using
OS :- windows 10
webserver :- xampp
php :- 7.3.3
it got a warning error and error message is junk.
mysqli::__construct(): (HY000/2002):
�ڑ��ς݂̌Ăяo���悪���̎��Ԃ�߂��Ă������������Ȃ��������߁A�ڑ��ł��܂���ł����B�܂��͐ڑ��ς݂̃z�X�g���������Ȃ��������߁A�m�����ꂽ�ڑ��͎��s���܂����
Using following code:
$dbServerName= "*******ap-northeast-1.rds.amazonaws.com";
$dbUsername= "example";
$dbPassword= "*******";
$dbName= "example";
// Create connection
$conn = new mysqli($dbServerName, $dbUsername, $dbPassword, $dbName);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
dbServerName is amazon.com for example,
password is * for security.
I am trying to get my PHP pages to connection to a remote server hosting the MySQL database. I can connect via the command line just fine with the same username and password. Below is a simple test file I created. The only thing I've done to the code is remove the password, but I know that's not the issue. Like I said I can connect via the CLI. This is a Linux install. When viewed from the web browser, all I get is an error 500 page. If I comment out the mysqli statement, the page displays the Connected successfully.
<?php
$servername = "12.0.1.170";
$username = "jason";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
/ die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Per the php documentation for mysqli_connect()
$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);
Your code appears to be lacking all four parameters required.
You have:
$conn = mysqli_connect($servername, $username, $password);
Perhaps try:
$conn = mysqli_connect($servername, $username, $password, $database);
with a $database='db_name'; value added into your variables section.
There are two possible issues with remote mysql connection:
1. Firewall of the server: You must enable incoming connection on port (for. e.g. 3306).
2. User in mysql: You must have a user created (for e.g. jason in this case)
Try connecting this way
$con = mysqli_connect('localhost','root','','db_name');
if($con)
echo "hell yeah its connected";
else
echo "i m boned"
// create connection
$conn = new mysqli($server, $username, $password, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
I am trying to connect my php file with a cleardb database using the heroku platform. My connection details and config are correct when I test using the sqlworkbench, but how do I know if my php is connected?
I've tried echo "Connected successfully", but it doesn't appear.
And where is the composer.json file?
Website : https://best-app-ever-g52grp.herokuapp.com/