-1

I have changed my server to a new one and after copying my files to the new server and setting configuration I have a problem with the connection to MySQL using PHP.

I can still connect to my database using my username and password using Bash command line or MySQL Workbench. I'm stuck with it. Any suggestions?

Server ubuntu 16.04, Http server: Nginx

PHP version: 5.6

MySQL version: 5.7.20

My PHP config file:

define("DB_HOST", "localhost");
define("DB_NAME", "zlproject");
define("DB_USER", "username");
define("DB_PASS", "password");
define("DB_PORT", "3306");

My connection script:

public function connect() {

    if (!isset($this->connection)) {
        $this->connection = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";dbport=" . DB_PORT . ";charset=utf8", DB_USER, DB_PASS);
    }

    return $this->connection;
}

Error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost' (using password: YES)'

UPDATE

I resolve my problem and it will be a lesson for me. My defined password in config file contained $ sign so half of my stored passwords was tried to read like a another variable and it caused error.

L. Z.
  • 31
  • 5
  • are you sure, you are providing correct username and password ?? – Ravi Jan 07 '18 at 14:34
  • @Ravi Yes, I tried to connect with same username and password over bash command line and mysql workbench and its working fine. – L. Z. Jan 07 '18 at 14:36
  • can you post the screenshot of bash where you are passing same username and password ? – Ravi Jan 07 '18 at 14:38
  • @Ravi Im connecting using putty to my VPS server then I type `mysql -u username -p` then typing password and its working – L. Z. Jan 07 '18 at 14:43
  • if you are doing everything right, then you shouldn't get error. – Ravi Jan 07 '18 at 14:44
  • Try giving only the dbname (and use unix domain socket this way). new PDO("mysql:dbname=" . DB_NAME, DB_USER, DB_PASS); – YaniMan Jan 07 '18 at 14:45
  • @YaniMan Still the same error – L. Z. Jan 07 '18 at 14:57
  • Try using '127.0.0.1' instead of 'localhost'. On some configuration it do the trick. – ino Jan 07 '18 at 15:06
  • @ino I tried now change my DB_HOST from 'localhost' to '127.0.0.1' and still same error – L. Z. Jan 07 '18 at 15:09
  • So connect from Bash and run command: `SHOW GRANTS;` and paste here the results. – ino Jan 07 '18 at 15:13
  • `GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' WITH GRANT OPTION` `GRANT ALL PRIVILEGES ON `zlproject`.* TO 'username'@'localhost'` `GRANT PROXY ON ''@'' TO 'username'@'localhost' WITH GRANT OPTION ` – L. Z. Jan 07 '18 at 15:17
  • You said that you connect via putty to VPS server. Are you using any port forwarding? Is the port 3306 of the VPS MySQL? – ino Jan 07 '18 at 15:25
  • @ino Yes, MySQL working on port 3306. Not using port forwarding – L. Z. Jan 07 '18 at 15:28
  • are you using the script from the same server? or the new server had the database and the old had the script? in the 2nd case you should ad privilegies for the old server's ip to mysql or will be banned if i remember well i found something here https://stackoverflow.com/questions/14779104/how-to-allow-remote-connection-to-mysql i am not sure if is working =i am not mysql guru ,just a programmer –  Jan 07 '18 at 21:48

1 Answers1

0

Password contained $ sign in my php config file.

For example:

define ("DB_PASS", "example$example");

Server in that case will read that password as example + variable $example.

And it cause a error for me when trying to connect via PHP and not when trying to connect using bash command line to MySQL server.

L. Z.
  • 31
  • 5