linux環境mysql忘記root密碼,按照以下命令執行即可

1、首先停止mysql服務進程:

service mysql stop

2、編輯mysql的配置文件my.cnf

vim /etc/my.cnf

3、找到 [mysqld]這個模塊:

在最後面添加一段代碼

skip-grant-tables   ##忽略mysql權限問題,直接登錄

4、然後保存 :wq!退出

啓動mysql服務:service mysqld start

5、然後修改密碼

mysql> use mysql; ##使用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 password=password("123456") where user="root";##更新密碼
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0
mysql> flush privileges;##刷新權限
Query OK, 0 rows affected (0.00 sec)

6、 用root登錄

[root@web1 ~]# ps -ef |grep mysql  ##顯示mysql現有的進程
root      56407      1  0 17:50 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/web1.pid
mysql     56533  56407  0 17:50 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/web1.err --pid-file=/data/mysql/web1.pid
root      56560   1737  0 17:55 pts/0    00:00:00 grep mysql
[root@web1 ~]# killall mysqld  ##刪除mysql現有進程
[root@web1 ~]# ps -ef |grep mysql
root      56566   1737  0 17:56 pts/0    00:00:00 grep mysql
[root@web1 ~]# service mysqld start ##重新啓動mysql服務
Starting MySQL. SUCCESS!
[root@web1 ~]# mysql -uroot -p ##使用新密碼登錄
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.34 Source distribution
Copyright (c) 2000, 2016, 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>

7、把剛剛添加的跳過驗證信息刪除,對安全有很大影響



發佈了29 篇原創文章 · 獲贊 45 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章