Php Mongo Driver Is Not Making Connection to Authenticated Mongo Server - php

I am trying to make connection to remote MongoDb host through PHP. The server has authentication enabled on it with a user name and password.
I am trying to connect it through php MongoDB\Driver\Manager class. But Its not giving me any kind of response.
I am trying to do it using following connection uri format:
mongodb://[username:password#]host[:port][/[database][?options]]
according to
https://www.php.net/manual/en/mongodb-driver-manager.construct.php
But there is no response in connection variable where I am receiving it.
<?php
$conn = new MongoDB\Driver\Manager("mongodb://USER:PASSWORD#HOST:PORT/admin")
var_dump($conn);
?>
In the password, there are no special characters except #. Also when I hit this url directly in command line then it gives:
No such file or directory
I think according to the documentation, this connection uri format is correct and it should make the connection. But I am kind of stuck here and don't know what to do further.

Related

PHP ODBC_connect with existing ODBC drivers

I have an ODBC Connection that works:
$cqmconn = odbc_connect("Driver={SQL Server Native Client 10.0};
Server=JOHN-HP;
Database=cqmla;", "sa", "sa") or die ("Unable to connect to CQM site.");
However, I have an existing ODBC Driver called "cqm2" that connects to cqmla, but the line beow does NOT make a connection:
$cqmconn = odbc_connect("cqm2","sa","sa") or die ("Unable to connect to CQM site.");
I can, however, use that ODBC connection (and credentials) with other software and it works fine. What am I missing?
I ask this because while I certainly can use the first method here, I have ANOTHER proprietary system that I cannot get to work, although it does have an ODBC connection I can access via other software using the: $cqmconn = odbc_connect("cqm2","sa","sa") format. If I can get my existing connection to work here, I figure I should then be able to call the other one.
What am I missing?
PS: I've tried adding SQL_CUR_USE_ODBC to the end of the call, but it changed nothing.
Answering my own question:
It seems that odbc_connect() for PHP does NOT recognize User DSNs, only System DSNs. As a result, when I changed the DSN I was trying to use for a connection to a System DSN, I was able to use the: $cqmconn = odbc_connect("cqm2","sa","sa") paradigm.

Cannot connect to Azure MySQL from Laravel app

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"

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.

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

"Invalid ns" when connecting to mongodb server

Im trying to connect to a MongoLabs mongodb server I setup for a small project Im working on. I can connect fine when connecting from my local machine, but when I try to make a connection from my Mediatemple server I get a 'Invalid ns' error:
Unable to connect to MongoDB: Failed to connect to: dsXXXXXX.mongolab.com:27769: send_package: the query returned a failure: Invalid ns [XXXXXX_dev/XXXXXX_dev.$cmd] (code: 16256)
Any suggestions on how to fix this would be a major help.
Perhaps your database name in your connection string or collection name (together they make a namespace) have special characters, and they make the namespace name illegal.
So you should make sure your connection string is ok.
For example, mine looks like
mongodb://mydblogin:mydbpassword#ds044444.mongolab.com:44444/mydatabase
Also make sure that collection name is legal.

Categories