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.
What am I doing wrong here, how to fix this?