Question

The Error: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'tech'@'localhost' (using password: YES)' in .......

I'm able to remotely connect in command line, but not using php, any ideas where to look?

Here are the details:

Webserver A: 1.1.1.1 holds the php script:

$REGIS_DB_SERVER = "2.2.2.2"; //I've tried the IP as well as "pretendhostname.com"
$REGIS_DB_NAME = "db_name";
$REGIS_DB_USER = "tech"; // or any other user name
$REGIS_DB_PASSWORD = "password";

//connect to db
$db = new PDO("mysql:host = $REGIS_DB_SERVER; dbname=$REGIS_DB_NAME; charset=utf8", $REGIS_DB_USER, $REGIS_DB_PASSWORD);
print_r($db -> errorInfo());
echo "Connected<br>";

$sql = "CREATE TABLE Test(PersonID int,LastName varchar(255),FirstName varchar(255),Address varcha(255),City varchar(255))";
$result = $db->query($sql);

echo "$result <br>";

?>

Webserver B: 2.2.2.2 holds the mySQL database: Running Ubuntu 11.04

in /etc/mysql/my.cnf

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#Commented out to allow remote connections to this database
#bind-address           = 216.70.81.240

I run mysql> select Host,User from user; and get:

+-----------------------+------------------+
| Host                  | User             |
+-----------------------+------------------+
| %                     | root             |
| 127.0.0.1             | root             |
| 1.1.1.1               | tech             |
| localhost             | debian-sys-maint |
| localhost             | root             |
| localhost             | tech             |
+-----------------------+------------------+

Open Port 3306 in ubuntu 12.04 to allow connections to mysql from any ip implies it's not an iptable problem and to test out the connection:

I ssh-ed into Webserver A 1.1.1.1 and ran:

mysql -u tech -p -h 2.2.2.2
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 200672
Server version: 5.1.63-0ubuntu0.11.04.1 (Ubuntu)
Was it helpful?

Solution 2

After finding: PDO cannot connect remote mysql server I realized that if the host is not "understood", the PDO connection will default to host=localhost. Looking more closely, I realized there were 2 spaces between the assignment. I did not realize spaces mattered in the syntax.

So in the end, it was a syntax error:

The working solution (by removing spaces):

$db = new PDO("mysql:host=$REGIS_DB_SERVER; dbname=$REGIS_DB_NAME; charset=utf8", $REGIS_DB_USER, $REGIS_DB_PASSWORD);

OTHER TIPS

you need disabled the SELinux, for run the below CMD

setsebool httpd_can_network_connect=1

This is definitely a database permissions/privileges issue. Ask your dba/sysad to GRANT privileges for your local server to access the database

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top