ERROR 1044 (42000): Access denied for user - mysql

I cannot figure out how to log into my database, I keep getting errors and cannot execute any queries on the database, here is an example of my error.
HW-001b63b70b4c:~ imac$ /usr/local/mysql/bin/mysql;
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.5.13 MySQL Community Server (GPL)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
+--------------------+
2 rows in set (0.00 sec)
mysql> create database dev;
ERROR 1044 (42000): Access denied for user ''#'localhost' to database 'dev'
mysql>

What about logging in as root:
/usr/local/mysql/bin/mysql -u root

Related

MYSQL set a default database

When I connect my MYSQL , I have to choose database to use everytime using :
use mysql_crashcourse;
I noticed the DATABASE() returns null when I check it right after I connect to MYSQL
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| NULL |
+------------+
My question is : how can I set a default MYSQL database ,so that I don't need to provide database info each time I connect or login to MYSQL ?
If you log-in directly from cli.
Create a client session on my.cnf and restart mysql service.
Note every root user logged from localhost will have the default database.
Example:
root#ergesttstsrv:~# cat /etc/mysql/my.cnf
[client]
host=localhost
user=root
password=
database=gesti
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
root#ergesttstsrv:~# service mysql restart
root#ergesttstsrv:~# mysql -u root -p
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.25 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select database();
+------------+
| database() |
+------------+
| gesti |
+------------+
1 row in set (0.00 sec)
If you do not select anything else in the query, the return value is null. If you want to get anything else, you can do for example:
mysql> SELECT 1 IS NULL, 1 IS NOT "something";

Cant login to mysql new user?

I've recently downloaded and started using mysql. I've set it up hopefully correct and my root user also logs in. The mysql service is also running.
I ran the following lines to create a datbase and new user, but I could not log in to my new user. Please help. I don't know what I'm doing wrong.
I've searched for a solution online but get confused as to what is the right steps to follow.
Thank you :)
PS C:\Users\Akhil> mysql -u root -p;
Enter password: *****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 5.6.41-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mytestdb |
| performance_schema |
| sakila |
| test |
| world |
+--------------------+
7 rows in set (0.00 sec)
mysql> use mytestdb
Database changed
mysql> create user myuser identified by 'mypass';
ERROR 1396 (HY000): Operation CREATE USER failed for 'myuser'#'%'
mysql> DROP USER myuser;
Query OK, 0 rows affected (0.00 sec)
mysql> create user myuser identified by 'mypass';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON mytestdb.* TO myuser;
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
PS C:\Users\Akhil> mysql -u myuser -p
Enter password: ******
ERROR 1045 (28000): Access denied for user 'myuser'#'localhost' (using password: YES)
PS C:\Users\Akhil>
Why am I getting access denied is beyond me...Help?
I've solved the error. What I did was Login with root user. Then type the following command:-
DROP USER ''#localhost ;
then it worked like a charm!

Do we need to run flush privileges after changing variable default_password_lifetime in MySQL?

MySQL has a new variables in MySQL-5.7 which keeps the mysql user's password expiration details - after how many days the password expires for a particular user.
Detail of this variables : Doc
When we changed this variable, is it necessary to run flush privileges or the changes will take affect immediately for all the users having default expire policy ?
According to the documentation, it should not be necessary:
6.3.6 Password Expiration Policy
...
When a client successfully connects, the server determines whether the
account password is expired:
The server checks whether the password has been manually expired and, if so, restricts the session.
Otherwise, the server checks whether the password is past its lifetime according to the automatic password expiration policy. If so,
the server considers the password expired and restricts the session.
...
IMPORTANT: The change take effect only for subsequent connections.
Here an example:
$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.18
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SELECT ##GLOBAL.default_password_lifetime;
+------------------------------------+
| ##GLOBAL.default_password_lifetime |
+------------------------------------+
| 0 |
+------------------------------------+
1 row in set (0.00 sec)
mysql> CREATE USER 'johndoe'#'localhost'
-> IDENTIFIED WITH mysql_native_password AS '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4'
-> PASSWORD EXPIRE DEFAULT;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
$ mysql -u johndoe -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.18
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SELECT CURRENT_USER();
+-------------------+
| CURRENT_USER() |
+-------------------+
| johndoe#localhost |
+-------------------+
1 row in set (0.00 sec)
mysql> exit
Bye
$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.18
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SET ##GLOBAL.default_password_lifetime := 1;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT ##GLOBAL.default_password_lifetime;
+------------------------------------+
| ##GLOBAL.default_password_lifetime |
+------------------------------------+
| 1 |
+------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2010-01-01 00:00:01 |
+---------------------+
1 row in set (0.00 sec)
mysql> \! date -s "2010-01-02 $(date +%H:%M:%S)"
Sat Jan 02 00:00:05 UTC 2010
mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2010-01-02 00:00:06 |
+---------------------+
1 row in set (0.01 sec)
mysql> exit
Bye
$ mysql -u johndoe -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.7.18
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SELECT CURRENT_USER();
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
Other information of interest:
6.2.6 When Privilege Changes Take Effect.
I think the answer is no.
because default_password_lifetime is a global variables,which information stored in information_schema.GLOBAL_VARAIBLES,It's a memory engine table!
In mysql,the flush action cause the data in the buffer write back into disk,it's only necessary in MyISAM engine;however, the flush privileges clause is reload the data from disk MyISAM file to memory about account & privileges related table.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-db'

I'm trying to delete a database inside an RDS MySQL instance and I get the following error . Can someone kindly help me resolve this issue
[ec2-user#ip-10-10-1-14 ~]$ mysql -h wda2d.cmkridjjwpjp.ap-southeast-2.rds.amazonaws.com -u happy-p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6241
Server version: 5.6.19-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+-----------------------+
| Database |
+-----------------------+
| information_schema |
| database |
| innodb |
| mysql |
| performance_schema |
| richard-db |
| word-db |
| wordpress-db |
+-----------------------+
10 rows in set (0.00 sec)
mysql> DROP DATABASE wordpress-db;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-db' at line 1
mysql> Ctrl-C -- exit!
Aborted
Dashes are not valid identifier characters unless you wrap them in ticks. (Otherwise they represent the math operation of subtraction).
DROP DATABASE `wordpress-db`;
I recommend renaming those tables to follow best practices and use underscores instead.

MySQL grant ineffective?

Okay, I'm perplexed. I'm getting an "Access denied" error trying to do an update. There's a grant to allow that user to perform an update on that table, but it's being denied anyway. Yes, I've tried "flush properties".
$ mysql -h DBHOST -u DBUSER -p DBNAME
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7340
Server version: 5.0.77 Source distribution
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> update user set lastlogin = now() where userid = 1;
ERROR 1227 (42000): Access denied; you need the SUPER privilege for this operation
mysql> show grants for DBUSER;
+--------------------------------------------------------------------------------------+
| Grants for DBUSER#% |
+--------------------------------------------------------------------------------------+
...
| GRANT SELECT, INSERT ON `DBNAME`.`useroldpassword` TO 'DBUSER'#'%' |
...
| GRANT SELECT, INSERT, UPDATE ON `DBNAME`.`user` TO 'DBUSER'#'%' |
...
+--------------------------------------------------------------------------------------+
68 rows in set (0.01 sec)
mysql>
There is a trigger on the table:
CREATE TRIGGER DEFINER=`DEFINER`#`localhost` UserPasswordUpdate BEFORE UPDATE ON User
FOR EACH ROW
BEGIN
DECLARE count int;
IF(NOT NEW.Password<=>OLD.Password) THEN
SELECT count(*) into count FROM UserOldPassword WHERE UserID=NEW.UserID AND Password=NEW.Password;
IF(count != 0) THEN
INSERT INTO Unknown VALUES(1);
END IF;
INSERT INTO UserOldPassword(UserID,PasswordDate,Password) VALUES(NEW.UserID, NOW(), NEW.Password);
SET NEW.LastPasswordChangeDate=NOW();
END IF;
END
Both the executing user (see above) and the stated definer should have permissions to insert into the UserOldPassword table:
mysql> show grants for DEFINER;
+---------------------------------------------------------------------------------+
| Grants for DEFINER#% |
+---------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'DEFINER'#'%' IDENTIFIED BY PASSWORD '...' |
| GRANT ALL PRIVILEGES ON `DBNAME`.* TO 'DEFINER'#'%' |
+---------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql> show grants for DEFINER#localhost;
+-----------------------------------------------------------------------------------------+
| Grants for DEFINER#localhost |
+-----------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'DEFINER'#'localhost' IDENTIFIED BY PASSWORD '...' |
| GRANT ALL PRIVILEGES ON `DBNAME`.* TO 'DEFINER'#'localhost' |
+-----------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
Try:
GRANT SUPER ON `DBNAME`.`user` TO 'DBUSER'#'%'
You probably have a trigger on the table which is why it's not working.
I upgraded from MySQL 5.0.77 to 5.5.15 and now it works fine.
Run this:
mysql> flush privileges;