您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關MySQL用戶和權限及破解root口令的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
MySQL用戶和權限
在MySQL中有一個系統自身就帶有的數據庫叫MySQL,數據庫裝好以后系統自帶了好幾個數據庫MySQL就是其中過一個,MySQL數據庫有個用戶賬戶權限相關的表叫user表,在其中就有創建的用戶。
MySQL中完整的用戶名是由用戶+主機名形成,主機名決定了這個用戶在哪個主機上能登陸。
一、用戶的創建和密碼修改
1.用戶的創建
create user 'USERNAME'@'HOST' identified by 'PASSWORD';
USERNAME:用戶名
HOST:主機地址
PASSWORD:密碼
示例:
MariaDB [(none)]> create user masuri@192.168.73.133 identified by 'centos'; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> select user,host,password from mysql.user; +--------+-----------------------+-------------------------------------------+ | user | host | password | +--------+-----------------------+-------------------------------------------+ | root | localhost | | | root | localhost.localdomain | | | root | 127.0.0.1 | | | root | ::1 | | | | localhost | | | | localhost.localdomain | | | masuri | 192.168.73.133 | *128977E278358FF80A246B5046F51043A2B1FCED | +--------+-----------------------+-------------------------------------------+ 7 rows in set (0.00 sec)
MySQL中有匿名賬戶,可以通過跑安全加固腳本mysql_secure_installation來進行刪除,也可以手動將其刪除。
刪除用戶:
DROP USER 'USERNAME'@'HOST';
示例:
MariaDB [(none)]> select user,host,password from mysql.user; +--------+-----------------------+-------------------------------------------+ | user | host | password | +--------+-----------------------+-------------------------------------------+ | root | localhost | | | root | localhost.localdomain | | | root | 127.0.0.1 | | | root | ::1 | | | | localhost | | | | localhost.localdomain | | | masuri | 192.168.73.133 | *128977E278358FF80A246B5046F51043A2B1FCED | +--------+-----------------------+-------------------------------------------+ 7 rows in set (0.00 sec) MariaDB [(none)]> DROP USER ''@'localhost'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> DROP USER ''@'localhost.localdomain'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> select user,host,password from mysql.user; +--------+-----------------------+-------------------------------------------+ | user | host | password | +--------+-----------------------+-------------------------------------------+ | root | localhost | | | root | localhost.localdomain | | | root | 127.0.0.1 | | | root | ::1 | | | masuri | 192.168.73.133 | *128977E278358FF80A246B5046F51043A2B1FCED | +--------+-----------------------+-------------------------------------------+ 5 rows in set (0.00 sec)
2.密碼的修改
mysql密碼的修改
SET PASSWORD FOR user = PASSWORD('cleartext password') UPDATE table SET password = password('cleartext password')
示例:
對masuri用戶做密碼的修改
MariaDB [(none)]> SET PASSWORD FOR masuri@192.168.73.133 = PASSWORD ('magedu'); Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> select user,host,password from mysql.user; +--------+-----------------------+-------------------------------------------+ | user | host | password | +--------+-----------------------+-------------------------------------------+ | root | localhost | | | root | localhost.localdomain | | | root | 127.0.0.1 | | | root | ::1 | | | masuri | 192.168.73.133 | *6B8CCC83799A26CD19D7AD9AEEADBCD30D8A8664 | +--------+-----------------------+-------------------------------------------+ #此時密碼已經發生改變
root賬號口令為空,為root口令設置口令,由于一條一條的設置太過麻煩也可以使用修改表的操作來修改密碼
MariaDB [(none)]> update mysql.user set password=password('centos') where user='root'; Query OK, 4 rows affected (0.01 sec) Rows matched: 4 Changed: 4 Warnings: 0 MariaDB [(none)]> select user,host,password from mysql.user; +--------+-----------------------+-------------------------------------------+ | user | host | password | +--------+-----------------------+-------------------------------------------+ | root | localhost | *128977E278358FF80A246B5046F51043A2B1FCED | | root | localhost.localdomain | *128977E278358FF80A246B5046F51043A2B1FCED | | root | 127.0.0.1 | *128977E278358FF80A246B5046F51043A2B1FCED | | root | ::1 | *128977E278358FF80A246B5046F51043A2B1FCED | | masuri | 192.168.73.133 | *6B8CCC83799A26CD19D7AD9AEEADBCD30D8A8664 | +--------+-----------------------+-------------------------------------------+ 5 rows in set (0.00 sec)
此時密碼已經修改但依舊無法登陸,需要將權限刷新
MariaDB [(none)]> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)
二、MySQL權限管理
權限管理涉及到多種權限的類別,比如說有管理類、程序類、數據庫級別、表級別和字段級別
管理類:能否創建用戶,能否顯示數據庫列表,能否重新加載配置文件,能否關閉數據庫,和復制相關的能否執行,能否管理進程,能否創建臨時表,能否創建數據庫中的文件。
程序類主要涉及3個程序,函數,存儲過程和觸發器,例如能否創建,修改,刪除和執行這些程序庫,表和字段級別的權限:比如能否在庫,表字段里進行增、刪、查、改等操作
1.授權GRANT
授權用戶時如果用戶不存在可以將其創建出來,在授權前首先要確認自己是管理員有授權的權限。
GRANT priv_type [(column_list)] [, priv_type [(column_list)]] ... ON [object_type] priv_level TO user_specification [, user_specification] ... [REQUIRE {NONE | ssl_option [[AND] ssl_option] ...}] [WITH with_option ...]
示例:
創建一個wordpress的用戶,并授權。
MariaDB [(none)]> CREATE DATABASE wordpress; Query OK, 1 row affected (0.02 sec) MariaDB [(none)]> GRANT ALL ON wordpress.* TO wpuser@'192.168.73.%' identified by 'mylinuxops'; Query OK, 0 rows affected (0.00 sec)
2.查看用戶的權限
MariaDB [(none)]> show grants for wpuser@'192.168.73.%'; +------------------------------------------------------------------------------------------------------------------+ | Grants for wpuser@192.168.73.% | +------------------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'wpuser'@'192.168.73.%' IDENTIFIED BY PASSWORD '*EC0DBFB480593BB6ED2EC028A4231A72D8137406' | | GRANT ALL PRIVILEGES ON `wordpress`.* TO 'wpuser'@'192.168.73.%' | +------------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec)
3.授權的其他選項
MAX_QUESRIES_PER_HOUR count #每小時最多查多少次 MAX_UPDATES_PER_HOUR count #每小時最多改多少次 MAX_CONNECTIONS_PER_HOUR count #每小時最多連多少次 MAX_USER_CONNECTIONS count #用戶的最大數連接數
取消權限
REVOKE priv_type [(column_list)] [, priv_type [(column_list)]] ... ON [object_type] priv_level FROM user [, user] ...
示例:
MariaDB [(none)]> revoke delete on wordpress.* from wpuser@'192.168.73.%'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show grants for wpuser@'192.168.73.%'; +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Grants for wpuser@192.168.73.% | +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'wpuser'@'192.168.73.%' IDENTIFIED BY PASSWORD '*EC0DBFB480593BB6ED2EC028A4231A72D8137406' | | GRANT SELECT, INSERT, UPDATE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `wordpress`.* TO 'wpuser'@'192.168.73.%' | +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec) # 此時wpuser@'192.168.73.%'已經沒有了delete權限
MySQL的root口令破解
工作中有時候可能會遇到root口令丟失的情況,此時可以通過以下方法進行找回root口令
以下為示范如何破解root口令
一、密碼未知無法登陸MySQL
[root@localhost ~]# mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
二、破解
1.修改配置文件/etc/my.cnf,添加兩行參數
skip_grant_tables:跳過授權表信息,此項生效后再次使用MySQL就無需使用密碼了,但是遠程的其他用戶也可以不使用密碼登陸,有一定的風險性
skip_networking:關閉網路功能,由于光啟用skip_grant_tables選項,其他用戶也可以無需密碼登陸MySQL非常危險,所以需要關閉網路功能只允許本地的用戶進行操作。
[root@localhost ~]# vim /etc/my.cnf [mysqld] skip_networking=on #不啟用網絡功能 skip_grant_tables=on #跳過授權表 [root@localhost ~]# service mysqld restart #對位置文件修改后需要重新啟動服務 Restarting mysqld (via systemctl): [ OK ]
2.登陸MySQL,進行密碼修改
[root@localhost ~]# mysql #此時已經無需輸入密碼就能登陸 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 11 Server version: 10.2.23-MariaDB-log Source distribution 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)]> UPDATE mysql.user SET password=PASSWORD('123456') where user='root'; #對root的口令進行修改 Query OK, 4 rows affected (0.01 sec) Rows matched: 4 Changed: 4 Warnings: 0
3.口令修改完畢后,需要將配置文件恢復
將剛才啟用的兩個選項進行注銷或者刪除,然后重啟服務
[root@localhost ~]# vim /etc/my.cnf [mysqld] #skip_networking=on #skip_grant_tables=on [root@localhost ~]# service mysqld restart Restarting mysqld (via systemctl): [ OK ]
4.使用新口令登陸MySQL
[root@localhost ~]# mysql -uroot -p123456 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 10 Server version: 10.2.23-MariaDB-log Source distribution 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)]>
關于“MySQL用戶和權限及破解root口令的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。