I just installed Ubuntu 16.04 LTS along with the packages php
, mariadb
and nginx
. I ran mysql_secure_installation
and changed the root password.
Now when I try to login to mysql
using the root account while logged in Ubuntu as normal user account I get access denied.
When I login using sudo mysql
, mysql doesn't even ask me password. If I run mysql_secure_installtion
I see that old settings never got set permanently.
What am I doing wrong?
I recently upgrade my Ubuntu 15.04 to 16.04 and this has worked for me:
First, connect in sudo mysql
Check your accounts present in your db
Delete current root@localhost account
Recreate your user
Give permissions to your user (don't forget to flush privileges)
Exit MySQL and try to reconnect without sudo from your local machine.
Edit: a previous version of the answer post recommended creating a user
'root'@'%'
. However, it is more secure to create'root'@'localhost'
so connections can only be made from localhost, and not remotely. Both solutions work equally as well for local development.I hope this will help someone :)
Taken from Change User Password in MySQL 5.7 With "plugin: auth_socket"
So in order to to change the
plugin
back tomysql_native_password
:Login with sudo:
Change the
plugin
and set a password with a single command:Of course you can also use the command above to set an empty password.
Just for the record, (and
MariaDB < 10.2
users) there is also another way to only change theplugin
without providing a password (leaving it empty):In short, on MariaDB
where you replace NEWPASSWORD with the password you want, and everything else verbatim.
The issue here is that when MariaDB or MySQL are installed/updated (especially if at some point root is set without a password) then in the Users table the password is actually empty (or ignored), and logging in depends on the system user corresponding to a MySQL user. You can test this as follows by switching to system root, and then type:
Then enter either no password or the wrong password. You'll probably be let in. (You may even be able to log in from the unix root by simply
# mysql
as the password is irrelevant and the user is defined).So what's happening? Well, if you log in as root and do the following:
you'll note
auth_socket
(which may readunix_socket
on MariaDB). These sockets ignore passwords and allow the corresponding Unix user in without a password check. This is why you can log in with root but not with a different user.So the solution is to update the Users to not use the
auth_socket/unix_socket
and properly set a password.On MariaDB (<10.2, see comments below) which is on the Ubuntu version 16 as of 2017 this should suffice. NEWPASSWORD is your password.
mysql_native_password
you type verbatim.(It's possible that setting the plugin to empty would work. YMMV. I didn't try this. So this is an alternative.)
Otherwise:
Then
For the record, the solution involving deleting the user and recreating it with '%' got me totally locked out of the database, and can cause other problems unless you get the
grant
statement exactly right - easier to simply update the root you already have.In my experience, the issue only happens with the root user, as other users will be added manually not part of an initial install/update.
By default,
root
user is set to authenticate through anauth_socket
rather than with a password.In order to use the password authentication, do:
Login to MySQL root shell:
Check authentication methods enabled for different users (optional)
Make
root
to authenticate with a password:Flush privileges for the changes to take effect;
Exit and authenticate with your password
If you just run mysql command under root user you will be granted access without asked for password, because socket authentication enabled for root@localhost. .
The only way to set password is to switch to native authentication like:
Try to create new mysql account, for me it has worked (mysql 5.7.12):
Login as sudo:
Create new user and grant him privileges (no password):
Login as new user:
I’ve been adapting some provisioning scripts I have created to use MariaDB and ran into this exact issue. Piecing together lots of info here an Gazzer’s answer really zeros in in the issue; it all boils down to the
auth_socket
/unix_socket
setting.So when using MariaDB 5.5 (under Ubuntu 14.04) and MariaDB 10 under (Ubuntu 16.04), logging into MySQL and running this command cleared things up right away:
The other answers—including the highest voted answer as of this post by Loremhipsum—really encourage bad practices by recommending dropping a user and then recreating them. To me, that is a pretty radical solution. The best/simplest solution os to nullify the
plugin
value, flush privileges and get on with life.I had to do two things (thanks to @Todor and @Loremhipsum):
and then:
I would not recommend to drop user
root
.Try this code first,
and then,
then press
Ctrl+Z
and type:bg
to run the process from the foreground into the background, then verify your access by:Maximum security
As unix authentication is the most secure authentication, the best thing to do is leave the default root account as is (i.e. continue to use unix socket authentication with no password) and if not using the root account, use it via
sudo
. If an attacker has root/sudo access; then they already have full control of your server...When you need to access MySQL/MariaDB via an alternate method (i.e. not CLI), create a new MySQL/MariaDB user with a password and only the required permissions.