如果某天發現自己登陸不上mysql,又恰巧記不得密碼,那隻能通過本地重新修改mysql.user裡面的密碼。
首先
$ sudo service mysqld stop //確認mysqld和免安裝的mysqld都關閉 $ sudo mysqld_safe --skip-grant-table & //進入本地安全模式
到這裡後mysql可以直接用root登陸進去不需要密碼,接著
mysql>use mysql mysql>SELECT host,user,password,Grant_priv,Super_priv FROM mysql.user; +--------------+---------+-------------------------------------------+------------+------------+ | host | user | password | Grant_priv | Super_priv | +--------------+---------+-------------------------------------------+------------+------------+ | 192.168.28.% | Looft | *FED29C14B2E900D70B11B1F1B370F953BA51A6A0 | N | Y | | 192.168.28.% | live | *FED29C14B2E900D70B11B1F1B370F953BA51A6A0 | N | Y | | 192.168.28.% | root | *FED29C14B2E900D70B11B1F1B370F953BA51A6A0 | Y | Y | | localhost | ranger | wtfwtfwtf | N | N | | localhost | root | 0 | Y | Y | | % | root | *FED29C14B2E900D70B11B1F1B370F953BA51A6A0 | N | Y | | 127.0.0.1 | root | *FED29C14B2E900D70B11B1F1B370F953BA51A6A0 | Y | Y | | % | ranger | *84BB87F6BF7F61703B24CE1C9AA9C0E3F2286900 | N | Y | +--------------+---------+-------------------------------------------+------------+------------+
這裡看到ranger@localhost密碼有問題,沒有權限,沒有通過password函數進行加密,跟其他的不一樣。於是進行update更改密碼並加密
mysql>UPDATE mysql.user SET Grant_priv='Y', Super_priv='Y',Password=password('wtfwtfwtf') WHERE User='ranger' and host='localhost'; mysql>flush privileges;
搞定之後記得關閉mysqld_safe,並開啟mysqld進程。
$ sudo mysqladmin -uroot -p***** shutdown $ sudo service mysqld start $ mysql -uranger -pwtfwtfwtf Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 20 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
測試後ok
新加入規則也很簡單
mysql>insert into mysql.user(Host,User,Password) values("127.0.0.1","ranger",Password=password('wtfwtfwtf'));