一、環境:
CentOS 6.8
mysql 5.6
二、背景
給外包的工作人員提供我司某臺服務器的 mysql 中某個數據庫的訪問權限。
之所以要做限制,是防止他們對我司其他的數據庫非法進行操作。
三、正文
1、使用 root 管理員登陸 mysql
mysql -uroot -p123456;
2、創建新用戶
create user 'user1'@'%' identified by '07fa533360d9';
'%' - 所有情況都能訪問
‘localhost’ - 本機才能訪問
’111.222.33.44‘ - 指定 ip 才能訪問
注:修改密碼
update mysql.user set password=password('新密碼') where user='user1';
這個時候訪問,是除了默認生成的兩個數據庫,看不到任何其它的數據庫:
3、給該用戶添加權限
grant all privileges on 想授權的數據庫.* to 'user1'@'%';
all 可以替換爲 select,delete,update,create,drop
這個時候訪問,比剛纔看到的數據庫多出了一個剛剛添加的。
4、刪除用戶
Delete FROM mysql.user Where User='user1';
四、可能遇到的問題
flush privileges;
在操作 mysql 庫做用戶的增刪修改的時候,操作完畢的時候最好使用 flush privilege 命令刷新一下權限。否則可能會修改不生效