破解當前MySQL管理用戶(root)的密碼

密碼恢復及設置
用戶受權及撤銷
MySQL管理工具

1 密碼恢復及設置
1.1 問題php

本案例要求熟悉MySQL管理密碼的控制,完成如下任務操做:html

練習重置MySQL管理密碼的操做
經過正常途徑設置MySQL數據庫的管理密碼

1.2 步驟node

實現此案例須要按照以下步驟進行。mysql

步驟一:重置MySQL管理密碼web

1)首先中止已運行的MySQL服務程序sql

[root@dbsvr1 ~]# systemctl  stop mysqld.service           //中止服務
[root@dbsvr1 ~]# systemctl  status mysqld.service          //確認狀態
mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled)
   Active: inactive (dead) since 五 2017-04-07 23:01:38 CST; 21s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 20260 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 20238 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 20262 (code=exited, status=0/SUCCESS)

2)而後跳過受權表啓動MySQL服務程序數據庫

這一步主要利用mysqld的 --skip-grant-tables選項vim

修改my.cnf配置,添加 skip_grant_tables=1啓動設置:瀏覽器

[root@dbsvr1 ~]# vim /etc/my.cnf
[mysqld]
skip_grant_tables=1
.. ..
[root@dbsvr1 ~]# systemctl  restart mysqld.service
[root@dbsvr1 ~]# service mysql status
mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled)
   Active: active (running) since 五 2017-04-07 23:40:20 CST; 40s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 11698 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 11676 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 11701 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─11701 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.p...

3)使用mysql命令鏈接到MySQL服務,重設root的密碼安全

因爲前一步啓動的MySQL服務跳過了受權表,因此能夠root從本機直接登陸

[root@dbsvr1 ~]# mysql -u root
Enter password:                                 //直接回車便可
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17 MySQL Community Server (GPL)
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>

進入 mysql> 環境後,經過修改mysql庫中user表的相關記錄,重設root用戶從本機登陸的密碼:

mysql> UPDATE mysql.user SET authentication_string=PASSWORD('1234567')
    -> WHERE user='root' AND host='localhost';              //重設root的密碼
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1
mysql> FLUSH PRIVILEGES;                                  //刷新受權表
Query OK, 0 rows affected (0.01 sec)
mysql> exit                                              //退出mysql> 環境
Bye

經過執行「FLUSH PRIVILEGES;」可以使受權表當即生效,對於正常運行的MySQL服務,也能夠用上述方法來修改密碼,不用重啓服務。本例中由於是恢復密碼,最好重啓MySQL服務程序,因此上述「FLUSH PRIVILEGES;」操做可跳過。

4)從新以正常方式啓動MySQL服務程序,驗證新密碼

若是前面是修改/etc/my.cnf配置的方法來跳過受權表,則重置root密碼後,應去除相應的設置以恢復正常:

[root@dbsvr1 ~]# vim /etc/my.cnf
[mysqld]
#skip_grant_tables=1                              //註釋掉或刪除此行
.. ..

按正常方式,經過mysql腳本重啓服務便可:

[root@dbsvr1 ~]# systemctl  restart mysqld.service

驗證無密碼登陸時,將會被拒絕:

[root@dbsvr1 ~]# mysql -u root
Enter password:                            //沒有跳過受權表回車會報錯
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

只有提供重置後的新密碼,才能成功登入:

[root@dbsvr1 ~]# mysql -u root –p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17 MySQL Community Server (GPL)
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>

步驟二:正常設置MySQL管理密碼

正常的前提是:已知當前MySQL管理用戶(root)的密碼。

1)方法1,在Shell命令行下設置

使用mysqladmin管理工具,須要驗證舊的密碼。好比,如下操做將會把root的密碼設置爲 1234567:

[root@dbsvr1 ~]# mysqladmin -u root -p password '1234567'                    
Enter password:                                   //驗證原來的密碼
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.                              //提示明文修改不安全,並非報錯

2)方法2,以root登入mysql> 後,使用SET PASSWORD指令設置

這個與新安裝MySQL-server後首次修改密碼時要求的方式相同,平時也能夠用:

mysql> SET PASSWORD FOR root@localhost=PASSWORD('1234567');
Query OK, 0 rows affected, 1 warning (0.00 sec)

3)方法3,以root登入mysql> 後,使用GRANT受權工具設置

這個是最多見的用戶受權方式(下一節會作更多受權的練習):

mysql> GRANT all ON *.* TO root@localhost IDENTIFIED BY '1234567';
Query OK, 0 rows affected, 1 warning (0.00 sec)

4)方法4,以root登入mysql> 後,使用UPDATE更新相應的表記錄

這種方法與恢復密碼時的操做相同:

mysql> UPDATE mysql.user SET authentication_string=PASSWORD('1234567')
    -> WHERE user='root' AND host='localhost';          //重設root的密碼
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 1
mysql> FLUSH PRIVILEGES;                                  //刷新受權表
Query OK, 0 rows affected (0.00 sec)

在上述方法中,須要特別注意:當MySQL服務程序以 skip-grant-tables 選項啓動時,若是未執行「FLUSH PRIVILEGES;」操做,是沒法經過SET PASSWORD或者GRANT方式來設置密碼的。好比,驗證這兩種方式時,都會看到ERROR 1290的出錯提示:

mysql> SET PASSWORD FOR root@localhost=PASSWORD('1234567');
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> GRANT all ON *.* TO root@localhost IDENTIFIED BY '1234567';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

2 用戶受權及撤銷
2.1 問題

容許root從192.168.4.0/24網段 訪問,對全部庫/表有徹底權限,密碼爲tarena
添加一個管理帳號dba007,徹底控制及受權
撤銷root從本機訪問的權限,而後恢復
容許webuser從任意客戶機登陸,只對webdb庫有徹底權限,密碼爲 888888
撤銷webuser的徹底權限,改成查詢權限

2.2 方案

使用2臺RHEL 7虛擬機,如圖-1所示。其中192.168.4.10是MySQL服務器,受權及撤銷操做均在此服務器上執行;而192.168.4.120做爲測試客戶機,須要安裝好MySQL-client軟件包,以便提供mysql命令。

圖-1

同時,MySQL服務器自己(192.168.4.10)也能夠做爲測試客戶機。
2.3 步驟

實現此案例須要按照以下步驟進行。

步驟一:用戶受權及撤銷

1)容許root從192.168.4.0/24訪問,對全部庫表有徹底權限,密碼爲tarena。

受權以前,從192.168.4.0/24網段的客戶機訪問時,將會被拒絕:

[root@host120 ~]# mysql -u root -p -h 192.168.4.10
Enter password:                                  //輸入正確的密碼
ERROR 2003 (HY000): Host '192.168.4.120' is not allowed to connect to this MySQL server

受權操做,此處可設置與從localhost訪問時不一樣的密碼:

mysql> GRANT all ON *.* TO root@'192.168.4.%' IDENTIFIED BY 'tarena';
Query OK, 0 rows affected (0.00 sec)

再次從192.168.4.0/24網段的客戶機訪問時,輸入正確的密碼後可登入:

[root@host120 ~]# mysql -u root -p -h 192.168.4.10
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.7.17 MySQL Community Server (GPL)
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>

從網絡登入後,測試新建一個庫、查看全部庫:

mysql> CREATE DATABASE rootdb;                  //建立新庫rootdb
Query OK, 1 row affected (0.06 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| home               |
| mysql              |
| performance_schema |
| rootdb             |                         //新建的rootdb庫
| sys                |
| userdb             |
+--------------------+
7 rows in set (0.01 sec)

2)在Mysql服務器上創建一個管理帳號dba007,對全部庫徹底控制,並賦予其受權的權限

新建帳號並受權:

mysql> GRANT all ON *.* TO dba007@localhost
    -> IDENTIFIED BY '1234567'
    -> WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

查看dba007的權限:

mysql> SHOW GRANTS FOR dba007@localhost;
+-----------------------------------------------------------------------+
| Grants for dba007@localhost                                           |
+-----------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'dba007'@'localhost' WITH GRANT OPTION |
+-----------------------------------------------------------------------+
1 row in set (0.00 sec)

3)撤銷root從本機訪問的權限,而後恢復

注意:若是沒有事先創建其餘管理帳號,請不要輕易撤銷root用戶的本地訪問權限,不然恢復起來會比較困難,甚至不得不重裝數據庫。

撤銷root對數據庫的操做權限:

mysql> REVOKE all ON *.* FROM root@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW GRANTS FOR root@localhost;
+--------------------------------------------------------------+
| Grants for root@localhost                                    |
+--------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'root'@'localhost' WITH GRANT OPTION   |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+--------------------------------------------------------------+
2 rows in set (0.00 sec)

驗證撤銷後的權限效果:

mysql> exit                                      //退出當前MySQL鏈接
Bye
[root@dbsvr1 ~]# mysql -u root -p                  //從新以root從本地登入
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.15 MySQL Community Server (GPL)
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> CREATE DATABASE newdb2014;                  //嘗試新建庫失敗
ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'newdb2014'
mysql> DROP DATABASE rootdb;                          //嘗試刪除庫失敗
ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'rootdb'

嘗試以當前的root用戶恢復權限,也會失敗(無權更新受權表):

mysql> GRANT all ON *.* TO root@localhost IDENTIFIED BY '1234567';
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

怎麼辦呢?

退出當前MySQL鏈接,以上一步添加的管理帳號dba007登入:

mysql> exit                                          //退出當前MySQL鏈接
Bye
[root@dbsvr1 ~]# mysql -u dba007 -p                   //以另外一個管理帳號登入
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 5.7.17 MySQL Community Server (GPL)
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.

由管理帳號dba007從新爲root添加本地訪問權限:

mysql> GRANT all ON *.* TO root@localhost IDENTIFIED BY '1234567';
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW GRANTS FOR root@localhost;              //查看恢復結果
+---------------------------------------------------------------------+
| Grants for root@localhost                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)

退出,再從新以root登入,測試一下看看,權限又恢復了吧:

mysql> exit                                      //退出當前MySQL鏈接
Bye
[root@dbsvr1 ~]# mysql -u root -p                 //從新以root登入
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 5.7.17 MySQL Community Server (GPL)
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> CREATE DATABASE newdb2014;                  //成功建立新庫
Query OK, 1 row affected (0.00 sec)

4)容許webuser從任意客戶機登陸,只對webdb庫有徹底權限,密碼爲 888888

添加受權:

mysql> GRANT all ON webdb.* TO webuser@'%' IDENTIFIED BY '888888';
Query OK, 0 rows affected (0.00 sec)

查看受權結果:

mysql> SHOW GRANTS FOR webuser@'%';
+----------------------------------------------------+
| Grants for webuser@%                               |
+----------------------------------------------------+
| GRANT USAGE ON *.* TO 'webuser'@'%'                |
| GRANT ALL PRIVILEGES ON `webdb`.* TO 'webuser'@'%' |
+----------------------------------------------------+
2 rows in set (0.00 sec)

5)撤銷webuser的徹底權限,改成查詢權限

撤銷全部權限:

mysql> REVOKE all ON webdb.* FROM webuser@'%';
Query OK, 0 rows affected (0.00 sec)

只賦予查詢權限:

mysql> GRANT select ON webdb.* TO webuser@'%';
Query OK, 0 rows affected (0.00 sec)

確認受權更改結果:

mysql> SHOW GRANTS FOR webuser@'%';
+--------------------------------------------+
| Grants for webuser@%                       |
+--------------------------------------------+
| GRANT USAGE ON *.* TO 'webuser'@'%'        |
| GRANT SELECT ON `webdb`.* TO 'webuser'@'%' |
+--------------------------------------------+
2 rows in set (0.00 sec)

3 MySQL管理工具
3.1 問題

本案例要求基於LAMP平臺部署一套phpMyAdmin應用系統,實現對MySQL服務器的Web方式管理。
3.2 方案

使用2臺RHEL6虛擬機 + 1臺Windows 7真機,如圖-2所示。其中192.168.4.10是MySQL服務器,受權操做在此服務器上執行;另外一臺Linux服務器192.168.4.6上部署phpMyAdmin管理平臺,實現從瀏覽器訪問的Web管理方式。

圖-2
3.3 步驟

實現此案例須要按照以下步驟進行。

步驟一:在MySQL服務器上配置用戶訪問受權

爲了實驗方便起見,直接以root用戶爲例,容許其從192.168.4.0/24網段訪問,密碼設置爲1234567。

mysql> GRANT all ON *.* TO root@'192.168.4.%' IDENTIFIED BY '1234567';
Query OK, 0 rows affected, 1 warning (0.00 sec)

步驟二:搭建phpMyAdmin管理平臺(192.168.4.6)

phpMyAdmin是以PHP語言開發的一套用來管理MySQL數據庫的網頁程序,所以須要有支持PHP的網站服務器才能正常使用phpMyAdmin平臺。

1)LAMP平臺的簡易部署

直接以yum方式安裝httpd、mysql、php、php-mysql軟件包,本例中只須要MySQl客戶端程序,無需安裝mysql-server:

[root@dbsvr ~]# yum -y install httpd mariadb php php-mysql
.. ..

因爲RHEL 7未提供php-mbsring包,而phpMyAdmin套件須要相關庫文件,所以須要額外下載適用的RPM包(由教員提供),安裝時忽略依賴關係便可:

[root@dbsvr ~]# rpm -ivh php-mbstring-5.3.3-26.el6.x86_64.rpm -nodeps
.. ..

完成安裝之後,對httpd服務配置稍做調整,啓動httpd網站服務:

[root@dbsvr ~]# vim /etc/httpd/conf/httpd.conf
ServerName localhost.localdomain
.. ..
DirectoryIndex  index.php index.html
.. ..
[root@dbsvr ~]# systemctl  restart httpd

2)下載、部署phpMyAdmin套件

訪問http://www.phpmyadmin.net/,下載支持多語言的源碼程序包phpMyAdmin-4.1.2-all-languages.zip。

將下載回來的源碼包解壓,並部署到網站目錄:

[root@dbsvr ~]# unzip phpMyAdmin-4.1.2-all-languages.zip
[root@dbsvr ~]# mv phpMyAdmin-4.1.2-all-languages /var/www/html/pma

切換到部署後的pma程序目錄,拷貝配置文件,並修改配置以正確指定MySQL服務器的IP地址。

[root@dbsvr ~]# cd /var/www/html/pma/
[root@dbsvr pma]# cp config.sample.inc.php config.inc.php
[root@dbsvr pma]# vim config.inc.php
<?php
.. ..
$cfg[‘blowfish_secret’]=’tarena’;          //在單引號裏隨意添加字符,若是不修改這項,會報錯【配置文件如今須要絕密的短語密碼(blowfish_secret)】。
.. ..
$cfg['Servers'][$i]['host'] = '192.168.4.10';
.. ..
?>

3)從瀏覽器訪問phpMyAdmin系統

在Windows 7客戶機中,打開IE網頁瀏覽器,訪問部署了phpMyAdmin系統的網站http://192.168.4.6/pma/index.php,便可打開phpMyAdmin管理平臺。如圖-3所示,輸入正確的數據庫用戶名(如root)及密碼登入便可。

圖-3

登入成功後,如圖-4所示,便可在受權範圍內對MySQL數據庫進行管理。

圖-4

相關文章
相關標籤/搜索