MySQL用戶和權(quán)限
在MySQL中有一個(gè)系統(tǒng)自身就帶有的數(shù)據(jù)庫叫MySQL,數(shù)據(jù)庫裝好以后系統(tǒng)自帶了好幾個(gè)數(shù)據(jù)庫MySQL就是其中過一個(gè),MySQL數(shù)據(jù)庫有個(gè)用戶賬戶權(quán)限相關(guān)的表叫user表,在其中就有創(chuàng)建的用戶。
MySQL中完整的用戶名是由用戶+主機(jī)名形成,主機(jī)名決定了這個(gè)用戶在哪個(gè)主機(jī)上能登陸。
一、用戶的創(chuàng)建和密碼修改
1.用戶的創(chuàng)建
create user 'USERNAME'@'HOST' identified by 'PASSWORD';
USERNAME:用戶名
HOST:主機(jī)地址
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來進(jìn)行刪除,也可以手動(dòng)將其刪除。
刪除用戶:
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')
示例:
對(duì)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 |
+--------+-----------------------+-------------------------------------------+
#此時(shí)密碼已經(jīng)發(fā)生改變
root賬號(hào)口令為空,為root口令設(shè)置口令,由于一條一條的設(shè)置太過麻煩也可以使用修改表的操作來修改密碼
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)
此時(shí)密碼已經(jīng)修改但依舊無法登陸,需要將權(quán)限刷新
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
二、MySQL權(quán)限管理
權(quán)限管理涉及到多種權(quán)限的類別,比如說有管理類、程序類、數(shù)據(jù)庫級(jí)別、表級(jí)別和字段級(jí)別
管理類:能否創(chuàng)建用戶,能否顯示數(shù)據(jù)庫列表,能否重新加載配置文件,能否關(guān)閉數(shù)據(jù)庫,和復(fù)制相關(guān)的能否執(zhí)行,能否管理進(jìn)程,能否創(chuàng)建臨時(shí)表,能否創(chuàng)建數(shù)據(jù)庫中的文件。
程序類主要涉及3個(gè)程序,函數(shù),存儲(chǔ)過程和觸發(fā)器,例如能否創(chuàng)建,修改,刪除和執(zhí)行這些程序庫,表和字段級(jí)別的權(quán)限:比如能否在庫,表字段里進(jìn)行增、刪、查、改等操作
1.授權(quán)GRANT
授權(quán)用戶時(shí)如果用戶不存在可以將其創(chuàng)建出來,在授權(quán)前首先要確認(rèn)自己是管理員有授權(quán)的權(quán)限。
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 ...]
示例:
創(chuàng)建一個(gè)wordpress的用戶,并授權(quán)。
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.查看用戶的權(quán)限
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.授權(quán)的其他選項(xiàng)
MAX_QUESRIES_PER_HOUR count #每小時(shí)最多查多少次
MAX_UPDATES_PER_HOUR count #每小時(shí)最多改多少次
MAX_CONNECTIONS_PER_HOUR count #每小時(shí)最多連多少次
MAX_USER_CONNECTIONS count #用戶的最大數(shù)連接數(shù)
取消權(quán)限
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)
# 此時(shí)wpuser@'192.168.73.%'已經(jīng)沒有了delete權(quán)限
MySQL的root口令破解
工作中有時(shí)候可能會(huì)遇到root口令丟失的情況,此時(shí)可以通過以下方法進(jìn)行找回root口令
以下為示范如何破解root口令
一、密碼未知無法登陸MySQL
[root@localhost ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
二、破解
1.修改配置文件/etc/my.cnf,添加兩行參數(shù)
skip_grant_tables:跳過授權(quán)表信息,此項(xiàng)生效后再次使用MySQL就無需使用密碼了,但是遠(yuǎn)程的其他用戶也可以不使用密碼登陸,有一定的風(fēng)險(xiǎn)性
skip_networking:關(guān)閉網(wǎng)路功能,由于光啟用skip_grant_tables選項(xiàng),其他用戶也可以無需密碼登陸MySQL非常危險(xiǎn),所以需要關(guān)閉網(wǎng)路功能只允許本地的用戶進(jìn)行操作。
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
skip_networking=on #不啟用網(wǎng)絡(luò)功能
skip_grant_tables=on #跳過授權(quán)表
[root@localhost ~]# service mysqld restart #對(duì)位置文件修改后需要重新啟動(dòng)服務(wù)
Restarting mysqld (via systemctl): [ OK ]
2.登陸MySQL,進(jìn)行密碼修改
[root@localhost ~]# mysql #此時(shí)已經(jīng)無需輸入密碼就能登陸
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'; #對(duì)root的口令進(jìn)行修改
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0
3.口令修改完畢后,需要將配置文件恢復(fù)
將剛才啟用的兩個(gè)選項(xiàng)進(jìn)行注銷或者刪除,然后重啟服務(wù)
[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)]>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- MySQL查詢用戶權(quán)限的方法總結(jié)
- MySQL給新建用戶并賦予權(quán)限最簡(jiǎn)單的方法
- MySQL用戶與權(quán)限的管理詳解
- mysql語句查詢用戶權(quán)限過程詳解
- MySQL用戶賬戶管理和權(quán)限管理深入講解
- 詳解mysql8.0創(chuàng)建用戶授予權(quán)限報(bào)錯(cuò)解決方法
- MySQL用戶權(quán)限驗(yàn)證與管理方法詳解
- mysql創(chuàng)建本地用戶及賦予數(shù)據(jù)庫權(quán)限的方法示例
- MySQL用戶權(quán)限管理詳解
- MySql設(shè)置指定用戶數(shù)據(jù)庫查看查詢權(quán)限