ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'db'
No, you should run mysql -u root -p
in bash, not at the MySQL command-line.
If you are in mysql, you can exit by typing exit.
MySql Access denied for user ''@'localhost' to database
I actually used the wrong username. To check the username and servername just run the query given below-
select CURRENT_USER();
In general, username set as root
and server name set as localhost
by default.
MySQL Error: : 'Access denied for user 'root'@'localhost'
- Open and edit
/etc/my.cnf
or/etc/mysql/my.cnf
, depending on your distribution. - Add
skip-grant-tables
under[mysqld]
- Restart MySQL
- You should be able to log in to MySQL now using the below command
mysql -u root -p
- Run
mysql> flush privileges;
- Set new password by
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';
- Go back to /etc/my.cnf and remove/comment skip-grant-tables
- Restart MySQL
- Now you will be able to login with the new password
mysql -u root -p
MySQL ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)
You probably have an anonymous user ''@'localhost'
or ''@'127.0.0.1'
.
As per the manual:
When multiple matches are possible, the server must determine which of
them to use. It resolves this issue as follows: (...)
- When a client attempts to connect, the server looks through the rows [of table mysql.user] in sorted order.
- The server uses the first row that matches the client host name and user name.
(...)
The server uses sorting rules that order rows with the most-specific Host values first.
Literal host names [such as 'localhost'] and IP addresses are the most specific.
Hence, such an anonymous user would "mask" any other user like '[any_username]'@'%'
when connecting from localhost
.
'bill'@'localhost'
does match 'bill'@'%'
, but would match (e.g.) ''@'localhost'
beforehands.
The recommended solution is to drop this anonymous user (this is usually a good thing to do anyways).
Below edits are mostly irrelevant to the main question. These are only meant to answer some questions raised in other comments within this thread.
Edit 1
Authenticating as 'bill'@'%'
through a socket.
root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -ppass --socket=/tmp/mysql-5.5.sock
Welcome to the MySQL monitor (...)
mysql> SELECT user, host FROM mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| bill | % |
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
+------+-----------+
4 rows in set (0.00 sec)
mysql> SELECT USER(), CURRENT_USER();
+----------------+----------------+
| USER() | CURRENT_USER() |
+----------------+----------------+
| bill@localhost | bill@% |
+----------------+----------------+
1 row in set (0.02 sec)
mysql> SHOW VARIABLES LIKE 'skip_networking';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| skip_networking | ON |
+-----------------+-------+
1 row in set (0.00 sec)
Edit 2
Exact same setup, except I re-activated networking, and I now create an anonymous user ''@'localhost'
.
root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql
Welcome to the MySQL monitor (...)
mysql> CREATE USER ''@'localhost' IDENTIFIED BY 'anotherpass';
Query OK, 0 rows affected (0.00 sec)
mysql> Bye
root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -ppass \
--socket=/tmp/mysql-5.5.sock
ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)
root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -ppass \
-h127.0.0.1 --protocol=TCP
ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)
root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -ppass \
-hlocalhost --protocol=TCP
ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)
Edit 3
Same situation as in edit 2, now providing the anonymous user's password.
root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -panotherpass -hlocalhost
Welcome to the MySQL monitor (...)
mysql> SELECT USER(), CURRENT_USER();
+----------------+----------------+
| USER() | CURRENT_USER() |
+----------------+----------------+
| bill@localhost | @localhost |
+----------------+----------------+
1 row in set (0.01 sec)
Conclusion 1, from edit 1: One can authenticate as 'bill'@'%'
through a socket.
Conclusion 2, from edit 2: Whether one connects through TCP or through a socket has no impact on the authentication process (except one cannot connect as anyone else but 'something'@'localhost'
through a socket, obviously).
Conclusion 3, from edit 3: Although I specified -ubill
, I have been granted access as an anonymous user. This is because of the "sorting rules" advised above. Notice that in most default installations, a no-password, anonymous user exists (and should be secured/removed).
Spring Boot (Access denied for user ''@'localhost' to database 'myappdb')
You can see an error message in the logs saying Access denied for user ''@'localhost' to database 'myappdb'
which means that you are trying to access your database as '' (no user), which in turn means that your connection configuration might be an issue.
I would say that this part:
spring.datasource.data-username=root
spring.datasource.data-password=
Should go something like this:
spring.datasource.username=root
spring.datasource.password=yourpass
Also, make sure you are connecting to the correct port of the database, this line over here:
spring.datasource.url=jdbc:mysql://localhost/myappdb?useSSL=false
It would be localhost:3306
for mysql.
access denied for user @ 'localhost' to database ''
Try this: Adding users to MySQL
You need grant privileges to the user if you want external acess to database(ie. web pages).
Error 1044 42000 : Access denied for user ''@' localhost' to database
Entering mysql without a special user is not working. Or better to say: You enter the database, but the user you use then, has nearly no rights/priviliges. So in the beginning you need to login with your root account with mysql -u root -p
.
Then you can create your databases and work with them.
Hint 1: Never work with the root user in a productive system, but create an user, that just has access to one database.
Hint 2: Change the password of the root user as soon as possible with UPDATE user SET Password=PASSWORD('mein_pwd') WHERE user='root';
when you're in the database.
Access denied for 'user'@'localhost'
I don't think this is a port issue, the request is reaching it's destination. Using root@localhost
will work when logging in through the command line (mysql -u root -p
) but you don't want to use it for connecting with your code. Keep in mind that when establishing your connection, you need to use host=localhost
or host=127.0.0.1
explicitly. If you use the IP address (even on the same server), you will get access denied.
[user@localhost ~]# mysql --host=127.0.0.1 --protocol=TCP -u root -p
Enter password:
mysql>
[user@localhost ~]# mysql --host=192.168.1.10 --protocol=TCP -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'hostname' (using password: YES)
Here are the steps I'd recommend taking:
- Create a dedicated user that you can use for connecting in your scripts.
If source of script is same server as MySQL.
CREATE USER '<user>'@'localhost'
IDENTIFIED BY 'password';
GRANT ALL
ON <database>.*
TO '<user>'@'localhost';If the connection is always established from the same place but a different location than MySQL, run the following on the command line.
CREATE USER '<user>'@'<IP_address>'
IDENTIFIED BY 'password';
GRANT ALL
ON <database>.*
TO '<user>'@'<IP_address>';If the source of the connection varies, run the following command.
CREATE USER '<user>'@'<IP_address>'
IDENTIFIED BY 'password';
GRANT ALL
ON <database>.*
TO '<user>'@'%';
Here is a link to the documentation if you have any questions.
mysql Access denied for user ''@'localhost' to database ''
Try to connect with following
mysql_connect("localhost","root", '') or die(mysql_error())
MYSQL is Deprecated So please use mysqli
Related Topics
Create a Zip File and Download It
How to Check If the Request Is Made via Ajax in Codeigniter
How to Loop Through a MySQL Query via Pdo in PHP
When Should I Declare Variables in a PHP Class
Type Casting for User Defined Objects
What Is the Equivalent of JavaScript's Encodeuricomponent in PHP
Download Files from Server PHP
Configure Multiple Databases in Zf2
How to Check If $_Get Is Empty
PHP Can't Connect to MySQL with Error 13 (But Command Line Can)
Codeigniter: Load Controller Within Controller
How to Place Two Forms on the Same Page
PHP Substr() Function with Utf-8 Leaves � Marks at the End