1

Resloved: I used 127.0.0.1 instead of localhost.

I successfully access mysql database globe_bank using username and password in the mac terminal:

Ashlys-MBP:~ Ashly$ mysql -u Webuser -p globe_bank
Enter password: 

mysql>

However, I can't login from php, using the same username and password. Here's the warning message shown in the browser:

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'webuser'@'localhost' (using password: YES) in /Applications/MAMP/htdocs/Globe_bank/private/database.php on line 6

Here's my code:

define("DB_SERVER", "localhost");
define("DB_USER", "Webuser");
define("DB_PASS", "7654321aA!");
define("DB_NAME", "globe_bank");

  function db_connect() { 
    $connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME); //line 6
    return $connection;
  }

  function db_disconnect($connection) {
    if(isset($connection)) {
      mysqli_close($connection);// line 12
    }
  }

Also

mysql> select user, host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| Webuser          | localhost |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

and

mysql>  SELECT USER(), CURRENT_USER();
+-------------------+-------------------+
| USER()            | CURRENT_USER()    |
+-------------------+-------------------+
| Webuser@localhost | Webuser@localhost |
+-------------------+-------------------+
1 row in set (0.00 sec)

And I checked the grants for Webuser:

mysql> SHOW GRANTS FOR 'Webuser'@'localhost';
+---------------------------------------------------------------------------+
| Grants for Webuser@localhost                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
+---------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `Webuser`@`localhost` WITH GRANT OPTION                                                                                                                                               
| GRANT APPLICATION_PASSWORD_ADMIN,AUDIT_ADMIN,BACKUP_ADMIN,BINLOG_ADMIN,BINLOG_ENCRYPTION_ADMIN,CLONE_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,GROUP_REPLICATION_ADMIN,INNODB_REDO_LOG_ARCHIVE,INNODB_REDO_LOG_ENABLE,PERSIST_RO_VARIABLES_ADMIN,REPLICATION_APPLIER,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SERVICE_CONNECTION_ADMIN,SESSION_VARIABLES_ADMIN,SET_USER_ID,SHOW_ROUTINE,SYSTEM_USER,SYSTEM_VARIABLES_ADMIN,TABLE_ENCRYPTION_ADMIN,XA_RECOVER_ADMIN ON *.* TO `Webuser`@`localhost` WITH GRANT OPTION |

2 rows in set (0.00 sec)

then

mysql> FLUSH PRIVILEGES; 

By the way,

  • mysql version is mysql-8.0.22-macos10.15-x86_64.

  • This is how I create the user in the terminal:

    mysql> CREATE USER 'Webuser'@'localhost' IDENTIFIED BY '7654321aA!';

  • my port should be 8888, I use MAMP, when it started, it shows "localhost:8888/MAMP/?language=English" on the browser.

  • I restarted mysql, the warning persist.

  • Actually, there is another warning message, I figured the reason that cause this warning is because I cannot access the mysql database in the first place. The other warning show in the bottom is (I have mark the line 12 on the above php code)

Warning: mysqli_close() expects parameter 1 to be mysqli, bool given in /Applications/MAMP/htdocs/Globe_bank/private/database.php on line 12

  • found the data directory, | datadir |/usr/local/mysql/data/ |, but "The folder “data” can’t be opened because you don’t have permission to see its contents."

I followed some advices and did some following:

mysql> describe user;

+--------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field                    | Type                              | Null | Key | Default               | Extra |
+--------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host                     | char(255)                         | NO   | PRI |                       |       |
| User                     | char(32)                          | NO   | PRI |                       |       |
| authentication_string    | text                              | YES  |     | NULL                  |       |
| password_expired         | enum('N','Y')                     | NO   |     | N                     |       |
| password_last_changed    | timestamp                         | YES  |     | NULL                  |       |
+--------------------------+-----------------------------------+------+-----+-----------------------+-------+
51 rows in set (0.01 sec)

then

mysql> FLUSH PRIVILEGES;
mysql> UPDATE user SET authentication_string='7654321aA!' WHERE user='Webuser@localhost';

Stop and Restart Mysql. Nothing changed. Same warning.

This is the screenshot

What am I doing wrong here, how to fix this?

Ash3T
  • 11
  • 3
  • Which is line 5? Have you looked at phpAdmin and user accounts? Check here that the user is there. – RGriffiths Nov 12 '20 at 18:42
  • Please avoid saying things like "The previous associated post on stack overflow is not helpful" and instead link to answers that didn't work for you, and further tell us what you tried and why it didn't work. – Chris Haas Nov 12 '20 at 18:45
  • can take a look at the mysql error log – nbk Nov 12 '20 at 18:47
  • Have you tried running `FLUSH PRIVILEGES`? Also, try resetting the password to something very simple just for sanity's sake. – Chris Haas Nov 12 '20 at 18:49
  • do you have an `authentication_string` on user `webuser` in table mysql.user ? look for `identified by` clause on the grant command. – YvesLeBorg Nov 12 '20 at 18:50
  • @RGriffiths function db_connect() { // this is the line 5 on the database.php – Ash3T Nov 12 '20 at 19:53
  • @Chris Haas Got it. linke is here: https://stackoverflow.com/questions/10299148/mysql-error-1045-28000-access-denied-for-user-billlocalhost-using-passw – Ash3T Nov 12 '20 at 19:53
  • @nbk thanks for your suggestion, I checked about how to look for the error log, but I cannot find the /var/log/mysql/mysql.log in mysql folder or subfolders – Ash3T Nov 12 '20 at 20:18
  • @YvesLeBorg thanks, mysql version is mysql-8.0.22-macos10.15-x86_64. I tried all the following: SET PASSWORD FOR 'webuser'@'localhost' = PASSWORD('7654321a'); UPDATE mysql.user SET authentication_string=PASSWORD('7654321a') WHERE user='webuser'; UPDATE mysql.user SET Password=PASSWORD('7654321a') WHERE User='webuser'; and in terminal, all three result in saying that "ERROR 1064 (42000): You have an error in your SQL syntax" – Ash3T Nov 12 '20 at 20:23
  • PASSWORD('7654321a'); doesn't work anymore in mysql 8. Also run SHOW VARIABLES WHERE Variable_Name LIKE "%dir" and see where the data directory is there you find also the error log – nbk Nov 12 '20 at 20:30
  • @nbk Thanks! I find the directory, but it shows that "The folder “data” can’t be opened because you don’t have permission to see its contents." :( I did right-click the folder then choose Get Info. to make changes in Sharing & Permissions section. But it says, "The operation can’t be completed because you don’t have the necessary permission." – Ash3T Nov 13 '20 at 01:04
  • so have you tried specifying port in PHP? Have you tried `define("DB_SERVER", "localhost:8888");`? `define("DB_SERVER", "127.0.0.1:8888");`? – YakovL Nov 13 '20 at 09:34
  • @YvesLeBorg yes, I do. I just used UPDATE user SET authentication_string='7654321aA!' WHERE user='Webuser@localhost'; No luck to get rid of the warning and access to the database. Do I need to change anything on php? I also included other things I did in the end of my post. Thanks for your help! – Ash3T Nov 13 '20 at 17:24
  • @YakovL thanks for your suggestions. I just tried to define("DB_SERVER", "localhost:8888"); define("DB_SERVER", "127.0.0.1:8888"); But it seems the page cannot finish loading. For both of the above two changes, in the left bottom corner of the browser, it shows message "waiting for local host". – Ash3T Nov 13 '20 at 17:30
  • @Ash3T are you sure that the other part of your php script actually finishes and responses to browser? Sounds like `define("DB_SERVER", "localhost:8888");` is actually a solution for your problem with your DB connection and now you have another one which is not related to the first one. You should probably extend your question with a [mcve] so that we can see what script actually gives you such result. – YakovL Nov 13 '20 at 19:42
  • Thanks @YakovL, I just made changed to "display_errors On(Local Value) On(Master Value) on my phpinfo.php" And I uploaded a screenshot of my browser. I am newbie learning php and Mysql from lynda.com, following every step from one course. However, I got stuck here. – Ash3T Nov 14 '20 at 00:38
  • Thanks everyone for pitching in. @YakovL I use this "DB_SERVER", "127.0.0.1". And it works!!! No warnings. Is anyone know why this might happen? And "127.0.0.1:8888" won't work. – Ash3T Nov 14 '20 at 22:48
  • Well, I guess the answer can be found somewhere in https://stackoverflow.com/q/7382602/3995261 , https://www.quora.com/Is-there-any-difference-between-localhost-and-127-0-0-1 Some guys just [recommend](https://www.quora.com/Is-there-any-difference-between-localhost-and-127-0-0-1/answer/Chawanat-Nakasan) to use `127.0.0.1` over `localhost` to avoid confusions caused by rather complicated reasons – YakovL Nov 15 '20 at 10:12

0 Answers0