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
Related
I have a .php file which should receive and show data from a remote database. I run my program from PHPStorm (which is connected to the remote database through the "Database" right-hand pane) and a browser. Both ways I get an error which depends on the number of arguments I pass to pg_connect() function.
If I use
$dbconn = pg_connect("host=pg hostaddr=server.address.ru port=5432 dbname=studs user=... password=...")
than the error is
Unable to connect to PostgreSQL server: could not parse network address "server.address.ru": Unknown host in...
But I am sure that I wrote the address correctly (there are no typos in it). This way I am not sure about the correctness of the format of the passed arguments.
If I use
$dbconn = pg_connect("host=server.address.ru dbname=studs user=... password=...")
command, the error is
pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Connection timed out
I found a lot of information about this errors, but it mostly refers to localhosts and doesn't solve my problem. I guess, the problem can be in the way this connection is set in the function, but I do not know why it doesn't work properly. How can I solve it?
Thanks to #TangentiallyPerpendicular, I got on a right way of setting the connection. But since I have PostgreSQL remote connection, it wasn't just up to this answer.
What I did and how I set the connection (I work from PHPStorm so all the actions are based on this platform):
Open PuTTY and set an SSH connection (an SSH tunnel) between the server's DB port (for PostgreSQL it's usually 5432) and your local computer's PostgreSQL port (5432 most often too). You can do the same from a command line.
Open PHPStorm and in "Database" section (an icon on the right-hand side of the environment or "Data Sources and Drivers" in Settings) set general information ("General" section) and set another SSH tunnel ("SSH/SSL"). In SSH Configurations (the same "SSH/SSL" section) set a local port - it will be important in our connection! Let's say, this port is 20000. The port of the server you're connecting to is a usual one (usually 22 or 2222).
In the program the right use of function is $dbconn = pg_connect("host=localhost port=20000 dbname=studs user=... password=...") or die('Error: ' . pg_last_error());
The connection is set.
For those who has troubles setting an SSH tunnel with a remote PostgreSQL from PHP this can be useful too.
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();
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.
I have a wierd problem. I have a service running using PostgreSQL 8.3. On parallel platform I have installed PostgreSQL 9.1 and try to connect to it using my working php script. Got an answer:
pg_connect(): Unable to connect to PostgreSQL server:
FATAL: no pg_hba.conf entry for host "X.X.X.X",
user "postgres", database "dbtrsy", SSL off
My pg_hba.conf contains:
host all all X.X.X.X/32 password
host all all Y.Y.Y.Y/32 password
I can easily connect to DB using PgAdmin from Y.Y.Y.Y. Now the wierd thing: I look at the log and I can see:
LOG: connection received: host=X.X.X.X port=55696
LOG: connection authorized: user=postgres database=dbtrsy
LOG: disconnection: session time: 0:00:00.017 user=postgres database=dbtrsy host=X.X.X.X port=55696
Any clue why I got disconnected automatically after being authorized correctly? When I try to login with different password / username it throw "password authentication failed" error whis is correct.
Any idea? Thanks!
If I were to guess, I would guess that something is causing the pg_hba.conf to be read incorrectly, perhaps exposing some sort of bug in the underlying system, or perhaps with another non-bug problem.
My recommendation for a problem of this sort is to take it up on the PostgreSQL email lists, and be prepared to show your entire configuration. You could also try commenting out lines in the pg_hba.conf until you find the problem. If your configuration is sufficiently sensitive, you may want to run another server with the same configuration, but different IP addresses on the client, to create a self-contained test case.
I am attempting to use PHP's PDO_DBLIB driver to connect to a remote database and am having some issues.
The database is connectable via the same environment using telnet and a SQL client. However, connecting using the following code in PHP does not work:
<?php
$conn = new PDO('dblib:dbname=TestDB;host=RemoteServer;charset=utf8', 'my_user', 'my_pass');
Running this code, whether it be from the command line or Apache, yields the following error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)'
I am able to connect to the database using the same code on a different web server, which leads me to believe that it's a configuration issue. The php.ini files on the two servers look relatively the same. They each have the same PDO libraries enabled with the same options configured.
Does anyone have any idea why this could be happening?
Turns out that it was a much simpler issue than I thought. For whatever reason, the development server was not using Port 1433 as the default port in the connection and was instead using Port 4000.
I discovered this by enabling the logs in the freetds.conf file and monitoring them as I was making the request.
Also, something to note: The DBLIB extension uses a colon (:) as a separator between the host and the port instead of the comma. Unfortunately, the error that you receive when you use a comma isn't very descriptive, so hopefully someone benefits from this discovery.
Write port into freetds.conf directly for this host:
[RemoteServer]
host = RemoteServer
port = 1433
And leave php-code us is:
$conn = new PDO('dblib:dbname=TestDB;host=RemoteServer;charset=utf8', 'my_user', 'my_pass');