Cannot connect to Azure MySQL from Laravel app - php

I have a Laravel App that is using MySQL as its main DB. We are now trying to implement the Azure MySQL service, but the usernames comes with an '#' in it.
For example, if my server name is "test", then to connect from cli:
mysql -htest.mysql.database.azure.com -u'someuser#test' -p
I can connect from Java and Go with no problem, but Laravel can't handle the '#' in the username. It takes the string after '#' as the host and tries to connect to it, but it doesn't resolve to any IP, so the connection fails.
I have tried to escape the sign like 'user\#test', 'user#test' and 'userU+0040test', but nothing works. The server always returns this error:
SQLSTATE[HY000] [9002] The connection string may not be right. Please visit portal for references
The Azure portal shows the code to connect using mysqli, and that works, but I cannot use that from Laravel (I think?).
How could I make this connection work?
Thanks

Try using double qoutes around your username. Like
e.g "someuser#test"

Related

MySQL connection using my hostname instead of ipv4 address, leading to no access

I recently began a new internship where we use the Laravel PHP framework. When trying to connect to the company's MySQL server, I can connect using the given credentials using a program such as MySQL Workbench because my ipv4 address is whitelisted. When using Laravel, it is using my hostname in the connection string, leading to a no access error and I am not sure how to change that. Does anyone know how to fix this?
SQLSTATE[HY000] [1045] Access denied for user 'xxxx'#'ipxx-xx-xx-xxx.ri.ri.cox.net' (using password: YES)
I had the same issue, my mistake was incorrect username defined for local development vs remote connection, after correcting it, mysql connection picks the ip instead of my hostname. Also the following configuration fix the issue too. original answer here
DATABASE_URL=mysql://username:password#db_host:3306/db_name
Add this to your env variables

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.

Unable to perform any query to Mysql server on Azure from local php server

I am able to establish connection to mysql on Azure from local server. Then I am trying to get data from users table but it is failing. I tried using mysqli and also pdo connections. In any case it is failing. Below is the screenshot of failure. Please give me a solution.
I am using Laravel 5.5.
In laravel controller :
if (DB::connection()->getDatabaseName())
{
return 'Connected to the DB: ' . DB::connection()->getDatabaseName();
}
this connection is established successfully and returning db name
But when tried to query like this, it is throwing error:
if (DB::connection()->getDatabaseName())
{
return DB::select('select * from `users`');
}
ERROR MESSAGE :
Illuminate\Database\QueryException: SQLSTATE[HY000] [2002] (SQL: select * from `users`) in file C:\SIRI-22\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 664
The same code is working fine when connected to local mysql server.
AS I AM NOT ALLOWED TO POST ANSWER TO MY QUESTION, I AM POSTING SOLUTION TO THIS ISSUE HERE:
I resolved the issue. It is explained in the comments section of azure website (https://learn.microsoft.com/en-us/azure/mysql/howto-configure-ssl).
Explanation :
You might experience problems if you're trying to connect to Azure Database for MySQL over SSL from PHP through MySQLi or PDO. The problem in both cases is that the SSL certificates used by Azure Database for MySQL do not match the hostnames of the servers you're connecting to, and hence server certificate verification fails. (Many other clients will happily connect over SSL without verifying the server certificate, which only makes it harder to figure out what is wrong.)
Fortunately, this can be solved if you're running at least PHP 5.6.16 for MySQLi or PHP v7.1.4 for PDO and you're using the MySQL Native Driver--and Azure App Service meets these requirements. For MySQLi, you will need to add the MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT flag when you connect. For PDO, you will need to set the PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT option to false when you connect.
See https://bugs.php.net/bug.php?id=68344 and https://bugs.php.net/bug.php?id=71003 for details.
This is explained by Warlock. Thanks to him !
Had you check your Azure Firewall connection of your MySQL server, since Microsoft Azure doesn't allow any external connection by default.
You have error in your query builder. You can try
DB::table("users")->get();

phpMyAdmin refuses MYSQL connection - How to fix it?

I'm creating a web application in PHP and I need to connect to a database and retrieve information from it. The database in question is being hosted on phpMyAdmin. I'm currently using the following PHP code to connect to it.
//Attempt to connect to the database
$conn = mysql_connect('localhost', 'my_username', 'my_password') or die (mysql_error());
//Tell the user if they were successful
echo "Connection successful!";
//Close the connection
mysql_close();
When I run the website, it produces the following SQL error:
"No connection could be made because the target machine actively refused it."
I'm sure that my username and password are spelled correctly and I believe that 'localhost' is the server name that I need to use. Is there a different mysql_connect command that I need to use for phpMyAdmin? If not, how can I solve this problem?
Edit:
Upon publishing the website to Microsoft Azure (where I need to host it), I've found that it produces a different error:
"An attempt was made to access a socket in a way forbidden by its access permissions."
How will I be able to fix this error? Or will fixing the original error also solve this one?
Do not use mysql_* functions. They are deprecated. For more information, see this question: Why shouldn't I use mysql_* functions in PHP?
phpMyAdmin hosts nothing; it is simply a PHP application that connects to your database just like your own app is attempting to do.
Take a look at the phpMyAdmin config file and ensure you are using the same host. Also try the same username/password. If this succeeds, it's advisable to set up a dedicated username/password for your application. This can be done within phpMyAdmin on the privileges page.
Try use IP instead of localhost.
User has permissions? Check them

How to resolve mysql connection error?

I have an application designed in mysql and php. Earlier this app was working fine. But now it is not. Its giving me an error :
***Failed to connect to MySQL: Can't connect to local MySQL server through socket
'/var/lib/mysql/mysql.sock' (2)***
How can I resolve this error ?
The application is running on a redhat machine.
Does the directory "/var/lib/mysql" exist? also please check your mysql user (or better, mysql group) have writer permission. If so, please try re-starting mysql and see what happens
You must follow the below steps to debug the error:-
start mysql engine.
check below database connecting variable
a. host name
b. username
c. password
d. database name
now check query for connecting with mysql.
now check whether it's able to connect with database or not.
I think this will help You to resolve the problem. If it persists then let me know.
Reference link you may find the solution.According to the link it says:
Are you connecting to "localhost" or "127.0.0.1" ? I noticed that when you connect to "localhost" the socket connector is used, but when you connect to "127.0.0.1" the TCP/IP connector is used. You could try using "127.0.0.1" if the socket connector is not enabled/working.
i think your mysql server is not running. Make sure your mysql server is running
use
# mysqladmin -u root -p status
to check status
if it is running , try to connect from command line. And execute some simple select statement from command line. If your are able to do that, make sure that username and password (used in command line) are used in your application too.
If your are application working sometime and sometime not, in that case make sure you are closing connections in your program after use.

Categories