Unable to Connect to Mysql Through PDO on Certain Networks - php

I am trying to connect to a MySQL database through PHP's PDO. It works fine on my home network and some other networks (coffee shops, public internet). When I try to connect through my phone's personal hotspot or through hotel wifi, it no longer works.
I am using the same login information across all of these with the same machine setup (XAMPP VM - Mac). This is my connection file:
$DBO = "mysql:host=$DB_Servername;dbname=$DB_Name";
try {
$conn = new PDO($DBO, $DB_Username, $DB_Password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Connection Failed: " . $e->getMessage();
}
This is the error I am getting:
Connection Failed: SQLSTATE[HY000] [2002] Connection timed out
I've tried the server name with the website address and the ip address. The strange thing is when I'm using the VS code database extension, I am able to connect to the database fine (no matter the network I am on). I can't figure out why that would be working but the PDO wouldn't be.
I read this answer about adding a space after 'mysql: ', and it returns this new message:
Connection Failed: SQLSTATE[HY000] [1045] Access denied for user 'dailyrts_realDB'#'localhost' (using password: YES)
The user account I am logging in with is assigned to the correct database and the new IP's I connect to are being registered.
dns_get_record returns:
dns_get_record(): A temporary server error occurred.

I wasn't able to find a fix using XAMPP-VM however, the same code works perfectly when using the XAMPP Installer version of the app on MacOS. I looked around for a bit to see if anyone else was having this problem and I didn't find anything that matches this issue specifically. Most were referring to connecting to a local database.
This answer about accessing MySQL through the root user is pretty close to the issue I was having and they recommend following this guide to fix the issue. I don't need to have the VM version installed so I haven't tested this myself however, it seems to have worked for him.
I'm not sure why the VM version only works on some networks and not others when connecting to MySQL databases. I'm sure this has something to do with port blocking on the network's end (mobile hotspot providers are notorious for blocking ports) and because the VM version handles network traffic differently from the regular version.
It looks like issues with the VM version are not isolated to this specific issue on MacOS. In my personal experience (and others), I'd recommend going with the installer version if you are able to. It is more developed and has more online support surrounding it.

Related

Connection failed: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client. Busy ports or source code problem?

I decided to continue an old project that I had abandoned some time ago due to many mistakes. With new strength I started to remove mistakes but I reached a point where I got stuck. I found with great difficulty that my problem came from the PDO connection to my database and half of my code became unusable. I read on the topic and tried many different ways to solve the problem, but without success. For starters I will say that I use XAMPP, Server version: 10.4.8-MariaDB, Apache / 2.4.41 (Win64) OpenSSL / 1.1.1c PHP / 7.3.1, phpMyAdmin Version information: 5.1.1 (up to date). Since I haven't used XAMPP for a long time and I have Workbench, Postgree and others installed, I had to change the ports of Apache and MySQL. I put a password on the root account in phpMyAdmin because I read that this can help, but then there were more problems, I used this request in phpMyAdmin:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password
BY '...';
But in this query it gave me a syntax error and for that I used the one that was successful:
ALTER USER 'root'#'localhost' IDENTIFIED BY '...';
But the problem still exists. I also want to insert the fact that there are 2 connection strings in my project. There is no problem with one, but with the PDO connection string I get this error from the try catch construction:
Connection failed: SQLSTATE [HY000] [2054] The server requested authentication method unknown to the client
I read that the problem may be due to busy ports, but I decided to consult you before I started messing with the XAMPP files. Basically I changed the port to 3308 and the workbench is to 3306 but still when I run XAMPP I get an error message:
Problem detected!
Port 3306 in use by "Unable to open process"!
MySQL WILL NOT start without the configured ports free!
You need to uninstall / disable / reconfigure the blocking application
or reconfigure MySQL and the Control Panel to listen on a different port
However, I still managed to start MySQL.
I decided to write this to get advice and possibly help people with the same problem, because I saw that there are a lot of people who have encountered this.
I found a solution to my problem thanks to #iambpn -> https://github.com/laradock/laradock/issues/1392. I also have a mysql server installed. I changed my connection string in the same way as the post from Aug 23, 2019 by putting the host. my solution looks like this:
$host = "127.0.0.1:3308";
$dbname = "inventory";
$user = "root";
$password = "------";
try {
$dsn = 'mysql:host='.$ host.';dbname ='.$dbname;
$connect = new PDO ($dsn,$user,$password);
$connect->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
return $connect;
} catch (PDOException $e) {
echo 'Connection failed:'.$e->getMessage();
exit;
}
but before that the XAMPP ports must be changed if SQL server is installed!

Constructor failed on creating a new PDO connection

I'm trying to access a remote MySQL server. I created a user in the remote server and granted all the remote privileges to all hosts.
This is my code for connecting to database:
<?php
try{
$dsn = "mysql:host=MY_REMOTE_SERVER_IP;dbname=DB_NAME;port=3306";
$db = new PDO($dsn, 'USER_NAME','PASSWORD');
}catch(Exception $e){
echo $e->getMessage();
}
The code is working fine on my localhost. I even tried the code on some Playgrounds and it's working fine. But on my website, it's not working. The website does not connect to the database and always returns Constructor failed error.
The PHP version is 7.4 on my website and it supports PDO. But I don't know why it does not make the connection?
This is a picture of the error:
Just guessing: you maybe should not use the remote ip address, but localhost or 127.0.0.1 for your database host.
According to the pdo source code, this error message means the connection failed, which is not really helpful, but could mean one of these things (but not limited to):
hostname is wrong
database name is wrong
credentials are wrong
some networking issue
you name it :)
My prime suspect is the hostname, but just make sure all parameters are correct.

PDO statement in Azure websites with MySQL in-App

I have created a web app to communicate with MySQL In-App for Azure website.
However I'm getting error as,
"Connection failed: SQLSTATE[HY000] [2002] An attempt was made to access a socket in a way forbidden by its access permissions."
I have made sure that host, database, username and password are correct.
Don't know the issue, looks like authentication issue though.
Sample Code :
$pdocon = "mysql:host".$conStr["Data Source"].";dbname=".$conStr["Database"];
try {
$conn = new PDO($pdocon, $conStr["User ID"], $conStr["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();
}
Where, if I simply print $pdocon I have values.
Also, I have correct values in my $conStr array.
If you are using lower App Service Plan, you may scale to a higher App Service Plan and then check.
Check in your web app application settings if there is a connection string. PHPmyadmin uses MYSQLCONNSTR_ to connect to the MySQL server. If you have a connection string in application setting change the connection string type to Custom , so you can still have the information if needed or delete it. This will force PHPmyadmin to access MYSQLCONNSTR_localdb and connect to the MySQL in-app server.
Reference:https://blogs.msdn.microsoft.com/appserviceteam/2016/09/08/troubleshooting-faq-for-mysql-in-apppreview/
As mentioned in this document (https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox), Connection attempts to local addresses (e.g. localhost, 127.0.0.1) and the machine's own IP will fail, except if another process in the same sandbox has created a listening socket on the destination port.
Rejected connection attempts, such as the following example which attempts to connect to 127.0.0.1:80, will result an exception error.
Refer these FAQs (https://github.com/projectkudu/kudu/wiki/MySQL-in-app) for more details:
Where can I find MySql Credentials (connection string)?
The connection string flows to your application as an env variable MYSQLCONNSTR_localdb. Beware that we are not using the default MySql port (3306). In fact, the port number may vary for each application life cycle depending on its availability at startup time. The port info is also available as an env variable WEBSITE_MYSQL_PORT to your site.
How to use phpMyAdmin with MySql in-app?
phpMyAdmin is enabled by default with the feature. You can access it thru https://.scm.azurewebsites.net/phpMyAdmin/. Since MySql is only started with the main site, do make sure that the main site is running (simplest way is to turn on AlwaysOn) before using phpMyAdmin. Unlike phpMyAdmin from SiteExtenions gallery, this phpMyAdmin is aware of MySql credentials and will connect automatically.
Important: If you previously have phpMyAdmin installed via SiteExtension gallery, you will have to uninstall it. Since this phpMyAdmin from SiteExtension gallery will take precedent and it is notMySql In-App aware, it will not work with MySql In-App.
Maybe the values for connection string are not correct. That message is related to a bad username, password, database or server values.
Try to check this link if you are obtaining the connection values in another form. The connection string is stored under D:\home\data\mysql\MYSQLCONNSTR_localdb.txt
Another option can be related to stopped MySQL process -only for free app service plan- in such case you should run any PHP file that fires the MySQL process.

How to remotely connect to OpenShift MySQL database using PHP?

I've a Tomcat 7 application running on Openshift server, say myapp.
I've also installed MySQL 5.5 cartridge and got the username and password say myUsername and myPassword respectively.
I tried to remotely connect to the database by writing a simple PHP script from my php localhost.
<?php
$link = mysql_connect('127.8.217.2:3306', 'myUsername', 'myPassword');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db('myapp',$link) or die ("could not open db".mysql_error());
but the output shows
Could not connect: Access denied for user 'myUsername'#'localhost' (using password: YES)
I don't know what i am missing.
Now the question part.
1) Is it possible to remotely connect to the OpenShift MySQL database using PHP from either localhost or some other server ? If yes, How ?
If there's any question, shoot it into the comments.
EDIT
I've looked at port-forwarding with the help of this thread. but that's not i want.
Based on your explanation as having a need for a both Java and PHP application needing to connect to same server, your options are either
1) doing port forwarding or connecting through SSH, both explained in this link I offered as a duplicate of this, or
2) Trying something explained here and here to create a shared database using a scalable application - however, people in other similar threads, like this one, have claimed that it is "for Openshift/Redhat internet environments, not for regular customers of Openshift" (source). Don't know if it is so, but at least if you go this way, you should test from another openshift gear, not from local computer.

MySQL remote access trouble?

I have a database set up on a godaddy server. It is configured to allow remote access, and there are a couple of websites I'm running which need to access this data. It works when accessed from another godaddy site, and I can connect from my development environment both at work and home. We recently set up hosting with mydomain.com.
Here is the code block that triggered it:
function connect(){
$servername = "XX.XX.XXX.XX";
$dbusername = "databaseusername";
$dbpassword = "mahpassword";
$dbname = "databasename";
try{
$newMysql = new PDO("mysql:host=".$servername.";dbname=".$dbname, $dbusername, $dbpassword);
}
catch(PDOException $e){
echo 'connection Failed: '. $e->getMessage();
die;
}
}
and now I'm getting this error message on the new site:
connection Failed: SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'XX.XX.XXX.XX' (111)
The only problems I can think of is that either for some reason there are a limited number of IP addresses the MySQL database will connect to by default (which seems squirrely), I'm getting blocked by a firewall on the MySQL server (again.. doesn't make sense to me), or there is some setting on the mydomain hosting server disallowing remote requests (?)
I'm new to this kind of thing, so I'm open to any suggestions. I could probably just set up another database on the new site, but I don't want the hassle of keeping them synchronized if I don't need to. What might be wrong? Are there any workarounds?
[edit]
connected to remote database via console (mysql -h XX.XX.XXX.XX ...), the privileges were found under the information_schema database, a quick select * from SCHEMA_PRIVILEGES and select * from USER_PRIVILEGES shows that 'databaseusername'#'%' has sufficient privileges. Not that it helped me any, but maybe it'll help someone down the road.
[/edit]
As it has been more than a year since I asked this question, I suppose I need to answer it just to close it.
It turns out that godaddy had blocked mydomain.com servers via firewall ("Remote access" was limited). so in order to accomplish what I wanted to do, I had to copy and store the database on both sites.

Categories