77

I locked my root user out from our database. I need to get all privileges back to the root user. I have my password and I can log in to MySQL. But the root user has no all privileges.

Steven
  • 24,410
  • 42
  • 108
  • 130
  • 3
    command: mysqld --skip-grant-tables – User Feb 04 '14 at 14:46
  • 4
    @macdonjo `mysql --skip-grant-tables` returns: `mysql: unknown option '--skip-grant-tables'` If it was that simple, I don't think OP would have asked the question. – smilebomb Aug 05 '14 at 13:42
  • 5
    @jefffabiny the commands 'mysql' and 'mysqld' are not the same (note the "d"). mysqld --skip-grant-tables works as expected. – thelogix Sep 30 '14 at 11:55
  • mysql is the client you use to connect and mysqld is d daemon. Usually it's here `/etc/rc.d/init.d/mysqld start --skip-grant-tables`. If you work with daemons like a server, you've to know the init.d. – m3nda May 24 '15 at 07:07
  • But I don't have mysqld. At least when I enter it in search, nothing comes up. Just mysql. Is it a separate installation? I'm using MySQL 5.7. – Roxann Higuera Jan 27 '18 at 11:34
  • @erm3nda `/etc/init.d/mysql start --skip-grant-tables` just does not obey. I can start `mysqld` directly, but then `mysql_upgrade` errors out because it doesn not find the socket file. – Csaba Toth Feb 24 '18 at 00:54
  • @CsabaToth You've ommited the "D" letter in the command. To clarify, **mysqld is the server** and **mysql is the client**. The client will never (and is not intended to) accept such argument. It's on the server side, you have to start the server with those args then connect with client later. – m3nda Feb 24 '18 at 16:32
  • @erm3nda The **executable** / process itself is `mysqld`, but the service/init.d **script** to start the **server** is called `mysql`. I can start the `mysqld` manually as a process, but then the `sock` socket file is not setup properly without further parameterization as it does when it operates as a daemon with the `/etc/init.d/mysql` (no "d"). So if you start the mysqld that way, the `mysql_upgrade` fails. – Csaba Toth Feb 25 '18 at 04:41
  • I am not totally sure why mysql service is called mysql instead of mysqld, if you do a `cat /etc/init.d/myql` you'll see a bash script, which calls `mysqld` binary. Forget it, you don't need the service to run `mysqld` on your own. I think you're missunderstanding something. Stop the service, then try to run `mysqld --skip-grant-tables` or even better, try with the mysqld_safe option that @Arpit commented https://stackoverflow.com/a/47687614/2480481 – m3nda Feb 25 '18 at 06:26
  • 1
    Mysql has a step by step manual for doing this: [Reset mysql root password](http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html) – Ikke Nov 10 '09 at 14:38
  • 1
    There is no command given in that page to start mysqld with --skip-grant-tables. I like my articles with 'copy paste' smoothness! :) – Aditya M P Apr 04 '13 at 06:31
  • That's the article I'm trying to follow -- it doesn't say exactly how to start it with that command option. Just "Stop mysqld and restart it with the --skip-grant-tables option." – eselk Feb 01 '14 at 00:39

13 Answers13

62

I had the same problem as the title of this question, so incase anyone else googles upon this question and wants to start MySql in 'skip-grant-tables' mode on Windows, here is what I did.

Stop the MySQL service through Administrator tools, Services.

Modify the my.ini configuration file (assuming default paths)

C:\Program Files\MySQL\MySQL Server 5.5\my.ini

or for MySQL version >= 5.6

C:\ProgramData\MySQL\MySQL Server 5.6\my.ini 

In the SERVER SECTION, under [mysqld], add the following line:

skip-grant-tables

so that you have

# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this 
# file.
#
[mysqld]

skip-grant-tables

Start the service again and you should be able to log into your database without a password.

tonycoupland
  • 4,127
  • 1
  • 28
  • 27
  • 4
    the location for my.ini file is at `C:\ProgramData\MySQL\MySQL Server 5.6` not `Program Files` folder – Timeless Sep 03 '15 at 09:32
  • Great, does anyone know how to do this on unix? /etc/my.cnf is not the place to put "skip-grant-tables", I get "mysql: unknown option '--skip-grant-tables'" if I run mysql with it there. – Alkanshel Jun 06 '18 at 20:44
  • 2
    I edited /etc/my.cnf and added `[mysqld]` and on the next line entered `skip-grant-tables` without the `--`, and with a restart of MySQL it worked. – Phlip Jul 09 '18 at 18:19
  • @Amalgovinus, you need mysqld.cnf. The location is distribution dependent-- use locate :) – Chris Rees Jun 25 '21 at 09:37
58

How to re-take control of the root user in MySQL.

DANGER: RISKY OPERATTION

  • Start session ssh (using root if possible).
  • Edit my.cnf file using.

    sudo vi /etc/my.cnf
    
  • Add line to mysqld block.*

    skip-grant-tables
    
  • Save and exit.

  • Restart MySQL service.

    service mysql restart
    
  • Check service status.

    service mysql status
    
  • Connect to mysql.

    mysql
    
  • Using main database.

    use mysql;
    
  • Redefine user root password.

    UPDATE user SET `authentication_string` = PASSWORD('myNuevoPassword') WHERE `User` = 'root'; 
    
  • Edit file my.cnf.

    sudo vi /etc/my.cnf
    
  • Erase line.

    skip-grant-tables
    
  • Save and exit.

  • Restart MySQL service.

    service mysqld restart
    
  • Check service status.

    service mysql status
    
  • Connect to database.

    mysql -u root -p
    
  • Type new password when prompted.

This action is very dangerous, it allows anyone to connect to all databases with no restriction without a user and password. It must be used carefully and must be reverted quickly to avoid risks.

icc97
  • 11,395
  • 8
  • 76
  • 90
gizmo_marco
  • 675
  • 5
  • 2
  • 2
    `service mysqld restart` is wrong - `service mysql restart` is correct – fnc12 May 31 '17 at 14:30
  • 5
    For MySQL 5.7 (on Ubuntu) the .cnf file I had to edit was located at /etc/mysql/mysql.conf.d – James Fennell Feb 08 '18 at 21:56
  • For MariaDB 10.2.33 - I cannot run SQL UPDATE, it returns this error: `ERROR 1290 (HY000): The MariaDB server is running with the --skip-grant-tables option so it cannot execute this statement` – Frodik Sep 21 '20 at 09:46
  • PASSWORD() function is deprecated after version 5.7.5. See also https://stackoverflow.com/questions/52320576 – Leland Hepworth Mar 18 '21 at 14:47
  • You can edit the existing conf file also. The location is in `/etc/mysql/mysql.conf.d/mysqld.cnf` – arulraj.net Mar 24 '23 at 03:39
22

After trying lots of things, this is what worked for me:

sudo mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newpassword'; 

What that does is first we use sudo to log in mysql as root without needing a password. Then we just update root's password.

After that, I restarted mysqld:

sudo service mysql restart

And the newpassword logged root in!

Neithan Max
  • 11,004
  • 5
  • 40
  • 58
8

On the Linux system you can do following (Should be similar for other OS)

Check if mysql process is running:

sudo service mysql status

If runnning then stop the process: (Make sure you close all mysql tool)

sudo service mysql stop

If you have issue stopping then do following

Search for process: ps aux | grep mysqld Kill the process: kill -9 process_id

Now start mysql in safe mode with skip grant

sudo mysqld_safe --skip-grant-tables &
Arpit
  • 991
  • 14
  • 17
5

I'm in windows 10, using WAMP64 server. Searched for my.cnf and my.ini. Found my.ini in C:\wamp64\bin\mariadb\mariadb10.2.14.

Following the instructions from the colleagues:

  1. Opened the quick start menu from Wampserver, selected 'Stop All Services'
  2. Opened my.ini in a text editor, searched for [mysqld]
  3. Added 'skip-grant-tables' at the end of the [mysqld] section (but within it)
  4. Save the file, leave the editor open
  5. In the Wampserver menu, select "Restart Services'. There will be a warning about the skip-grant-tables option
  6. In the Wampserver menu select MySQL to open the prompt
  7. It asked for a password, just press enter
  8. Paste the command ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newpassword';
  9. It must report that the operation was successful (no tables affected)
  10. In the my.ini file, erase the 'skip-grant-tables' line, save the file
  11. In the WampServer menu, select once more Restart Service

Now you can enter with the new password. Thanks to all answers here.

AllOutOfSalt
  • 1,516
  • 3
  • 27
  • 46
Faltausername
  • 51
  • 1
  • 1
4

If you use mysql 5.6 server and have problems with C:\Program Files\MySQL\MySQL Server 5.6\my.ini:

You should go to C:\ProgramData\MySQL\MySQL Server 5.6\my.ini.

You should add skip-grant-tables and then you do not need a password.

# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this 
# file.
#
# server_type=3
[mysqld]
skip-grant-tables

Note: after you are done with your work on skip-grant-tables, you should restore your file of C:\ProgramData\MySQL\MySQL Server 5.6\my.ini.

heesang
  • 41
  • 2
  • For linux fedora users, file will be my.cnf under /etc/ directory. Add skip-grant-tables line in that file and restart the mysqld service by "service mysqld restart" command. Thanks – Sohel Pathan Feb 13 '17 at 10:50
3

Use the following command (notice the "d"): mysqld --skip-grant-tables

trailing slash
  • 881
  • 11
  • 13
3

Please run this below command from the console to skip the user table verification while launching mysql database from command prompt

mysqld -skip-grant-tables
Prasad
  • 1,089
  • 13
  • 21
2

if you are running on Apple MacBook OSX then:

  1. Stop your MySQL server (if it is already running).
  2. Find your MySQL configuration file, my.cnf. (For me it was placed @ /Applications/XAMPP/xamppfiles/etc. You can just search if you can't find it).
  3. Open my.cnf file in any text editor.
  4. Add "skip-grant-tables" (without quotes) at the end of [mysqld] section and save the file.
  5. Now start your MySQL server. It'll start with skip-grant-tables option.

Do what you want now!!

PS: Please remove skip-grant-tables from my.cnf file once you are done with whatsoever you want to do ELSE MySQL server will always run without access grants.

Saurabh Hooda
  • 2,536
  • 2
  • 20
  • 30
1

Edit my.ini file and add skip-grant-tables and restart your mysql server :

[mysqld]
port= 3306
socket = "C:/xampp/mysql/mysql.sock"
basedir = "C:/xampp/mysql" 
tmpdir = "C:/xampp/tmp" 
datadir = "C:/xampp/mysql/data"
pid_file = "mysql.pid"
# enable-named-pipe
key_buffer = 16M
max_allowed_packet = 1M
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
log_error = "mysql_error.log"
skip-grant-tables
# Change here for bind listening
# bind-address="127.0.0.1" 
# bind-address = ::1 
Azouz Mohamadi
  • 141
  • 1
  • 2
0

if this is a windows box, the simplest thing to do is to stop the servers, add skip-grant-tables to the mysql configuration file, and restart the server.

once you've fixed your permission problems, repeat the above but remove the skip-grant-tables option.

if you don't know where your configuration file is, then log in to mysql send SHOW VARIABLES LIKE '%config%' and one of the rows returned will tell you where your configuration file is.

longneck
  • 11,938
  • 2
  • 36
  • 44
  • I don't know where the configuration file is. – Steven Nov 11 '09 at 02:24
  • see my updated post. but honestly, if you're having this problem AND you don't know where your configuration file is, you should have someone more experienced fix it for you. unless this is just your personal database you're learning on, then go ahead and learn away. :) – longneck Nov 11 '09 at 02:34
  • Error SQL query: Edit GRANT ALL PRIVILEGES ON * . * TO 'root'@'localhost' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ; MySQL said: Documentation #1290 - The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement You see, I have logged on to MySQL with the --skip-grant-tables option. But when I attempted to get all privileges back to the root user, I failed. – Steven Nov 11 '09 at 02:54
  • Such simple. Much easy. – o_O Jun 11 '14 at 02:13
0

I see that the question is old, but maybe my configuration will help someone. I use this configuration in scripts:

sed -i 's/^#skip-grant-tables.*/skip-grant-tables/g' /etc/my.cnf

service mysql restart

mysql -e "UPDATE mysql.user SET authentication_string='' WHERE user='root';"

sed -i 's/^skip-grant-tables.*/#skip-grant-tables/g' /etc/my.cnf

service mysql restart

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Vippi
  • 1
  • 2
0

This is how to do it on Ubuntu 20.4. This worked for me.
Go to /etc/mysql/mysql.conf.d/
You can write into terminal
cd /etc/mysql/mysql.conf.d/,
then you need to edit the file which is named mysqld.cnf.
On my PC, that file was a read-only file, so I needed to first change the permissions.
I wrote sudo chmod +rw mysqld.cnf in the terminal.
After that, I edited the file by typing sudo gedit mysqld.cnf in the terminal.
In the file, you will see [mysqld] somewhere, below [mysqld] add skip-grant-tables in a new line, so that it looks like this

[mysqld]
skip-grant-tables

Restart the mysql service by writting sudo service mysql restart in terminal.
If your server wasn't running then write sudo service mysql start in terminal.
Another thing worth mentioning here is that
I also had another problem which I fixed in almost the exact same manner.
My server wasn't listening at the port 3306,
so I also had to add port = 3306 in that mysqld.cnf file.
Now I have

[mysqld]
skip-grant-tables
port = 3306

in the mysqld.cnf file.

catloverxx
  • 47
  • 8