在mysql中用戶權(quán)限是一個(gè)很重析 參數(shù),因?yàn)榕_(tái)mysql服務(wù)器中會(huì)有大量的用戶卵皂,每個(gè)用戶的權(quán)限需要不一樣的秩铆,下面我來介紹如何給mysql用戶分配權(quán)限吧,有需要了解的朋友可參考灯变。
1,Mysql下創(chuàng)建新的用戶
語法:
1.create user 用戶名 identified by '密碼';
例:create user xiaogang identified by '123456';
新創(chuàng)建的用戶殴玛,默認(rèn)情況下是沒有任何權(quán)限的。
2. 如何給用戶分配權(quán)限
語法:
1.grant 權(quán)限 on 數(shù)據(jù)庫.數(shù)據(jù)表 to '用戶' @ '主機(jī)名';
例:給 xiaogang 分配所有的權(quán)限
grant all on *.* to 'xiaogang'@'%';
這個(gè)時(shí)候 xiaogang 就擁有了 所有權(quán)限了
3 如何更精準(zhǔn)的控制用戶的權(quán)限呢添祸?
1.grant 權(quán)限 on 數(shù)據(jù)庫.數(shù)據(jù)表 to '用戶' @ '主機(jī)名';
例:讓 xiaogang 有查詢 tmp 數(shù)據(jù)庫 tmp1 表的權(quán)限滚粟;
grant select on temp.temp1 to 'xiaogang'@'%';?? //這個(gè)時(shí)候 xiaogang 就具有查詢temp小的temp1的權(quán)限了。
例如:
mysql>grant select,insert,update,delete,create,drop on vtdc.employee to joe@10.163.225.87 identified by ‘123′;
給來自10.163.225.87的用戶joe分配可對(duì)數(shù)據(jù)庫vtdc的employee表進(jìn)行select,insert,update,delete,create,drop等操作的權(quán)限刃泌,并設(shè)定口令為123凡壤。
mysql>grant all privileges on vtdc.* to joe@10.163.225.87 identified by ‘123′;
給來自10.163.225.87的用戶joe分配可對(duì)數(shù)據(jù)庫vtdc所有表進(jìn)行所有操作的權(quán)限署尤,并設(shè)定口令為123。
mysql>grant all privileges on *.* to joe@10.163.225.87 identified by ‘123′;
給來自10.163.225.87的用戶joe分配可對(duì)所有數(shù)據(jù)庫的所有表進(jìn)行所有操作的權(quán)限亚侠,并設(shè)定口令為123曹体。
mysql>grant all privileges on *.* to joe@localhost identified by ‘123′;
給本機(jī)用戶joe分配可對(duì)所有數(shù)據(jù)庫的所有表進(jìn)行所有操作的權(quán)限,并設(shè)定口令為123硝烂。
4. 如何收回 權(quán)限,一般指有root用戶才具有該權(quán)限
語法:
1.revoke 權(quán)限 on? 數(shù)據(jù)庫.數(shù)據(jù)表 from '用戶'@'主機(jī)名';
例:收回 xiaogang的所有權(quán)限
revoke all on *.* from 'xiaogang' @'%';
好了下面我個(gè)把步驟總結(jié)一下很具體的一個(gè)過程
第一步:mysql服務(wù)的啟動(dòng)和停止
net stop mysql
net start mysql
第二步:直接登陸mysql
語法如下: mysql -u用戶名 -p用戶密碼
鍵入命令mysql -uroot -p箕别, 回車后提示你輸入密碼,輸入123456滞谢,然后回車即可進(jìn)入到mysql中了究孕,mysql的提示符是:
mysql>
注意,如果是連接到另外的機(jī)器上爹凹,則需要加入一個(gè)參數(shù)-h機(jī)器IP
第三步:增加新用戶
格式:grant 權(quán)限 on 數(shù)據(jù)庫.* to 用戶名@登錄主機(jī) identified by "密碼"
如,增加一個(gè)用戶user1密碼為password1镶殷,讓其可以在本機(jī)上登錄禾酱, 并對(duì)所有數(shù)據(jù)庫有查詢、插入绘趋、修改颤陶、刪除的權(quán)限。首先用以root用戶連入mysql陷遮,然后鍵入以下命令:
grant select,insert,update,delete on *.* to user1@localhost Identified by "password1";
如果希望該用戶能夠在任何機(jī)器上登陸mysql滓走,則將localhost改為"%"。
如果你不想user1有密碼帽馋,可以再打一個(gè)命令將密碼去掉搅方。
grant select,insert,update,delete on mydb.* to user1@localhost identified by "";
第四步: 操作數(shù)據(jù)庫
登錄到mysql中,然后在mysql的提示符下運(yùn)行下列命令绽族,每個(gè)命令以分號(hào)結(jié)束
(上面的方法本人已驗(yàn)證確實(shí)可行姨涡,下面方法摘自網(wǎng)上)
MySql中添加用戶,新建數(shù)據(jù)庫,用戶授權(quán),刪除用戶,修改密碼(注意每行后邊都跟個(gè);表示一個(gè)命令語句結(jié)束):
1.新建用戶
1.1 登錄MYSQL:
@>mysql -u root -p
@>密碼
1.2 創(chuàng)建用戶:
mysql> insert into mysql.user(Host,User,Password) values("localhost","test",password("1234"));
這樣就創(chuàng)建了一個(gè)名為:test 密碼為:1234 的用戶吧慢。
注意:此處的"localhost"涛漂,是指該用戶只能在本地登錄,不能在另外一臺(tái)機(jī)器上遠(yuǎn)程登錄检诗。如果想遠(yuǎn)程登錄的話匈仗,將"localhost"改為"%",表示在任何一臺(tái)電腦上都可以登錄逢慌。也可以指定某臺(tái)機(jī)器可以遠(yuǎn)程登錄悠轩。
1.3 然后登錄一下:
mysql>exit;
@>mysql -u?test -p
@>輸入密碼
mysql>登錄成功
2.為用戶授權(quán)
授權(quán)格式:grant 權(quán)限 on 數(shù)據(jù)庫.* to 用戶名@登錄主機(jī) identified by "密碼";
2.1 登錄MYSQL(有ROOT權(quán)限),這里以ROOT身份登錄:
@>mysql -u root -p
@>密碼
2.2 首先為用戶創(chuàng)建一個(gè)數(shù)據(jù)庫(testDB):
mysql>create database testDB;
2.3 授權(quán)test用戶擁有testDB數(shù)據(jù)庫的所有權(quán)限(某個(gè)數(shù)據(jù)庫的所有權(quán)限):
mysql>grant all privileges on testDB.* to test@localhost identified by '1234';
? mysql>flush privileges;//刷新系統(tǒng)權(quán)限表
格式:grant?權(quán)限 on 數(shù)據(jù)庫.* to 用戶名@登錄主機(jī) identified by "密碼";
2.4 如果想指定部分權(quán)限給一用戶涕癣,可以這樣來寫:
mysql>grant select,update on testDB.* to test@localhost identified by '1234';
mysql>flush privileges; //刷新系統(tǒng)權(quán)限表
2.5 授權(quán)test用戶擁有所有數(shù)據(jù)庫的某些權(quán)限:? ?
mysql>grant select,delete,update,create,drop on *.* to test@"%" identified by "1234";
???? //test用戶對(duì)所有數(shù)據(jù)庫都有select,delete,update,create,drop 權(quán)限哗蜈。
//@"%" 表示對(duì)所有非本地主機(jī)授權(quán)前标,不包括localhost。(localhost地址設(shè)為127.0.0.1距潘,如果設(shè)為真實(shí)的本地地址炼列,不知道是否可以,沒有驗(yàn)證音比。)
//對(duì)localhost授權(quán):加上一句grant all privileges on testDB.* to test@localhost identified by '1234';即可俭尖。
3、刪除用戶
? @>mysql -u root -p
@>密碼
? mysql>Delete FROM user Where User='test' and Host='localhost';
? mysql>flush privileges;
? mysql>drop database testDB; //刪除用戶的數(shù)據(jù)庫
刪除賬戶及權(quán)限:>drop user 用戶名@'%';
>drop user 用戶名@?localhost;?
4. 修改指定用戶密碼
? @>mysql -u root -p
? @>密碼
?? mysql>update mysql.user set password=password('新密碼') where User="test" and Host="localhost";
? mysql>flush privileges;
5. 列出所有數(shù)據(jù)庫
mysql>show database;
6. 切換數(shù)據(jù)庫
mysql>use '數(shù)據(jù)庫名';
7. 列出所有表
mysql>show tables;
8. 顯示數(shù)據(jù)表結(jié)構(gòu)
mysql>describe 表名;
9. 刪除數(shù)據(jù)庫和數(shù)據(jù)表
mysql>drop database 數(shù)據(jù)庫名;
mysql>drop table 數(shù)據(jù)表名;