關於mysql的用戶管理,筆記
1、創建新用戶
通過root用戶登錄之后創建
>> grant all privileges on *.* to testuser@localhost identified by "123456" ; // 創建新用戶,用戶名為testuser,密碼為123456 ;
>> grant all privileges on *.* to testuser@localhost identified by "123456" ; // 設置用戶testuser,可以在本地訪問mysql
>> grant all privileges on *.* to testuser@"%" identified by "123456" ; // 設置用戶testuser,可以在遠程訪問mysql
>> flush privileges ; // mysql 新設置用戶或更改密碼后需用flush privileges刷新MySQL的系統權限相關表,否則會出現拒絕訪問,還有一種方法,就是重新啟動mysql服務器,來使新設置生效
2、設置用戶訪問數據庫權限
>> grant all privileges on test_db.* to testuser@localhost identified by "123456" ; // 設置用戶testuser,只能訪問數據庫test_db,其他數據庫均不能訪問 ;
>> grant all privileges on *.* to testuser@localhost identified by "123456" ; // 設置用戶testuser,可以訪問mysql上的所有數據庫 ;
>> grant all privileges on test_db.user_infor to testuser@localhost identified by "123456" ; // 設置用戶testuser,只能訪問數據庫test_db的表user_infor,數據庫中的其他表均不能訪問 ;
3、設置用戶操作權限
>> grant all privileges on *.* to testuser@localhost identified by "123456" WITH GRANT OPTION ; //設置用戶testuser,擁有所有的操作權限,也就是管理員 ;
>> grant select on *.* to testuser@localhost identified by "123456" WITH GRANT OPTION ; //設置用戶testuser,只擁有【查詢】操作權限 ;
>> grant select,insert on *.* to testuser@localhost identified by "123456" ; //設置用戶testuser,只擁有【查詢\插入】操作權限 ;
>> grant select,insert,update,delete on *.* to testuser@localhost identified by "123456" ; //設置用戶testuser,只擁有【查詢\插入】操作權限 ;
>> REVOKE select,insert ON what FROM testuser //取消用戶testuser的【查詢\插入】操作權限 ;
4、設置用戶遠程訪問權限
>> grant all privileges on *.* to testuser@“192.168.1.100” identified by "123456" ; //設置用戶testuser,只能在客戶端IP為192.168.1.100上才能遠程訪問mysql ;
5、關於root用戶的訪問設置
設置所有用戶可以遠程訪問mysql,修改my.cnf配置文件,將bind-address = 127.0.0.1前面加“#”注釋掉,這樣就可以允許其他機器遠程訪問本機mysql了;
>> grant all privileges on *.* to root@"%" identified by "123456" ; // 設置用戶root,可以在遠程訪問mysql
>> select host,user from user; //查詢mysql中所有用戶權限
關閉root用戶遠程訪問權限
>> delete from user where user="root" and host="%" ; //禁止root用戶在遠程機器上訪問mysql
>> flush privileges ; //修改權限之后,刷新MySQL的系統權限相關表方可生效