SQL 中用戶建立與權限授予
一、原有
如果大家對我的博客動態非常關注的話,應該又看到我弄了一個隨機MAN信息的小工具。但是呢,那個工具還有待加強(顯示效果不是那么的好)。
所以我就希望可以顯示一些簡短的,一目了然的。所以就有了現在的新項目:https://github.com/erlinux/GetNewOne
二、效果
三、源碼
#!/bin/usr/env python3
# -*- coding:utf-8 -*-
# @function:random dictum
# @author:jiwenkangatech@foxmail.com
# @license:MIT License
import random
import os
maths=random.randrange(1,150,1)
os.system('mysql -h cdn.itxdm.com -u article -p"article" -e "select article from article.article where id=%s" | sed -n "2p"' % (maths) )
exit(0)
所以會用到數據庫,所以針對數據庫用戶創建與授權。
四、創建SQL用戶
1、基礎版
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
**create是四大操作之一:CREATE DROP UPDATE SELECT **
2、刪除用戶
DROP USER ‘demo’@‘localhost’;
3、登陸系統
mysql -u[username] -p[password]
注意:參數-p
后加入雙引號密碼不加空格可跳過交互環境(尾部加入 -e
參數可直接執行SQL語句)
FLUSH PRIVILEGES;
刷新權限表
五、用戶權限賦予
0、預備知識
- ALL PRIVILEGES- as we saw previously, this would allow a MySQL user all access to a designated database (or if no database is selected, across the system)
- CREATE- allows them to create new tables or databases
- DROP- allows them to them to delete tables or databases
- DELETE- allows them to delete rows from tables
- INSERT- allows them to insert rows into tables
- SELECT- allows them to use the Select command to read through databases
- UPDATE- allow them to update table rows
- GRANT OPTION- allows them to grant or remove other users' privileges
1、基礎版
GRANT [type of permission] ON [database name].[table name] TO ‘[username]’@'localhost’;
tips:如果很難記住,可以嘗試先記住查詢的命令。(別給 [database name].[table name] 加雙引號)
2、權限查詢
SHOW GRANTS FOR ‘[username]’@'localhost’;
3、刪除權限
REVOKE [type of permission] ON [database name].[table name] FROM ‘[username]’@‘localhost’;
FLUSH PRIVILEGES;
必要時,刷新權限
關於四大操作可參考以下圖片文件(可保存)
差不多就是這些了。在我項目中有用到,在此記錄。
本人並非DBA,理想的職業是運維開發。上述內容指點批評~
六:回顧:
作者通過自身項目需求,分享了位於 Linux 下 SQL 用戶的創建與授權。進而分享了一系列資料:DCL、DDL、DML 與 手記一份。
七、參考:
1、以往參賽知識經驗積累
3、官方英文文檔