Cannot completely restore root in MYSQL – cannot GRANT ALL - mysql

Somehow I lost root in mysql and I am trying to recover it.
I inserted root into the user table using the --skip-grant-tables option and then I ran
UPDATE mysql.user SET Grant_priv='Y', Super_priv='Y' WHERE User='root';
Then I started mysql regularly without --skip-grant-tables option
That’s what I see:
/usr/java/ep622_svn => mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.17 MySQL Community Server (GPL)
mysql> GRANT ALL ON *.* TO 'root'#'localhost';
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)

This link helped:
https://dba.stackexchange.com/questions/97439/how-do-i-give-rootlocalhost-permission-to-grant-privileges-in-mysql
One needs to execute under running with --skip-grant-tables option:
UPDATE mysql.user SET
Select_priv='Y',Insert_priv='Y',Update_priv='Y',Delete_priv='Y',
Create_priv='Y',Drop_priv='Y',Reload_priv='Y',Shutdown_priv='Y',
Process_priv='Y',File_priv='Y',Grant_priv='Y',References_priv='Y',
Index_priv='Y',Alter_priv='Y',Show_db_priv='Y',Super_priv='Y',
Create_tmp_table_priv='Y',Lock_tables_priv='Y',Execute_priv='Y',
Repl_slave_priv='Y',Repl_client_priv='Y',Create_view_priv='Y',
Show_view_priv='Y',Create_routine_priv='Y',Alter_routine_priv='Y',
Create_user_priv='Y',Event_priv='Y',Trigger_priv='Y',
Create_tablespace_priv='Y'
WHERE user='root';
FLUSH PRIVILEGES;

Related

I am trying to start mysql-client in Docker but i get access denied for user error

I am trying to connect MySQL Client to docker. I can connect just fine on the MySQL 8.0 Command Line Client, but when I try to connect to it from docker I get this error:
C:\Users\Bolin>docker exec -it mysql mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
I am on a window machine and I am trying to follow this tutorial
https://phoenixnap.com/kb/mysql-docker-container
hmm, looks like the issue is with privilege user for the database table,
Try to create a privileged user in your database named root. After that please check again
==========================================================================================
try these queries in your databse:
INSERT INTO mysql.user (Host, User, Password) VALUES ('%', 'root', password('YOURPASSWORD'));
GRANT ALL ON *.* TO 'root'#'%' WITH GRANT OPTION;
if the above code doesn't work try with the second
$mysql -u root mysql
$mysql> UPDATE user SET Password=PASSWORD('my_password') where USER='root';
$mysql> FLUSH PRIVILEGES;

Connect to mysql server without sudo

The command:
mysql -u root -p
gives the error:
ERROR 1698 (28000): Access denied for user 'root'#'localhost'
But running sudo privileges, works:
sudo mysql -u root -p
Is it possible to get rid of the sudo requirement because it prevents me from opening the database in intellij? I tried the following as in the answer to this question Connect to local MySQL server without sudo:
sudo chmod -R 755 /var/lib/mysql/
which did not help. The above question has a different error thrown
Only the root user needs sudo requirement to login to mysql. I resolved this by creating a new user and granting access to the required databases:
CREATE USER 'newuser'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database_name.* TO 'newuser'#'localhost';
now newuser can login without sudo requirement:
mysql -u newuser -p
You need to change algorithm. Following work for me,
mysql > ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY '';
mysql > FLUSH PRIVILEGES;
You can use the same ROOT user, or a NEW_USER and remove the SUDO privileges. Below example shows how to remove connect using ROOT, without SUDO.
Connect to MY-SQL using SUDO
sudo mysql -u root
Delete the current Root User from the User Table
DROP USER 'root'#'localhost';
Create a new ROOT user (You can create a different user if needed)
CREATE USER 'root'#'%' IDENTIFIED BY '';
Grant permissions to new User (ROOT)
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' WITH GRANT OPTION;
Flush privileges, so that the Grant tables get reloaded immediately. (Why do we need to flush privileges?)
FLUSH PRIVILEGES;
Now it's all good. Just in case, check whether a new root user is created.
SELECT User,Host FROM mysql.user;
+------------------+-----------+
| User | Host |
+------------------+-----------+
| root | % |
| debian-sys-maint | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
Exit mysql. (Press CTRL + Z). Connect to MySQL without SUDO
mysql -u root
Hope this will help!
first login to your mysql with sudo.
then use this code to change "plugin" coloumn value from "unix_socket" or "auth_socket" to "mysql_native_password" for root user.
UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE user = 'root' AND plugin IN ('unix_socket', 'auth_socket');
FLUSH PRIVILEGES;
finally restart mysql service. that's it.
if you want more info, check this link
UPDATE:
In new versions of mysql or mariadb you can use :
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password USING PASSWORD('your-password');
FLUSH PRIVILEGES;
I have solved this problem using following commands.
CREATE USER 'username'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'username'#'localhost';
FLUSH PRIVILEGES;
Here,
username = any user name you like.
and password = any password you like.
You can use the below query:
GRANT ALL PRIVILEGES ON *.* TO 'username'#'localhost' IDENTIFIED BY 'password';
This query is enough.
This answer needs to be slightly adapted for mariaDB instead of mysql.
First login as root using sudo:
$ sudo mysql -uroot
Then alter the mariadb root user:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password USING PASSWORD('mypassword');
FLUSH PRIVILEGES;
From now on sudo is not longer needed:
$ mysql -uroot -p
Version used:
mysql Ver 15.1 Distrib 10.4.13-MariaDB, for osx10.15 (x86_64) using readline 5.1
Login to mysql with sudo:
sudo mysql -u root -p
After that Delete current root#localhost account:
~ MariaDB [(none)]> DROP USER 'root'#'localhost';
~ MariaDB [(none)]> CREATE USER 'root'#'localhost' IDENTIFIED BY 'password';
~ MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' WITH GRANT OPTION;
~ MariaDB [(none)]> FLUSH PRIVILEGES;
In the comment of the question you answer you referenced, it reads
Ok, just try to analyze all of the directories down in the path of the
socket file, they need to have o+rx and the sock file too (it's not a
good idea to make it modifiable by others).
You can also try to remove mysql.sock and then restart mysqld, the
file should be created by the daemon with proper privileges.
This seemed to work for this question(the one you said you looked at) so it may work for you as well
The error Message:
"ERROR 1698 (28000): Access denied for user 'root'#'localhost'"
means that the Server not allow the connect for this user and not that mysql cant access the socket.
try this to solve the problem:
Login in your DB
sudo mysql -u root -p
then make these modifications:
MariaDB []>use mysql;
MariaDB [mysql]>update user set plugin=' ' where User='root';
MariaDB [mysql]>flush privileges;
MariaDB [mysql]>exit
try login again without sudo

Reset mysql root pass

I've been trying to reset the password for MySQL but all of the solutions I've found, nothing seems to work. I'm using OSX Yosemite with MAMP and MySQL 5.6.20
I attempted to reset the password via MAMP to pass but it does not work
$mysql -u root -p
Enter password: pass
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
$
and none of this works
$mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
$mysql -u root
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
$mysql -u root#localhost
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 62
Server version: 5.6.20 Homebrew
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use magento
ERROR 1044 (42000): Access denied for user ''#'localhost' to database 'magento'
mysql> SET PASSWORD FOR 'root' = PASSWORD('pass');
ERROR 1044 (42000): Access denied for user ''#'localhost' to database 'mysql'
mysql> SET PASSWORD FOR 'root'#'localhost' = PASSWORD('pass');
ERROR 1044 (42000): Access denied for user ''#'localhost' to database 'mysql'
mysql> SET PASSWORD FOR 'root'#'::1' = PASSWORD('pass');
ERROR 1044 (42000): Access denied for user ''#'localhost' to database 'mysql'
You can stop mysql server and restart it with --skip-grant-tables option. That way it doesn't look at credentials when opening up a mysql shell
After that,
mysql> UPDATE mysql.user SET Password=PASSWORD('MyNewPass')
-> WHERE User='root';
mysql> FLUSH PRIVILEGES;
Stop mysql server, and start it again
Just adding some details to Justin’s Kiang answer (which help me, thanks!).
On my OS X Machine:
sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/support-files/mysql.server start --skip-grant-tables
mysql -u root
and then the SQL code as per Justin’s answer:
UPDATE mysql.user SET Password=PASSWORD('newpassword') WHERE User='root';
FLUSH PRIVILEGES;
bye
And restarting the MySQL server again:
sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/support-files/mysql.server start
Adjust your path to mysql folder if needed.

MYSQL Access denied for user 'root'#'localhost'

I did the following steps to use MySQL in Ubuntu:
sudo aptitude install php5-mysql mysql-server
sudo service mysql stop
sudo mysqld_safe --skip-grant-tables &
sudo mysql -u root mysql
Change root password:
mysql> UPDATE mysql.user SET Password=PASSWORD('SecurePassword') WHERE
User='root';
mysql> FLUSH PRIVILEGES;
mysql> EXIT
Modify /etc/mysql/my.cnf:
[client]
user=root
password=SecurePassword
[mysqld]
...
default-time-zone = '+0:00'
Then:
sudo service mysql start
mysql -u root
mysql> SHOW GRANTS FOR root#localhost
+--------------------------------------------------------------------------------------------------+
| Grants for root#localhost |
+--------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'root'#'localhost' IDENTIFIED BY PASSWORD '[here is the Securepassword]' |
+-------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' WITH GRANT OPTION;
and I receive an error:
Access denied for user 'root'#'localhost' (using password: YES)
It was absolutely correct what you did, but I guess it's not working for one small reason.
You should use identified by password when you are going to grant privileges like this:
mysql> GRANT ALL PRIVILEGES ONE `*`.`*` TO 'root'#'localhost' IDENTIFIED BY PASSWORD
'*A4B6157319038724E3560894F7F932C8886EBFCF' WITH GRANT OPTION;
If you get an error message like this:
Warning: mysql_connect(): Access denied for user: ....
then the problem is that your application can not access the database. This can have several causes:
First of all, make sure the database settings in your db-config.php (connection file for your application) are correct, specifically the name and password of your MySQL user, the name of your database, and the name of your MySQL server.
If you're running your own server, you may need to give your MySQL user proper permissions. Log in to MySQL as the MySQL root user and issue these commands:
GRANT ALL PRIVILEGES ON database_name TO user#host IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
System like Ubuntu prefers to use auth_socket plugin for root account by default. 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 you checking this out instead.

Grant Issue In ubuntu

I am Using Ubuntu 12.04 (precise) 64-bit Since i am new to Ubuntu i am having issue with grant
I'm using following cmd to grant in Ubuntu example
Sudo mysql -u root -p
create user username identified by 'pass'
grant all privilege on Db.* to username
if check grants using
show grants for username
| GRANT ALL PRIVILEGES ON . TO 'username' IDENTIFIED BY PASSWORD |
now if i login as user-name it will not ask for password it will directly login without password if i enter password for user it gives me error message:
sudo mysql -u username -p
ERROR 1045 (28000): Access denied for user user-name (using password: YES)
and after login if i give
show databases (it will not show any databases which is granted)
please guide me with an example
And also i would like to know tat if stored procedure is created in root how to get access
for other user even tho if i give grant for the particular stored procedure to other user
is there some thing to do with definer
You don't need to run sudo to just connect to MySQL as root.
Create a database:
$ mysqladmin create mydatabase -uroot -p
Connect as root or another user who can grant permissions
$ mysql -D mydatabase -uroot -p
Allow foouser to connect from this very same machine
mysql> grant all privileges on mydatabase.* to 'foouser'#'localhost' identified by 'pass';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
Connect as your new user:
$ mysql -D mydatabase -u foouser -p
Enter password: pass
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 129
Server version: 5.5.29-0ubuntu0.12.04.2 (Ubuntu)