補充連接:Windows下忘記密碼處理: http://www.javashuo.com/article/p-uhhhwgvw-dp.htmlhtml
場景描述:mysql
Linux環境下忘記 root 密碼,sql
1. 修改MySQL的登陸設置:
# vim /etc/my.cnf
在[mysqld]的段中加上一句:skip-grant-tables
例如:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-grant-tables
保存而且退出vi。數據庫
2. 從新啓動mysqld
# service mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]vim
3. 登陸並修改MySQL的root密碼 服務器
# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 to server version: 3.23.56 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> USE mysql ; Database changed mysql> UPDATE user SET Password = password ( 'new-password' ) WHERE User = 'root' ; Query OK, 0 rows affected (0.00 sec) Rows matched: 2 Changed: 0 Warnings: 0 mysql> flush privileges ; Query OK, 0 rows affected (0.01 sec) mysql> quit
4.將MySQL的登陸設置修改回來
# vim /etc/my.cnf
將剛纔在[mysqld]的段中加上的skip-grant-tables刪除
保存而且退出vimsocket
5.從新啓動mysqld
# service mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]ui
************************************************this
6. 配置mysql容許遠程鏈接:spa
容許遠程用戶登陸訪問mysql的方法
須要手動增長能夠遠程訪問數據庫的用戶。
方法一(不推薦) 本地登入mysql,更改 "mysql" 數據庫裏的 "user" 表裏的 "host" 項,將"localhost"改成"%"
#mysql -u root -proot
mysql>use mysql; mysql>update user set host = '%' where user = 'root'; mysql>select host, user from user;
方法二 直接受權(推薦)
從任何主機上使用root用戶,密碼:youpassword(你的root密碼)鏈接到mysql服務器:(首先登錄Linux服務器,填寫下面代碼便可)
[root@localhost software]# mysql -u root -proot mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'youpassword' WITH GRANT OPTION; mysql>FLUSH PRIVILEGES;
操做完後切記執行如下命令刷新權限
FLUSH PRIVILEGES;
方法三:終極方法
找到mysql.cnf
註釋bind-address = 127.0.0.1
改成bind-address = 0.0.0.0
重啓服務。
方法四: 給指定用戶受權 --不須要刷新,重啓數據庫
mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> update user set host = '%' where user = '你的用戶'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0