Connection Issue - Unable to connect to database server? - php

I am getting this message in my site all of a sudden without making any changes in the config file. I will post my config code to see if there are any issues with it.
define('DB_SERVER', 'www.victorexoticagoa.com');
define('DB_SERVER_USERNAME', '******');
define('DB_SERVER_PASSWORD', '********');
define('DB_DATABASE', 'victor');
define('USE_PCONNECT', 'false');
define('STORE_SESSIONS', 'mysql');
define('CFG_TIME_ZONE', 'Asia/Kolkata');
The above code is from the configure.php file.
The below code is the connection:
tep_db_connect() or die('Unable to connect to database server!');
And the code below is the function which does the connection:
function tep_db_connect($server = DB_SERVER, $username = DB_SERVER_USERNAME, $password = DB_SERVER_PASSWORD, $database = DB_DATABASE, $link = 'db_link') {
global $$link;
if (USE_PCONNECT == 'true') {
$server = 'p:' . $server;
}
$link = mysqli_connect($server, $username, $password, $database);
if ( !mysqli_connect_errno() ) {
mysqli_set_charset($$link, 'utf8');
}
return $$link;
}
Any help will be gladly appreciated. Thanks

Check if your database is up by using the following command in a command shell:
mysql --host=www.victorexoticagoa.com --port=yourport --user=youruser --pass=yourpass
If it cannot connect, the problem is the server, not your code.

Have you checked if that is the way you need to connect or the port to connect? I dont see the port (thats not supposed to be port 80 most times). Please check docs of the host and ask for details. It should be documented.

Related

How to know if MySQL is using SSL to connect to a remote database?

Here is my code to connect to my remote DB:
$con = mysqli_init();
mysqli_ssl_set($con, "/etc/letsencrypt/keys/...certbot.pem", "/etc/letsencrypt/live/.../cert.pem", "/etc/letsencrypt/live/.../fullchain.pem", NULL, NULL);
$connection = mysqli_real_connect($con, self::$host, self::$user, self::$password);
self::$lastConnection = $connection;
$db = "db_prod";
Unfortunately, I cannot tell if it's actually working because if I intentionally mispell any of the characters for my certificates in mysqli_ssl_set, the connection is still successful. So how can I know for sure that this is working as intended?
Any help appreciated.
Since you have used mysqli_ssl_set to try establishing SSL connection to MySQL. The system will proceed to use it for subsequent connection request.
Hence, You may check the $connection which is the return value of mysqli_real_connect
So please amend to:
<?php
$con = mysqli_init();
mysqli_ssl_set($con, "/etc/letsencrypt/keys/...certbot.pem", "/etc/letsencrypt/live/.../cert.pem", "/etc/letsencrypt/live/.../fullchain.pem", NULL, NULL);
$connection = mysqli_real_connect($con, self::$host, self::$user, self::$password);
/////////////////
if (!$connection)
{
die("Connect Error: " . mysqli_connect_error());
}
/////////////////
self::$lastConnection = $connection;
$db = "db_prod";
?>

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

Connect to AWS mysql over SSL in 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);

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"
}

Categories