1、創(chuàng)建新用戶
通過(guò)root用戶登錄之后創(chuàng)建
>>?grant all privileges on *.* to?testuser@localhost identified by?"123456"?; // 創(chuàng)建新用戶危纫,用戶名為testuser伏嗜,密碼為123456 ;
>>?grant all privileges on *.* to?testuser@localhost identified by?"123456"?; // 設(shè)置用戶testuser救拉,可以在本地訪問(wèn)mysql
>>?grant all privileges on *.* to?testuser@"%" identified by?"123456"?; // 設(shè)置用戶testuser,可以在遠(yuǎn)程訪問(wèn)mysql
>>?flush privileges?; // mysql 新設(shè)置用戶或更改密碼后需用flush privileges刷新MySQL的系統(tǒng)權(quán)限相關(guān)表,否則會(huì)出現(xiàn)拒絕訪問(wèn)歌径,還有一種方法,就是重新啟動(dòng)mysql服務(wù)器亲茅,來(lái)使新設(shè)置生效
2回铛、設(shè)置用戶訪問(wèn)數(shù)據(jù)庫(kù)權(quán)限
>>?grant all privileges on?test_db.*?to?testuser@localhost identified by "123456" ; // 設(shè)置用戶testuser狗准,只能訪問(wèn)數(shù)據(jù)庫(kù)test_db,其他數(shù)據(jù)庫(kù)均不能訪問(wèn)?茵肃;
>>?grant all privileges on?*.*?to?testuser@localhost identified by "123456" ; // 設(shè)置用戶testuser腔长,可以訪問(wèn)mysql上的所有數(shù)據(jù)庫(kù) ;
>>?grant all privileges on?test_db.user_infor?to?testuser@localhost identified by "123456" ; // 設(shè)置用戶testuser验残,只能訪問(wèn)數(shù)據(jù)庫(kù)test_db的表user_infor捞附,數(shù)據(jù)庫(kù)中的其他表均不能訪問(wèn)?;
3您没、設(shè)置用戶操作權(quán)限
>>?grant?all privileges?on *.* to?testuser@localhost identified by "123456" WITH GRANT OPTION?; //設(shè)置用戶testuser鸟召,擁有所有的操作權(quán)限,也就是管理員 氨鹏;
>> grant?select?on *.* to?testuser@localhost identified by "123456" WITH GRANT OPTION?; //設(shè)置用戶testuser欧募,只擁有【查詢】操作權(quán)限 ;
>>?grant?select,insert?on *.* to?testuser@localhost identified by "123456" ?; //設(shè)置用戶testuser仆抵,只擁有【查詢\插入】操作權(quán)限 跟继;
>>?grant?select,insert,update,delete?on *.* to?testuser@localhost identified by "123456" ?; //設(shè)置用戶testuser,只擁有【查詢\插入】操作權(quán)限 镣丑;
>>?REVOKE?select,insert?ON what FROM?testuser //取消用戶testuser的【查詢\插入】操作權(quán)限 舔糖;
4、設(shè)置用戶遠(yuǎn)程訪問(wèn)權(quán)限
>>?grant all privileges on *.* to?testuser@“192.168.1.100”?identified by?"123456"?; //設(shè)置用戶testuser传轰,只能在客戶端IP為192.168.1.100上才能遠(yuǎn)程訪問(wèn)mysql 剩盒;
5、關(guān)于root用戶的訪問(wèn)設(shè)置
設(shè)置所有用戶可以遠(yuǎn)程訪問(wèn)mysql慨蛙,修改my.cnf配置文件辽聊,將bind-address = 127.0.0.1前面加“#”注釋掉,這樣就可以允許其他機(jī)器遠(yuǎn)程訪問(wèn)本機(jī)mysql了期贫;
>>?grant all privileges on *.* to?root@"%" identified by?"123456"?; // 設(shè)置用戶root跟匆,可以在遠(yuǎn)程訪問(wèn)mysql
>>?select host,user from user; //查詢mysql中所有用戶權(quán)限
? ? ?關(guān)閉root用戶遠(yuǎn)程訪問(wèn)權(quán)限
>>?delete from user where user="root" and host="%" ; //禁止root用戶在遠(yuǎn)程機(jī)器上訪問(wèn)mysql
>>?flush privileges?; //修改權(quán)限之后,刷新MySQL的系統(tǒng)權(quán)限相關(guān)表方可生效