Reset root password in MySQL 8 - mysql

When I use the method to reset the root password described on How to Reset the Root Password, starting the server with mysqld --init-file=/home/username/init-file doesn't work.
It gives the following error.
[Server] Could not open /var/log/mysqld.log file for error logging: Permission denied.
Permissions are correct.
Server starts when I use service mysqld start.
I am using MySQL 8.0.12 on Fedora 28.

If you can log in to your MySQL server and you want to change your password by query you can do it this queries:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
or removing the root password:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY '';
Thanks to this blog: https://juejin.im/entry/5b06698cf265da0db3501fdd
Also answered the same question here: https://stackoverflow.com/a/54511940/1157720

Just type the below command in your Phpmysql shell.
ALTER USER 'root'#'localhost' IDENTIFIED BY 'password'

You need to be the owner or group of /var/log/mysqld.log before you can do this. It's crashing because it can't write to it's log file.
If you do ls -l /var/log/mysqld.log you can see who owns it, then do su - username before attempting to start mysql.
You may need to become root (OS root user, not mysql root) to do this, depending on how your system was setup.
Here is what the permissions and ownership is supposed to be:
https://dev.mysql.com/doc/mysql-secure-deployment-guide/5.7/en/secure-deployment-permissions.html

Related

Mysql root user has no privileges

I've searched a lot but nothing works for me.
I have a fresh installation of a LAMP server, installed via tasksel command.
My system runs php 7.4 and MySql 8
For some reason root user (that in my case is called phpmyadmin, by default) has no privileges on the DBs.
I can log in, so I'm sure the password is correct, but I cannot create a Database.
$ mysql -u phpmyadmin -p
mysql > create database test;
Access denied for user 'phpmyadmin'#'localhost' to database 'test'
EDIT
As suggested, here's the output of
mysql> SELECT name, Create_priv FROM mysql.user;
SELECT command denied to user 'phpmyadmin'#'localhost' for table 'user'
NEW EDIT
I tried to access without the password, following the guide reported here:
...
No results for me :(
Stop the MySQL Server: sudo /etc/init.d/mysql stop (OK)
Start the mysqld configuration: sudo mysqld --skip-grant-tables
output:
[2] 2345
[1] Exit 1 sudo mysqld --skip-grant-tables
Run: sudo service mysql start
output
[2]+ Exit 1 sudo mysqld --skip-grant-tables
It seems to exit with errors... Am I wrong?
I also tried to login in that situation, but I receive this message "ERROR 1698 (28000) Access denied for user 'root'#'localhost'"
If I try my phpmyadmin user, the message is "ERROR 1045 (28000): Access denied for user 'phpmyadmin'#'localhost' (using password: NO)"
EDIT 06/19/2020
I followed the steps reported here
How to reset or change the MySQL root password?
I had to use MD5() instead of PASSWORD() because it's now deprecated.
There's still something wrong.
Now I can access the database with no privileges and set/change my password.
Then I can flush all privileges.
Then I try...
mysql -u root -p
I enter my new password...
ERROR 1698 (28000): Access denied for user 'root'#'localhost'
I can still log in with the user set in phpMyAdmin, but there are still no privileges for me.
I also reinstalled MySql server.
I remember I had a similar problem last time I installed an old Linux Virtual Machine, but I solved that issue easily. Now I'm wondering what's wrong :(
SOLVED
I reinstalled my virtual machine and installed manually both php and mysql server.
To be sure to set privileges in the right way I followed this small guide... maybe helpful for someone.
https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-20-04
Bye!

Access denied for user 'root'#'localhost' (using password: YES) after new installation on Ubuntu

Today I did a login as root into Ubuntu 14.04.1 LTS ll
and then apt-get install mariadb-server (without sudo but as root).
With mySQL -h localhost -u root --password=<PW> I got
Access denied for user 'root'#'localhost' (using password: YES)
With mySQL -u root -p I logged into the DB and did
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' IDENTIFIED BY '<PW>';
FLUSH ALL PRIVILEGES;
But this did not help. Have you got any idea?
I did not find the answer for the similar questions.
TL;DR: To access newer versions of mysql/mariadb as the root user, after a new install, you need to be in a root shell (ie sudo mysql -u root, or mysql -u root inside a shell started by su - or sudo -i first)
Having just done the same upgrade, on Ubuntu, I had the same issue.
What was odd was that
sudo /usr/bin/mysql_secure_installation
Would accept my password, and allow me to set it, but I couldn't log in as root via the mysql client
I had to start mariadb with
sudo mysqld_safe --skip-grant-tables
to get access as root, whilst all the other users could still access fine.
Looking at the mysql.user table I noticed for root the plugin column is set to unix_socket whereas all other users it is set to 'mysql_native_password'. A quick look at this page: https://mariadb.com/kb/en/mariadb/unix_socket-authentication-plugin/ explains that the Unix Socket enables logging in by matching uid of the process running the client with that of the user in the mysql.user table. In other words to access mariadb as root you have to be logged in as root.
Sure enough restarting my mariadb daemon with authentication required I can login as root with
sudo mysql -u root -p
or
sudo su -
mysql -u root -p
Having done this I thought about how to access without having to do the sudo, which is just a matter of running these mysql queries
GRANT ALL PRIVILEGES on *.* to 'root'#'localhost' IDENTIFIED BY '<password>';
FLUSH PRIVILEGES;
(replacing <password> with your desired mysql root password). This enabled password logins for the root user.
Alternatively running the mysql query:
UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE user = 'root' AND plugin = 'unix_socket';
FLUSH PRIVILEGES;
Will change the root account to use password login without changing the password, but this may leave you with a mysql/mariadb install with no root password on it.
After either of these you need to restarting mysql/mariadb:
sudo service mysql restart
And voila I had access from my personal account via mysql -u root -p
PLEASE NOTE THAT DOING THIS IS REDUCING SECURITY Presumably the MariaDB developers have opted to have root access work like this for a good reason.
Thinking about it I'm quite happy to have to sudo mysql -u root -p so I'm switching back to that, but I thought I'd post my solution as I couldn't find one elsewhere.
In clean Ubuntu 16.04 LTS, MariaDB root login for localhost changed from password style to sudo login style...
so, just do
sudo mysql -u root
since we want to login with password, create another user 'user'
in MariaDB console... (you get in MariaDB console with 'sudo mysql -u root')
use mysql
CREATE USER 'user'#'localhost' IDENTIFIED BY 'yourpassword';
\q
then in bash shell prompt,
mysql-workbench
and you can login with 'user' with 'yourpassword' on localhost
from superuser accepted answer:
sudo mysql -u root
use mysql;
update user set plugin='' where User='root';
flush privileges;
exit;
Try the command
sudo mysql_secure_installation
press enter and assign a new password for root in mysql/mariadb.
If you get an error like
ERROR 2002 (HY000): Can't connect to local MySQL server through socket
'/var/run/mysqld/mysqld.sock'
enable the service with
service mysql start
now if you re-enter with
mysql -u root -p
if you follow the problem enter with sudo su and mysql -u root -p now apply permissions to root
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' IDENTIFIED BY '<password>';
this fixed my problem in MariaDB.
Good luck
I had to be logged into Ubuntu as root in order to access Mariadb as root. It may have something to do with that "Harden ..." that it prompts you to do when you first install. So:
$ sudo su
[sudo] password for user: yourubunturootpassword
# mysql -r root -p
Enter password: yourmariadbrootpassword
and you're in.
The new command to flush the privileges is:
FLUSH PRIVILEGES
The old command FLUSH ALL PRIVILEGES does not work any more.
You will get an error that looks like that:
MariaDB [(none)]> FLUSH ALL PRIVILEGES;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ALL PRIVILEGES' at line 1
Hope this helps :)
Run mysql_upgrade.
Check that
SHOW GRANTS FOR 'root'#'localhost';
says
GRANT ALL PRIVILEGES ON ... WITH GRANT OPTION
Check that the table exists _mysql.proxies_priv_.
Access denied for user 'root'#'localhost' while attempting to grant privileges. How do I grant privileges?
System like Ubuntu prefers to use auth_socket plugin. It will try to authenticate by comparing your username in DB and process which makes mysql request; it is described in here
The socket plugin checks whether the socket user name (the operating
system user name) matches the MySQL user name specified by the client
program to the server, and permits the connection only if the names
match.
Instead you may want to back with the mysql_native_password, which will require user/password to authenticate.
About the method to achieve that, I recommend this instead.
First of all close terminal to exit this cmd;
Then run: sudo mysql (Do not forgot sudo otherwise MySQL requires password which you don't know - use sudo to skip mysql password authentication)
Select mysql database by following cmd
mysql> use mysql
then set new password for your root user
mysql> alter user root#localhost identified with mysql_native_password by 'MyNewPassword#123';
then
mysql> flush privileges;
and quit to close mysql connection
mysql> quit
Now you can run sudo mysql_secure_installation without any error
Here you have to enter your new set password (MyNewPassword#123)

How to disable MySQL root logins when no password is supplied?

MySQL is installed on my laptop and it works fine, except that I am allowed to log in without supplying the root password. I can also log in by supplying the root password. If the supplied password doesn't match, it denies access. The root password was changed to something of my own choosing when I originally installed MySQL. I just noticed the no-password logins today.
So, I need to stop access to the root account when a password isn't supplied. What I've tried so far is to reset the root password with:
mysqladmin -u root password TopSecretPassword
I then logged in to the console and issued:
mysql> flush privileges; exit;
I'm still able to log in to MySQL with:
%> mysql -u {enter}
How do I stop this behavior?
ADDITIONAL DETAILS:
%> mysql -u {enter}
mysql>SELECT USER(), CURRENT_USER();
> root#localhost, root#localhost
mysql>SELECT COUNT(*) FROM mysql.users WHERE user='root' AND password='';
> COUNT(*)
> 0
mysql>SELECT COUNT(*) FROM mysql.users WHERE user='';
> COUNT(*)
> 0
mysql>SELECT COUNT(*) FROM mysql.users WHERE user='root';
> COUNT(*)
> 1
%> vi /etc/my.cnf
/skip-grant-tables
> E486: Pattern not found: skip-grant-tables
Users encountering this behaviour in newer versions of MySQL/MariaDB (e.g. Debian Stretch, etc) should be aware that in the mysql.user table, there is column called 'plugin'. If the 'unix_socket' plugin is enabled, then root will be able to log in via commandline without requiring a password. Other log in mechanisms will be disabled.
To check if that's the case:
SELECT host, user, password, plugin FROM mysql.user;
which should return something like this (with unix_socket enabled):
+-----------+------+--------------------------+-------------+
| host | user | password | plugin |
+-----------+------+--------------------------+-------------+
| localhost | root | <redacted_password_hash> | unix_socket |
+-----------+------+--------------------------+-------------+
To disable that and require root to use a password:
UPDATE mysql.user SET plugin = '' WHERE user = 'root' AND host = 'localhost';
FLUSH PRIVILEGES;
Note: As noted by #marioivangf (in a comment) in newer versions of MySQL (e.g. 5.7.x) you may need to set the plugin to 'mysql_native_password' (rather than blank).
Then restart:
service mysql restart
Problem fixed!:
root#lamp ~# mysql
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
Source: https://stackoverflow.com/a/44301734/3363571
Big thanks to #SakuraKinomoto (please go up vote his answer if you find this useful).
I know this question is a few months old, but I had the same issue.
In my case, it was due to the presence of a user-specific configuration file located at ~/.my.cnf that contained the user and password. In my case, cPanel created this config file.
[client]
pass="ROOT_PASSWORD_WAS_HERE!"
user=root
User-specific configuration files are a feature of MySQl, and the location of all the config files read are detailed in the documentation: http://dev.mysql.com/doc/refman/5.1/en/option-files.html.
If you're running mysql on a *nix dist, run the following command to see if you have a user-specific config file:
cat ~/.my.cnf
Looks like you may have one or more anonymous users.
To see them run this query:
SELECT user,host,password FROM mysql.user WHERE user='';
To see that you authenticated as such, run this:
SELECT USER(),CURRENT_USER();
This will show how you tried to login and how mysql allowed you to login.
Run these two queries:
DELETE FROM mysql.user WHERE user='';
FLUSH PRIVILEGES;
That should do it !!!
CAVEAT #1
If this does not work, check /etc/my.cnf for this option:
skip-grant-tables
If that is in my.cnf, remove it and restart mysql.
CAVEAT #2
Something else to watch out for is having multiple root users. Please run this:
SELECT user,host,password FROM mysql.user WHERE user='root';
If you defined root to have a password and still get in as root, this is indicative of having multiple root users. There may be these entries in mysql.user
root#localhost
root#127.0.0.1
root#'hostnameofserver'
mysql may allow authentication from any of the root users if a root user has no password. This should manifest itself when you run SELECT USER(),CURRENT_USER(); because the output of each function will show up as different.
If one root user has the MD5 password and all other root users do not, you can spread that MD5 password to the other root users as follows:
UPDATE mysql.user
SET password =
(
SELECT password FROM mysql.user
WHERE user='root' AND password <> ''
)
WHERE user='root' AND password = '';
FLUSH PRIVILEGES;
Current answers are no longer ok for MySQL 8. Indeed:
In MySQL 8.0, caching_sha2_password is the default authentication plugin rather than mysql_native_password, which was the default in MySQL 5.7.
So the solution is to run mysql, then
ALTER USER 'root'#'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd'; EXIT;
and then mysql -uroot -p to try if it worked. (I don't remember if service mysql restart was necessary).
If you already applied the technique from the main answer of this actual question, here is how to revert it for MySQL 8:
UPDATE mysql.user SET plugin = 'caching_sha2_password' WHERE user = 'root' AND host = 'localhost';
FLUSH PRIVILEGES;
If anyone comes across this post while searching how to change root password and stop root-logins without password, Percona saved my ass once again:
https://www.percona.com/blog/2016/03/16/change-user-password-in-mysql-5-7-with-plugin-auth_socket/
basically the command that worked for me:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'test';

How can I rework my MySQL password so that I can rake db: create on rails?

I've very much so tried it all.
when I db:rake create, I get the following:
Access denied for user 'root'#'localhost' (using password: NO).
Please provide the root password for your mysql installation
>
rake aborted!
There is no password set. And I've searched all day on stackoverflow and on google.
I've even set a password for it and tried that out.
Yes, I've been to http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html#resetting-permissions-unix
But when I followed the instructions to reset the password, terminal and myself are unable to locate the .pid file!
I've tried using the mysqld_safe with --skip-grant-tables option and wound up with this:
110821 23:32:22 mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
rm: /tmp/mysql.sock: Permission denied
110821 23:32:24 mysqld_safe mysqld from pid file /usr/local/var/mysql/Tony-Ngs-MacBook-Air.local.pid ended
I believe you just need to add the password in your database.yml file. That's an error that's displayed when the client connecting to mysql didn't specify a password, not a configuration issue with the server (although it does appear that way with the phrasing).
If you need to set your password in MySQL, you do it with a grant option:
GRANT ALL ON *.* TO 'railsuser'#'localhost' IDENTIFIED BY 'password';

MySQL: can't access root account

I'm running MySQL 5.x on my local Windows box and, using MySQL administrator, I can't connect to the databases I created using the root account. The error I get is:
MySQL Error number 1045 Access denied
for user 'root'#'localhost' (using
password: YES)
I can't recall changing root account credentials, but I know I tried to give other computers on the LAN access to the db by adding their IPs. One thing I did for one of the IPs was to specify access to the account 'root' instead of root, i.e. I surrounded root with single quotation chars. All using MySQL administrator. Could this be the reason why i can't login using root?
Also, is there a way to create a new or reset the root account? As previously mentioned, I have full access to my box.
See these questions
How to change MySQL root password to default?
How do I retrieve my MySQL username and password?
How do I change the password of the root user in MySQL?
You can use the init files. Check the MySQL official documentation on How to Reset the Root Password (including comments for alternative solutions).
So basically using init files, you can add any SQL queries that you need for fixing your access (such as GRAND, CREATE, FLUSH PRIVILEGES, etc.) into init file (any file).
Here is my example of recovering root account:
echo "CREATE USER 'root'#'localhost' IDENTIFIED BY 'root';" > your_init_file.sql
echo "GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' WITH GRANT OPTION;" >> your_init_file.sql
echo "FLUSH PRIVILEGES;" >> your_init_file.sql
and after you've created your file, you can run:
killall mysqld
mysqld_safe --init-file=$PWD/your_init_file.sql
then to check if this worked, press Ctrl+Z and type: bg to run the process from the foreground into the background, then verify your access by:
mysql -u root -proot
mysql> show grants;
+-------------------------------------------------------------------------------------------------------------+
| Grants for root#localhost |
+-------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'root'#'localhost' IDENTIFIED BY PASSWORD '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B' |
See also:
No Password – No Problem at Everything MySQL
Bug #28331: Unclear error message when CREATE USER fails due to duplicate key
This worked for me:
https://blog.dotkam.com/2007/04/10/mysql-reset-lost-root-password/
Step 1: Stop MySQL daemon if it is currently running
ps -ef | grep mysql - checks if mysql/mysqld is one of the running processes.
pkill mysqld - kills the daemon, if it is running.
Step 2: Run MySQL safe daemon with skipping grant tables
mysqld_safe --skip-grant-tables &
mysql -u root mysql
Step 3: Login to MySQL as root with no password
mysql -u root mysql
Step 4: Run UPDATE query to reset the root password
UPDATE user SET password=PASSWORD("value=42") WHERE user="root";
FLUSH PRIVILEGES;
In MySQL 5.7, the 'password' field was removed, now the field name is 'authentication_string':
UPDATE user SET authentication_string=PASSWORD("42") WHERE
user="root";
FLUSH PRIVILEGES;
Step 5: Stop MySQL safe daemon
Step 6: Start MySQL daemon
There is a section in the MySQL manual on how to reset the root password which will solve your problem.
I got the same problem when accessing mysql with root. The problem I found is that some database files does not have permission by the mysql user, which is the user that started the mysql server daemon.
We can check this with ls -l /var/lib/mysql command, if the mysql user does not have permission of reading or writing on some files or directories, that might cause problem. We can change the owner or mode of those files or directories with chown/chmod commands.
After these changes, restart the mysqld daemon and login with root with command:
mysql -u root
Then change passwords or create other users for logging into mysql.
HTH
This code is solution for me
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'pWKQiSdwMBZDJfWZEnxgEHCPl4GgkJ';