next up previous contents
Next: 實機操作練習題 Up: 安裝與配置 Mariadb Previous: 查詢 MariaDB 參數   Contents   DYWANG_HOME

忘記 root 密碼

  1. 先關閉 mariadb 服務。
    [root@kvm8 ~]# systemctl stop mariadb.service
    
  2. mysqld_safe 跳過 grant tables,因為接著要下命令,所以讓其背景執行。
    [root@kvm8 ~]# mysqld_safe --skip-grant-tables &
    [1] 17390
    [root@kvm8 ~]# 200124 21:08:59 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
    200124 21:09:00 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
    
  3. 接著就可以免密碼登入 mysql 了。
    [root@kvm8 ~]# mysql -uroot
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 8
    Server version: 10.3.11-MariaDB MariaDB Server
    
    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MariaDB [(none)]>
    
  4. 使用 mysql 資料庫。
    MariaDB [(none)]> 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 密碼方法一:update 所有 root 帳號。
    MariaDB [mysql]> update user set password=password("123qwe") where user='root';
    Query OK, 1 row affected (0.001 sec)
    Rows matched: 4  Changed: 1  Warnings: 0
    
  6. 變更 root 密碼方法二:alter 變更帳號 root@localhost 的密碼。
    MariaDB [mysql]> alter user 'root'@'localhost' identified by '123qwe';
    Query OK, 0 rows affected (0.001 sec)
    
  7. 變更 root 密碼方法三:update 變更帳號 roo 的密碼。
    MariaDB [mysql]> update user set authentication_string=PASSWORD("123qwe") where User='root';
    Query OK, 4 rows affected (0.002 sec)
    Rows matched: 4  Changed: 4  Warnings: 0
    
  8. 更新權限。
    MariaDB [mysql]> flush privileges;
    Query OK, 0 rows affected (0.002 sec)
    
  9. 退出。
    MariaDB [mysql]> quit
    Bye
    
  10. mysql_safe 還在背景執行,先刪除。
    [root@kvm8 ~]# jobs 
    [1]+  Running                 mysqld_safe --skip-grant-tables &
    [root@kvm8 ~]# kill %1
    
  11. 再啟動 mariadb 服務。
    [root@kvm8 ~]# systemctl start mariadb.service 
    [1]+  Done                    mysqld_safe --skip-grant-tables
    


De-Yu Wang 2020-04-07