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.
Related
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.
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.
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"
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();
Following the directions at http://www.doctrine-project.org/projects/orm/1.2/docs/manual/introduction-to-connections/en#dsn,-the-data-source-name:examples, I get the following error
Message: PDO Connection Error: SQLSTATE[HY000] [2005] Unknown MySQL server host 'unix(' (1)
The exact DSN I am using is "mysql://root#unix(/tmp/mysql.sock)/test"
Yes, root with no password via local unix socket.
This exact configuration works with phpMyAdmin, so I know the settings are valid. I've also tried with another user with password and got the same error.
What confuses me is why it's throwing the error about the host, when I'm trying connect via socket as the instructions provided.
Doctrine needs PDO-like query string, see if it matches yours:
http://www.php.net/manual/en/pdo.construct.php
Moreover, have a look at this:
Doctrine (in symfony project) can not connect through socket