忘記 root 密碼

  1. 先關閉 mysqld 服務。
    [root@kvm6 ~]# /etc/init.d/mysqld stop
    Stopping mysqld:                                     [  OK  ]
    
  2. mysqld_safe 跳過 grant tables,因為接著要下命令,所以讓其背景執行。
    [root@kvm6 ~]# mysqld_safe --skip-grant-tables &
    [1] 30343
    [root@kvm6 ~]# 190520 20:21:32 mysqld_safe Logging to '/var/log/mysqld.log'.
    190520 20:21:32 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
    
  3. 接著就可以免密碼登入 mysql 了。
    [root@kvm6 ~]# mysql -uroot
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 1
    Server version: 5.5.49 MySQL Community Server (GPL) by Remi
    
    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.
    
  4. 使用 mysql 資料庫。
    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
    
  5. 變更 root 密碼。
    mysql> update user set password=password("123qwe") where user='root';
    Query OK, 0 rows affected (0.00 sec)
    Rows matched: 3  Changed: 0  Warnings: 0
    
  6. 更新權限。
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
  7. 退出。
    mysql> quit
    Bye
    
  8. 重新啟動 mysqld 服務。
    [root@kvm6 ~]# /etc/init.d/mysqld restart
    190520 20:22:55 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
    Stopping mysqld:                                           [  OK  ]
    Starting mysqld:                                           [  OK  ]
    [1]+  Done                    mysqld_safe --skip-grant-tables