2

Im using LEMP (Ubuntu Linux server, Nginx, Mysql, PHP) in my machine

I have followed this tutorial to set it up https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-ubuntu-18-04

I can access mysql using command line (mysql -u user -p)

but if i run the php file in the commend line, or load it in the browser I get the following error

running the file in command line

php index.php
PHP Notice: PHP Warning: mysqli_connect(): (HY000/1045): Access denied for user 
'my_username'@'localhost' (using password: YES) in index.php on line 13

PHP code

$localhost = "localhost";
$username = "my_username";
$pass = "my_password";
$database = "my_database";
$con =  mysqli_connect($localhost, $username, $pass, $database) or die("not 
connect". mysqli_connect_error());

This answers didnt really help me MySQL ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES) I think my problem is that i need to enable mysql to allow php communications some how?

peter
  • 23
  • 3

1 Answers1

0

An Access denied error can have many causes. Often the problem is related to the MySQL accounts.

In this case if you have a variable declared as $pass = "pas$w" you will get a E_NOTICE. Undefined variable: w. But declaring that like 'pas$w' would be ok.

MySQL 5.7 (replace for your mysql version) MySQL Docs describe some courses of action you can take to correct the problem.

a_e
  • 880
  • 2
  • 12
  • 23
  • what I find weird is I can access the user mysql -u user -p, using ssh to the server. only once I ran the php file I get this error, or try to load my webpage – peter Feb 10 '19 at 14:56
  • Did you grant access to that user? – a_e Feb 10 '19 at 14:58
  • yes I did, I can access the database using (mysql -u ser -p) create new table etc. – peter Feb 10 '19 at 14:59
  • Just for testing, Have you try to connect using mysql root user? – a_e Feb 10 '19 at 15:03
  • Yes I have and still same problem, one thing that just came to my attention my password contains the $ sign, im thinking that might have an affect on php when reading the password. will remove it and try again – peter Feb 10 '19 at 15:05
  • If you have something like $pass = "pas$w" you will get a E_NOTICE. Undefined variable: w. But declaring that like 'pas$w' is ok. – a_e Feb 10 '19 at 15:08
  • *p&*@bc17-G I get the password back but without the $ sign :) – peter Feb 10 '19 at 15:10
  • Hopefully this is the problem : – peter Feb 10 '19 at 15:10
  • Thanks for the tutorial I will read that too, as i am new to this – peter Feb 10 '19 at 15:11
  • Will change now give me a sec – peter Feb 10 '19 at 15:13
  • Yep it worked :) I cant believe I spend 2 days trying to figure this out, and the problem was the damn password containing $ sign – peter Feb 10 '19 at 15:17
  • I'll update the answer, so others can get benefit and be marked. – a_e Feb 10 '19 at 15:18