Logging into MySQL -u root denied, sudo mysql works - mysql

Currently running Ubuntu 16.04, and have just installed MySQL using
sudo apt-get install mysql-client mysql-server
The root password was left empty when prompted. Accessing using
mysql -u root
produces an error:
ERROR 1698 (28000): Access denied for user 'root'#'localhost'
However, when using sudo, it works just fine:
sudo mysql
# omitted welcome message
mysql>
mysql> -- When prompting for current user:
mysql> SELECT CURRENT_USER();
+----------------+
| CURRENT_USER() |
+----------------+
| root#localhost |
+----------------+
Why is access denied using mysql -u root?

Use the below command
mysql -uroot -p
Press Enter
Then it expects a password. If password is not set, then Press Enter again.

Related

What is the default root pasword for MySQL 5.7

Cannot login to MySQL database after fresh install with root ID and empty/no password like other older MySQL versions do
There's so many answers out there saying to reinstall mysql or use some combo of
mysqld_safe --skip-grant-tables
and / or
UPDATE mysql.user SET Password=PASSWORD('password')
and / or something else ...
... None of it was working for me
Here's what worked for me, on Ubuntu 18.04, from the top
With special credit to this answer for digging me out of the frustration on this ...
$ sudo apt install mysql-server
$ sudo cat /etc/mysql/debian.cnf
Note the lines which read:
user = debian-sys-maint
password = blahblahblah
Then:
$ mysql -u debian-sys-maint -p
Enter password: // type 'blahblahblah', ie. password from debian.cnf
mysql> USE mysql
mysql> SELECT User, Host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| User | Host | plugin |
+------------------+-----------+-----------------------+
| root | localhost | auth_socket |
| mysql.session | localhost | mysql_native_password |
| mysql.sys | localhost | mysql_native_password |
| debian-sys-maint | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
4 rows in set (0.00 sec)
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> COMMIT; // When you don't have auto-commit switched on
Either:
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'new_password';
Or:
// For MySQL 5.7+
UPDATE mysql.user SET authentication_string=PASSWORD('new_password') where user='root';
Then:
mysql> FLUSH PRIVILEGES;
mysql> COMMIT; // When you don't have auto-commit switched on
mysql> EXIT
$ sudo service mysql restart
$ mysql -u root -p
Enter password: // Yay! 'new_password' now works!
After you installed MySQL-community-server 5.7 from fresh on linux, you will need to find the temporary password from /var/log/mysqld.log to login as root.
grep 'temporary password' /var/log/mysqld.log
Run mysql_secure_installation to change new password
ref: http://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html
MySQL 5.7 changed the secure model: now MySQL root login requires a sudo
The simplest (and safest) solution will be create a new user and grant required privileges.
1. Connect to mysql
sudo mysql --user=root mysql
2. Create a user for phpMyAdmin
CREATE USER 'phpmyadmin'#'localhost' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'phpmyadmin'#'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Reference - https://askubuntu.com/questions/763336/cannot-enter-phpmyadmin-as-root-mysql-5-7
MySQL server 5.7 was already installed by default on my new Linux Mint 19.
But, what's the MySQL root password? It turns out that:
The default installation uses auth_socket for authentication, in lieu of passwords!
It allows a password-free login, provided that one is logged into the Linux system with the same user name. To login as the MySQL root user, one can use sudo:
sudo mysql --user=root
But how to then change the root password? To illustrate what's going on, I created a new user "me", with full privileges, with:
mysql> CREATE USER 'me'#'localhost' IDENTIFIED BY 'my_new_password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'me'#'localhost' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
Comparing "me" with "root":
mysql> SELECT user, plugin, HEX(authentication_string) FROM mysql.user WHERE user = 'me' or user = 'root';
+------+-----------------------+----------------------------------------------------------------------------+
| user | plugin | HEX(authentication_string) |
+------+-----------------------+----------------------------------------------------------------------------+
| root | auth_socket | |
| me | mysql_native_password | 2A393846353030304545453239394634323734333139354241344642413245373537313... |
+------+-----------------------+----------------------------------------------------------------------------+
Because it's using auth_socket, the root password cannot be changed: the SET PASSWORD command fails, and mysql_secure_installation desn't attain anything...
==> To zap this alternate authentication mode and return the MySQL root user to using passwords:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'SOME_NEW_ROOT_PASSWORD';
A good explanation.
More details from the MySQL manual.
In case you want to install mysql or percona unattended (like in my case ansible), you can use following script:
# first part opens mysql log
# second part greps lines with temporary password
# third part picks last line (most recent one)
# last part removes all the line except the password
# the result goes into password variable
password=$(cat /var/log/mysqld.log | grep "A temporary password is generated for" | tail -1 | sed -n 's/.*root#localhost: //p')
# setting new password, you can use $1 and run this script as a file and pass the argument through the script
newPassword="wh#teverYouLikE"
# resetting temporary password
mysql -uroot -p$password -Bse "ALTER USER 'root'#'localhost' IDENTIFIED BY '$newPassword';"
MySQL 5.7 or newer generates a default temporary password after fresh install.
To use MySQL first you would be required to get that password from the log file which is present at the /var/log/mysqld.log. So follow the following process:
grep 'temporary password' /var/log/mysqld.log
mysql_secure_installation
The second command is required to change the password for MySQL and also to make certain other changes like removing temporary databases, allow or disallow remote access to root user, delete anonymous users etc…
It seems things were designed to avoid developers to set the root user, a better solution would be:
sudo mysql -u root
Then create a normal user, set a password, then use that user to work.
create user 'user'#'localhost' identified by 'user1234';
grant all on your_database.* to 'user'#'localhost';
select host, user from mysql.user;
Then try to access:
mysql -u user -p
Boom!
None of these answers worked for me on Ubuntu Server 18.04.1 and MySQL 5.7.23. I spent a bunch of time trying and failing at setting the password and auth plugin manually, finding the password in logs (it's not there), etc.
The solution is actually super easy:
sudo mysql_secure_installation
It's really important to do this with sudo. If you try without elevation, you'll be asked for the root password, which you obviously don't have.
After a lot of try, I could reset the default password with the following commands (Ubuntu and derivatives):
sudo -i
mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld
/etc/init.d/mysql stop
mysqld_safe --skip-grant-tables &
mysql -uroot
use mysql;
update user set authentication_string=password('YOURPASSWORD') where user='root';
update user set plugin="mysql_native_password" where User='root';
flush privileges;
quit;
sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start
Sometimes, even after typed in the terminal
mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld
/etc/init.d/mysql stop
mysqld_safe --skip-grant-tables &
I got the error that the mysqld don't exists. So, quit, and type the same commands again.
And the final command
sudo /etc/init.d/mysql start
Sometimes doesn't work. Only after restart the computer.
I just installed Linux Mint 19 (based on Ubuntu 18.04) on my machine. I installed MySQL 5.7 from the repo (sudo apt install mysql-server) and surprisingly during installation, the setup didn't prompt to enter root password. As a result I wasn't able to login into MySQL. I googled here and there and tried various answers I found on the net, including the accepted answer above. I uninstalled (purging all dpkgs with mysql in its name) and reinstalled again from the default Linux Mint repositories. NONE works.
After hours of unproductive works, I decided to reinstall MySQL from the official page. I opened MySQL download page (https://dev.mysql.com/downloads/repo/apt) for apt repo and clicked Download button at the bottom right.
Next, run it with dpkg:
sudo dpkg -i mysql-apt-config_0.8.10-1_all.deb
At the installation setup, choose the MySQL version that you'd like to install. The default option is 8.0 but I changed it to 5.7. Click OK to quit. After this, you have a new MySQL repo in your Software Sources.
Update your repo:
sudo apt update
Finally, install MySQL:
sudo apt install mysql-server
And now I was prompted to provide root password! Hope it helps for others with this same experience.
As of Ubuntu 20.04 with MySql 8.0 : you can set the password that way:
login to mysql with sudo mysql -u root
change the password:
USE mysql;
UPDATE user set authentication_string=NULL where User='root';
FLUSH privileges;
ALTER USER 'root'#'localhost' IDENTIFIED WITH caching_sha2_password BY 'My-N7w_And.5ecure-P#s5w0rd';
FLUSH privileges;
QUIT
now you should be able to login with mysql -u root -p (or to phpMyAdmin with username root) and your chosen password.
P,S:
You can also login with user debian-sys-maint, the password is written in the file /etc/mysql/debian.cnf
To do it in non interactive mode (from a script):
systemctl start mysqld
MYSQL_ROOT_TMP_PSW=$(grep 'temporary password' $logpath/mysqld.log |sed "s|.*: ||")
## POPULATE SCHEMAS WITH ROOT USER
/usr/bin/mysql --connect-expired-password -u root -p${MYSQL_ROOT_TMP_PSW} < "$mysql_init_script"
Here's the head of the init script
SET GLOBAL validate_password_policy=LOW;
FLUSH privileges;
SET PASSWORD = PASSWORD('MYSQL_ROOT_PSW');
FLUSH privileges;
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%';
FLUSH privileges;
...
Then restart the service systemctl restart mysqld
In my case the data directory was automatically initialized with the --initialize-insecure option. So /var/log/mysql/error.log does not contain a temporary password but:
[Warning] root#localhost is created with an empty password ! Please
consider switching off the --initialize-insecure option.
What worked was:
shell> mysql -u root --skip-password
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'new_password';
Details: MySQL 5.7 Reference Manual > 2.10.4 Securing the Initial MySQL Account
I to was experiencing the same problem and the only thing I was able to do to make it work was to go this route:
drop user admin#localhost;
flush privileges;
create user admin#localhost identified by 'admins_password'
This allowed me to recreate my username and enter a password for the user name.

Mysql showing error on console/phpmyadmin : ERROR 1044 (42000): Access denied for user 'root'#'localhost' to database 'test123'

Not able to create,drop database from phpmyadmin or mysql command line. It showing error:
"ERROR 1044 (42000): Access denied for user 'root'#'localhost' to database 'test123'"
After googling i found that to grant the permission to user using the command:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost';
FLUSH PRIVILEGES;
But it also giving the error as mentioned above.
I have also uninstall and install the mysql and phpmyadmin again in the system but still getting same error's.
Please advice.
mysql>show grants;
+--------------+
| Grants for root#localhost |
+------------------------------------+
| GRANT USAGE ON *.* TO 'root'#'localhost' IDENTIFIED BY PASSWORD <secret> |
| GRANT ALL PRIVILEGES ON `oe_school`.* TO 'root'#'localhost' WITH GRANT OPTION |
+-----------------------------------------------+
Finally i solved issue it to uninstall MYSQL and also delete the Mysql folder.
sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo apt-get autoremove
sudo apt-get autoclean
sudo rm -rf /var/lib/mysql

Why isn't mysqldump recognizing the user with -u option?

I want to dump a database using the user root, but the command:
$ mysqldump –u root -p databaseName > file.sql
After entering the root password generates:
mysqldump: Got error: 1045: Access denied for user 'userX'#'localhost' (using password: YES) when trying to connect
Where userX is the local user in Linux (Ubuntu 12.04). It seems it's not recognizing the -u option.
But $ mysql -u root -p works as SELECT USER(),CURRENT_USER(); returns:
+----------------+----------------+
| USER() | CURRENT_USER() |
+----------------+----------------+
| root#localhost | root#localhost |
+----------------+----------------+
What's happening with mysqldump?
It wasn't a mysqldump problem. I wrote the command from the beginning and it worked.
It seems it was a ssh / console problem showing maybe a broken command.
I keep this answer if anyone else got this problem because of this issue.

can't login to any account in mysql

I just installed mysql on a mac and I can't seem to access any accounts from there.
I used the commands :
shell> mysql -u root -p
password:
and since I have not given one I let it go blank and I get an error saying incorrect password.
I can login using just
shell> mysql
buy I can't seem to be able to change passwords or even look at all the accounts in mysql.users.
I get the following error:
ERROR 1142 (42000): SELECT command denied to user ''#'localhost' for table 'users'
What do I do to resolve this issue and how do I use a software like sequel pro with the database?
Your problem seems, you dont have all the privileges for localhost#root, to get all the privileges run the below command.
mysql> grant all privileges on *.* to root#localhost identified by 'password' with grant option
in MySQL command prompt and you will have all the previleges to access the localhost as root.
use this command to set the password for MySQL
mysqladmin -u root password “newpassword”;
Example mysqladmin -u root password adhfhuef34;
You will also want to restart the database server after running this command
sudo /etc/init.d/mysql restart
If this Dint work, Try
$ mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("NEWPASSWORD") where User='root';
mysql> flush privileges;
mysql> quit

"mysqld" root access denied

My root user has the following privileges:
mysql> SHOW GRANTS FOR root#'%';
+--------------------------------------------------------------------------------------------------------------+
| Grants for root#% |
+--------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY PASSWORD '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B' |
| GRANT ALL PRIVILEGES ON `bedrock`.* TO 'root'#'%' |
+--------------------------------------------------------------------------------------------------------------+
When I try to execute the following command, I receive the following error message:
mysqladmin root -pSOMEPASSWORD
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'#'localhost' (using password: YES)'
Whilst I'm able to execute mysql command normally, I'm not able to do it to mysqld. What's the problem? Thanks.
You may have a user in your database without a password. Start mysql in safe mode then check to ensure none of your users have empty password fields. If you find one, either add a password or delete the user.
It may be that the password is incorrect and needs to be changed
For the improvement of the knowledge base:
The problem was in permissions and the command. It works when calling bin/mysqld_safe --user=mysql rather mysqladmin root -pSOMEPASSWORD. Note that user mysql had already got permissions throught chown -R and chgrp -R.
try this:
$ mysqladmin -u root -p password yourChosenPassword
Enter password:
and re-enter your same chosen password at the prompt, you will then be able to call mysqladmin from the command line:
$ mysqladmin -uroot -pyourChosenPassword [OPTIONS] command command....