I've a server with postgresql installed on the postgres user. When I do:
su - postgres
[entering the password]
psql
I can then execute queries on the database, that works fine.
However, from my php script (running on my own account):
$dbconnection = pg_connect("host=localhost port=5432 user=postgres password=XXXXXXX ");
(password crossed out of course)
I then get the message:
PHP Warning: pg_connect(): Unable to connect to PostgreSQL server:
FATAL: password authentication failed for user "postgres" FATAL:
password authentication failed for user "postgres" in
/home/username/script.php on line 18
I tried a lot of variations on the login string, but I keep getting the same message. Any ideas how I could try to solve this?
What is the current configuration for pg_hba.conf? And did you set a password for the database rol "postgres" ? su - postgres is for the Linux user "postgres", not for the database role "postgres". If you can start psql without entering the password, it looks like pg_hba.conf uses "trust" and not "password" of even better "md5". Or did you create a .pgpass file?
Your postgres may not listen on a network interface but on a filesystem socket in stead. What if you leave out host=/port= entirely?
Also, if you know the socketpath (and it's non-default), you may use that as host= argument
Be careful, the following (too short) connection string causes the mentioned error message ("Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL: Peer authentication failed for user "halmai" in..."):
pg_connect("dbname=halmai user=halmai password=halmai");
However, the more complete connection string works well:
pg_connect("host=localhost dbname=halmai user=halmai password=halmai");
My environment: php7.0.8.
Related
I have a database with a username, and password. The username is zerk2. I created this user and granted it all permissions known to man. I can use the php my admin panel to physically login to the server and it works every time. However, when I try to use php to log in, or node.js it tells me it cannot connect and gives me the following errors:
NODE JS Error:
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1054:14)
--------------------
at Protocol._enqueue (/rbd/pnpm-volume/2b16f76e-5470-449f-9885-fd3654098407/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/rbd/pnpm-volume/2b16f76e-5470-449f-9885-fd3654098407/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/rbd/pnpm-volume/2b16f76e-5470-449f-9885-fd3654098407/node_modules/mysql/lib/Connection.js:116:18)
at Object.<anonymous> (/app/connection.js:10:5)
at Module._compile (internal/modules/cjs/loader.js:759:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
at Module.load (internal/modules/cjs/loader.js:628:32)
at Function.Module._load (internal/modules/cjs/loader.js:555:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:826:10)
at internal/main/run_main_module.js:17:11
NODE JS Code:
const mysql = require('mysql');
const con = mysql.createConnection({
host: "localhost",
user: "zerk2",
password: "***",
database: "***",
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
PHP Error:
Warning: mysqli_connect(): (HY000/1045): ProxySQL Error: Access denied for user 'zerk2'#'2a02:4780:bad:f00d::7' (using password: YES) in /storage/ssd2/616/14109616/public_html/index.php on line 2
Connection Failed!
PHP Code:
// Note. The stars are there for privacy. However I am certain i have the correct order. Host, username, password, db name
<?php
$con = mysqli_connect("localhost", "zerk2", "***", "***") or die("Connection Failed!");
echo "Connection success!";
?>
Looks like the issue is the host 'zerk2'#'2a02:4780:bad:f00d::7'. You maybe on the same machine but your php and node running from different servers and looks like your node is running via a proxy. php could be running from apache and nodejs from another http server. Not sure how you have set up the server. To fix this you have to modify or create a mysql user with host set to % to allow access from any host.
UPDATES:
It's failing to connect to mysql server Error: connect ECONNREFUSED 127.0.0.1:3306 for NodeJS. Try to use server's ip address or domain as host instead of localhost and see if that works for you. PHP looks like it connects but the user don't have access could be the password or the user can't access it from host 2a02:4780:bad:f00d::7 that's why you'll need a mysql user with host %. Also note it will not work if you have two users with that same name and one's host is not %.
I feel like a total idiot. But the reason it was not working, is because apparently. The web browser FireFox has a sort of built in VPN. That it routes all of your request traffic into. If anyone gets this error, disable aly sort of Proxy, or VPN you might have enabled on your computer or browser.
I have windows 7 system and installed xampp in it.
I can access phpmyadmin using http://ipaddress:8080/phpmyadmin from remote system.
But if i try to access it from php it will give me error :
Warning: mysqli::mysqli(): (HY000/2013): Lost connection to MySQL server at 'reading initial communication packet', system error: 20
My php code is
define("HOST", "X.X.X.X");
define("PORT", "port"); // The host you want to connect to.
define("USER", "user"); // The database username.
define("PASSWORD", "password"); // The database password.
define("DATABASE", "DBname"); // The database name.
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE, PORT);
// or without port $mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
I have also tried firewall turning off, and also allow port 3306 to firewall, but no luck. Granted all PRIVILEGES to this user with host any (%).
I have also tried using bind_address = 0.0.0.0, commenting that line, and also bind_address = X.X.X.X (my IP).
Also tried changing socket type from "MySQL" to "TCP/IP". skip-networking line is also commented default.
I have tried to access from command prompt using:
mysql -h X.X.X.X -u root -p
This will give error:
Warning: mysqli::mysqli(): (HY000/2003): Can't connect to MySQL server on 'X.X.X.X' (110).
Using localhost in same system in connection string it works fine.
Where i am wrong i can't figure out. I have searched and tried all possible solutions.
Thanks for help.
Note: rather than above, right now my conf file is same as at time of installation. Please don't mark this question as duplicate.
Edit: #Jay Blanchard, As i have referred that solution also, in that case system error : 0, in my case system error : 20. Thanks for help.
My research says that error 20 on sql means it is being ran from a user that is not granted system admin rights. This limits the abilities of the sql server. The way to fix this is make sure the user the server is being ran from has admin rights. This can be done in the control panel from a windows user that has admin rights.
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'm trying to connect to an external database through this script:
$dsn = 'mysql:host=xxx.xxx.xxx.xxx;dbname=dbname';
$user = 'user';
$password = 'pass';
try {
$pdo = new PDO($dsn, $user, $password);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
die();
}
Regarding the data (host, username, database and password) everything is correct because when I tip xxx.xxx.xxx.xxx/phpmyadmin on the browser and I enter the user and password it let me in to the database with permissions to create, delete and such.
But when I try to connect through PDO it gives me the "Connection failed: SQLSTATE[28000] [1045] Acess denied for..."
The information displayed on the PHPMYADMIN panel is as follows:
MySQL
Server: Localhost via UNIX socket
Server version: 5.5.20
Protocol version: 10
User: user#localhost
MySQL charset: UTF-8 Unicode (utf8)
Web server
Apache
MySQL client version: 5.5.20
PHP extension: mysqli
Is it possible that I'm forced to use mysqli due to that "PHP extension: mysqli" property?
Thanks in advance!!!
Nothing seems wrong with your code except the setAttribute method is setting the error output to raise warnings.. but you're in a try/catch block.. so you may want to consider using PDO::ERRMODE_EXCEPTION.
Secondly, check your remote permissions for that username/password. There may be an IP limitation for that user account. You can use the wildcard % for the HOST on user accounts or (suggested) provide the server IP that is connecting TO the database.
I would venture a guess that phpmyadmin is working properly because phpmyadmin is on the host you're trying to connect to. My guess is that the server or user account is configured to only have access via 127.0.0.1/localhost - you can another record for the user you are trying to connect with and provide a wildcard or specific host for that user. Based on the information you've provided here, it seems like you'll just have to get MySQL setup to accept connections from other hosts.
Meanwhile I've tried to run the application on local but it seems that it can't connect either, so maybe this can be of some help when it comes to identify the problem above.
This time (in localhost), the error is:
Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'xxxxxxx_xxxxxxx'#'localhost' (using password: YES)
I created the user on my own phpmyadmin granting him all the permissions over this database, so I can't understand why even in localhost I can't connect to it. Can it be related to the problem above or is it something completely different that should go on a different topic? Thank you!
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