Ubuntu Server MySQL - Unable to access with root user
I am new to Ubuntu Server
and I made the command:
sudo apt-get install mysql-server mysql-common mysql-client
Everything is going well and I am back at the command line. When I type:
mysql -u root
I got an error Access denied for user 'root'@'localhost' (using password: NO)
. Any ideas what I did wrong? I have tried uninstalling all 3 packages and reinstalling them to no avail. Google didn't help :(
source to share
One way to do it:
/etc/init.d/mysql stop
mysqld_safe --skip-grant-tables &
mysql
Then you enter mysql, enter the following command:
use mysql;
update user set password='*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4' where User='root';
flush privileges;
After that, stop mysql and the start will be the same as usual
/etc/init.d/mysql stop
/etc/init.d/mysql start
mysql -u root -p
Your password should be: mypass
source to share
For newer MySQL versions (for MariaDB <10.2 use the query below as it doesn't support changing user but still uses auth_socket)
If you install 5.7 and don't provide a password to the user
root
, this will use the pluginauth_socket
. This plugin doesn't care and doesn't need a password. It just checks if the user is connected using a UNIX socket and then compares the username.
Taken from Change User Password in MySQL 5.7 Using "plugin: auth_socket" and https://askubuntu.com/a/801950/395418
So, to change plugin
to mysql_native_password
:
-
Login with sudo:
sudo mysql -u root
-
Change
plugin
and set the password with one command:ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'test';
You can of course also use the above command to set a blank password.
For write-only, there is another way to change just plugin
without providing a password (leaving it blank):
update mysql.user set plugin = 'mysql_native_password' where User='root';
FLUSH PRIVILEGES;
source to share
Tested on mysql 5.7.12
/etc/init.d/mysql stop
/usr/local/mysql/bin/mysqld_safe --skip-grant-tables &
/usr/local/mysql/bin/mysql
Then from the open sql console enter the following commands:
UPDATE mysql.user
SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N'
WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;
exit;
Stop the current process and start it again. Then check the new password (should be MyNewPass ).
/etc/init.d/mysql stop
/etc/init.d/mysql start
/usr/local/mysql/bin/mysql -u root -p
source to share