Connect to AWS mysql over SSL in PHP? - php

I am trying to connect to (remote) AWS MYSQL database using php and ssl.
I created a database user and could connect with it from php and also on console.
Then I modified the user to use SSL with "require ssl" in the database.
I can stil connect on the console by:
mysql -h host -u username -p --ssl
But in php I get an ERROR 2002.
$con=mysqli_init();
mysqli_ssl_set($con,NULL,NULL,"cacert.pem",NULL,NULL);
$link = mysqli_real_connect($con, "host", "username", "password");
As far as I know the ssl_set parameters doesn't matter with AWS databases.
How to connect to Amazon RDS via SSL?
I think it has to do with the mysqli_ssl_set method, without it a connection is made and access is denied by database and this is the correct behaviour. It might be some configuration error on client side, but I have no idea what I shoud check.
The error is fired by mysqli_real_connect, as mysqli_ssl_set always returns true, just after it tries to connect will it receive an error.

I noticed it did not work for me until I specified the port. The following worked:
$db = mysqli_init();
$db->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);
$db->ssl_set(NULL, NULL, 'rds-combined-ca-bundle.pem', NULL, NULL);
$db->connect($host, $user, $pass, $db, 3306);

$servername = "only-ssl-db.ct5b4uz1gops.eu-central-1.rds.amazonaws.com";
$username = "username";
$password = "password";
$dbname = "dbname";
$con = mysqli_init();
if (!$con){
die("mysqli_init failed");
}
mysqli_ssl_set($con,NULL,NULL,'rds-combined-ca-bundle.pem',NULL,'DHE-RSA-AES256-SHA');
if (!mysqli_real_connect($con,$servername, $username, $password, $dbname))
{
die("Connect Error: " . mysqli_connect_error());
}
// Some queries...
echo "Database connected";
printf("Client version: %d\n", mysqli_get_client_version());
mysqli_close($con);

Related

php mysql connection failed The server requested authentication method unknown to the client

$servername = "localhost";
$username = "jaskaran";
$password = "India#123";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
jaskaran has all access mysql and when i try with
mysql -u jaskaran -p it is working fine by when i try with php it does not why
error is The server requested authentication method unknown to the client
even i try this also
ALTER USER 'jaskaran'#'localhost' IDENTIFIED WITH mysql_native_password
BY 'India#123';

SNAT PORT Issue

I am getting SNAT Port issue. I don't know what to do I am using PHP 7.0 let me show my code
this is my db code
function db_connect() {
$server = 'P:servername'; // this may be an ip address instead
$user = 'username';
$pass = 'pasword';
$database = 'test'; // name of your database
// Create connection
$conn = mysqli_connect($server, $user, $pass, $database);
return $conn;
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
}
I am getting SNAT port issue what should I do?
Using Persistent Connections
If mysqli is used with mysqlnd, when a persistent connection is created it generates a COM_CHANGE_USER (mysql_change_user()) call on the server. This ensures that the re-authentication of the connection takes place.
https://www.php.net/manual/en/mysqlnd.persist.php

How to fix error in dbconn file when upload php mysql project to free webhosting site?

I've been trying to upload my PHP MySQL(in Dreamweaver) project to a free web-hosting site.
When I logged in, there is an error that appear in dbconn.php file.
The error is shown below:
and here's the code in my dbconn.php file:
<?php
/* php& mysqldb connection file */
$user = 1350048; //mysqlusername to db
$pass = "password"; //mysqlpassword to db
$host = "eskl.freeoda.com"; //server name or ipaddress
$dbname= 1350048; // db name in server freeoda
$dbconn= mysql_connect($host, $user, $pass);
if(isset($dbconn)){
mysql_select_db($dbname, $dbconn) or die("<center>Error: " . mysql_error() . "</center>");
}
else{
echo "<center>Error: Could not connect to the database.</center>";
}
?>
I would really appreciate if anyone can teach me how to solve this.. thanks in advance!
As Kerbholz already stated, don't use mysql_* functions, they are really outdated.
Instead use mysqli:
$servername = "eskl.freeoda.com";
$username = "1350048";
$password = "password";
$database = "1350048";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
For your error it got mostly something to do your host doesn't allow remote connections. Try to change the serverhost to localhost or 127.0.0.1

How to connect to oracle from php with tnsnames?

I am getting this error:
Server Response='12154 ORA-12154: TNS:could not resolve the connect identifier specified
I am on Ubuntu 14.04
My environment variables are:
ORACLE_HOME = /usr/lib/oracle/12.1/client64​
LD_LIBRARY_PATH = /usr/lib/oracle/12.1/client64/lib
TNS_ADMIN = /usr/lib/oracle/12.1/client64/network/admin
tnsnames.ora and sqlnet.ora are within /usr/lib/oracle/12.1/client64/network/admin
PS: I can connect through sqlplus with:
sqlplus64 user/pass#dbname
This is the code:
<?php
$conn = oci_connect('user', 'pass', 'dbname');
?>
It never worked on that way, what I did was to use Easy Connect string:
$conn = oci_connect('user', 'pass', 'host/servicename'); however the first way should have worked because in another environments it works.
tnsnames.ora file should look as follows
DBNAME=
(DESCRIPTION=
(ADDRESS=
(PROTOCOL=TCP)
(HOST=YOUR IP ADDRESS)
(PORT=YOUR PORT NUMBER)
)
(CONNECT_DATA=
(SERVICE_NAME=YOUR DBNAMEPROD)
)
)
conn.php file should look as follows
$USERNAME = "hr"; // Login Username
$PASSWORD = "hr"; // Login Passowrd
$DATABASE = "DBNAME"; // Connect string to connect to your database found in tnsnames.ora
$conn = oci_connect($USERNAME, $PASSWORD, $DATABASE);
if(!$conn){
echo "Your Connection Has an error";
}
else{
echo "Your Connection is Successful"
}

MSSQL using PDO to connect

I am trying to connect to a mssql server on my mac through pdo_dblib. I have the freetds.conf updated to the host I want to connect. My phpinfo tells me that I have all the driver hooked up and good to go. Below is the code that I wrote to test if I can connect to the server.
<?php
$servername = "IP";
$port = "port";
$username = "username";
$password = "password";
$myDB = "database";
try {
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", "$username", "$password");
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
However I get an error:
Connection failed: SQLSTATE[] (null) (severity 0)
I tried using SQL Developer to connect to the mssql server and it worked. I have been trying to solve this problem for a whole day. What might be the problem? Thanks!
Remove the quotes from the variables in the connection string -
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", $username, $password);
I found out the answer!
You have to uncomment TDS version protocol in /usr/local/Cellar/freetds/0.91/etc/freetds.conf.
Found the solution in Why won't my server connect to a remote MSSQL server using PHP mssql_connect?
but why do I have to do this...no idea...wish some one could give some explanation.

Categories