mysql新增root賬戶 並授權其他賬戶
方法1: update mysql.user set authentication_string=password('xxx') where user='root' and Host = 'localhost'; 方法2: mysql> alter user 'root'@'localhost' identified by '123'; 方法3: mysql> set password for 'root'@'localhost'=password('123');
重新整理許可權 mysql> flush privileges;
root有grant許可權但是無法進行授權
show grants for root;
#1, 如果不是顯示的 all privileges grant option ,則授權不能成功.
#2, 通過檢視,root的哪個許可權不是 Y use mydql; //切換資料庫
select * from user where user='root';
#3, 把所有不是Y的許可權項,改為Y update user set priv='Y' where user='root';
#4 改完之後檢視root,是否擁有所有許可權 all privileges ,並且有授權的許可權 grant option show grants for root;
使用者許可權管理主要有以下作用:
1. 可以限制使用者訪問哪些庫、哪些表
2. 可以限制使用者對哪些表執行SELECT、CREATE、DELETE、DELETE、ALTER等操作
3. 可以限制使用者登入的IP或域名
4. 可以限制使用者自己的許可權是否可以授權給別的使用者
一、使用者授權
mysql> grant all privileges on *.* to 'yangxin'@'%' identified by 'yangxin123456' with grant option;
新增許可權(和已有許可權合併,不會覆蓋已有許可權)
GRANT Insert ON `your database`.* TO `user`@`host`;
刪除許可權
REVOKE Delete ON `your database`.* FROM `user`@`host`;
all privileges:表示將所有許可權授予給使用者。也可指定具體的許可權,如:SELECT、CREATE、DROP等。
on:表示這些許可權對哪些資料庫和表生效,格式:資料庫名.表名,這裡寫“*”表示所有資料庫,所有表。如果我要指定將許可權應用到test庫的user表中,可以這麼寫:test.user
to:將許可權授予哪個使用者。格式:”使用者名稱”@”登入IP或域名”。%表示沒有限制,在任何主機都可以登入。比如:”yangxin”@”192.168.0.%”,表示yangxin這個使用者只能在192.168.0IP段登入
identified by:指定使用者的登入密碼
with grant option:表示允許使用者將自己的許可權授權給其它使用者
可以使用GRANT給使用者新增許可權,許可權會自動疊加,不會覆蓋之前授予的許可權,比如你先給使用者新增一個SELECT許可權,後來又給使用者添加了一個INSERT許可權,那麼該使用者就同時擁有了SELECT和INSERT許可權。
使用者詳情的許可權列表請參考MySQL官網說明:http://dev.mysql.com/doc/refman/5.7/en/privileges-provided.html
二、重新整理許可權
對使用者做了許可權變更之後,一定記得重新載入一下許可權,將許可權資訊從記憶體中寫入資料庫。
mysql> flush privileges;
三、檢視使用者許可權
mysql> grant select,create,drop,update,alter on *.* to 'yangxin'@'localhost' identified by 'yangxin0917' with grant option;
mysql> show grants for 'yangxin'@'localhost';
四、回收許可權
刪除yangxin這個使用者的create許可權,該使用者將不能建立資料庫和表。
mysql> revoke create on *.* from 'yangxin@localhost';
mysql> flush privileges;
五、刪除使用者
mysql> select host,user from user;
+---------------+---------+
| host | user |
+---------------+---------+
| % | root |
| % | test3 |
| % | yx |
| 192.168.0.% | root |
| 192.168.0.% | test2 |
| 192.168.0.109 | test |
| ::1 | yangxin |
| localhost | yangxin |
+---------------+---------+
8 rows in set (0.00 sec)
mysql> drop user 'yangxin'@'localhost';
六、使用者重新命名
shell> rename user 'test3'@'%' to 'test1'@'%';
七、修改密碼
1> 更新mysql.user表
mysql> use mysql;
# mysql5.7之前
mysql> update user set password=password('123456') where user='root';
# mysql5.7之後
mysql> update user set authentication_string=password('123456') where user='root';
mysql> flush privileges;
2> 用set password命令
語法:set password for ‘使用者名稱’@’登入地址’=password(‘密碼’)
mysql> set password for 'root'@'localhost'=password('123456');
3> mysqladmin
語法:mysqladmin -u使用者名稱 -p舊的密碼 password 新密碼
mysql> mysqladmin -uroot -p123456 password 1234abcd
注意:mysqladmin位於mysql安裝目錄的bin目錄下
八、忘記密碼
1> 新增登入跳過許可權檢查配置
修改my.cnf,在mysqld配置節點新增skip-grant-tables配置
[mysqld]
skip-grant-tables
2> 重新啟動mysql服務
shell> service mysqld restart
3> 修改密碼
此時在終端用mysql命令登入時不需要使用者密碼,然後按照修改密碼的第一種方式將密碼修改即可。
注意:mysql庫的user表,5.7以下版本密碼欄位為password,5.7以上版本密碼欄位為authentication_string
4> 還原登入許可權跳過檢查配置
將my.cnf中mysqld節點的skip-grant-tables配置刪除,然後重新啟動服務即可。
Mysql 有多個個許可權?經常記不住,今天總結一下,看後都能牢牢的記在心裡啦!!
很明顯總共28個許可權:下面是具體的許可權介紹:轉載的,記錄一下:
一.許可權表
mysql資料庫中的3個許可權表:user、db、host
許可權表的存取過程是:
1)先從user表中的host、user、password這3個欄位中判斷連線的IP、使用者名稱、密碼是否存在表中,存在則通過身份驗證;
2)通過許可權驗證,進行許可權分配時,按照useràdbàtables_privàcolumns_priv的順序進行分配。即先檢查全域性許可權表user,如果user中對應的許可權為Y,則此使用者對所有資料庫的許可權都為Y,將不再檢查db,tables_priv,columns_priv;如果為N,則到db表中檢查此使用者對應的具體資料庫,並得到db中為Y的許可權;如果db中為N,則檢查tables_priv中此資料庫對應的具體表,取得表中的許可權Y,以此類推。
二.MySQL各種許可權(共27個)
(以下操作都是以root身份登陸進行grant授權,以p1@localhost身份登陸執行各種命令。)
1.usage
連線(登陸)許可權,建立一個使用者,就會自動授予其usage許可權(預設授予)。
mysql>grantusageon*.*to‘p1′@’localhost’identifiedby‘123′;
該許可權只能用於資料庫登陸,不能執行任何操作;且usage許可權不能被回收,也即REVOKE使用者並不能刪除使用者。
2.select
必須有select的許可權,才可以使用selecttable
mysql>grantselectonpyt.*to‘p1′@’localhost’;
mysql>select*fromshop;
3.create
必須有create的許可權,才可以使用createtable
mysql>grantcreateonpyt.*to‘p1′@’localhost’;
4.createroutine
必須具有createroutine的許可權,才可以使用{create|alter|drop}{procedure|function}
mysql>grantcreateroutineonpyt.*to‘p1′@’localhost’;
當授予createroutine時,自動授予EXECUTE,ALTERROUTINE許可權給它的建立者:
mysql>showgrantsfor‘p1′@’localhost’;
+—————————————————————————+
Grantsforp1@localhost
+————————————————————————–+
|GRANTUSAGEON*.*TO‘p1′@’localhost’IDENTIFIEDBYPASSWORD‘*23AE809DDACAF96AF0FD78ED04B6A265E05AA257′|
|GRANTSELECT,CREATE,CREATEROUTINEON`pyt`.*TO‘p1′@’localhost’|
|GRANTEXECUTE,ALTERROUTINEONPROCEDURE`pyt`.`pro_shop1`TO‘p1′@’localhost’|
+————————————————————————————-+
5.createtemporarytables(注意這裡是tables,不是table)
必須有createtemporarytables的許可權,才可以使用createtemporarytables.
mysql>grantcreatetemporarytablesonpyt.*to‘p1′@’localhost’;
[mysql@mydev~]$mysql-hlocalhost-up1-ppyt
mysql>createtemporarytablett1(idint);
6.createview
必須有createview的許可權,才可以使用createview
mysql>grantcreateviewonpyt.*to‘p1′@’localhost’;
mysql>createviewv_shopasselectpricefromshop;
7.createuser
要使用CREATEUSER,必須擁有mysql資料庫的全域性CREATEUSER許可權,或擁有INSERT許可權。
mysql>grantcreateuseron*.*to‘p1′@’localhost’;
或:mysql>grantinserton*.*top1@localhost;
8.insert
必須有insert的許可權,才可以使用insertinto…..values….
9.alter
必須有alter的許可權,才可以使用altertable
altertableshopmodifydealerchar(15);
10.alterroutine
必須具有alterroutine的許可權,才可以使用{alter|drop}{procedure|function}
mysql>grantalterroutineonpyt.*to‘p1′@’localhost‘;
mysql>dropprocedurepro_shop;
QueryOK,0rowsaffected(0.00sec)
mysql>revokealterroutineonpyt.*from‘p1′@’localhost’;
[mysql@mydev~]$mysql-hlocalhost-up1-ppyt
mysql>dropprocedurepro_shop;
ERROR1370(42000):alterroutinecommanddeniedtouser‘p1′@’localhost’forroutine‘pyt.pro_shop’
11.update
必須有update的許可權,才可以使用updatetable
mysql>updateshopsetprice=3.5wherearticle=0001anddealer=’A';
12.delete
必須有delete的許可權,才可以使用deletefrom….where….(刪除表中的記錄)
13.drop
必須有drop的許可權,才可以使用dropdatabasedb_name;droptabletab_name;
dropviewvi_name;dropindexin_name;
14.showdatabase
通過showdatabase只能看到你擁有的某些許可權的資料庫,除非你擁有全域性SHOWDATABASES許可權。
對於p1@localhost使用者來說,沒有對mysql資料庫的許可權,所以以此身份登陸查詢時,無法看到mysql資料庫:
mysql>showdatabases;
+——————–+
|Database|
+——————–+
|information_schema|
|pyt|
|test|
+——————–+
15.showview
必須擁有showview許可權,才能執行showcreateview。
mysql>grantshowviewonpyt.*top1@localhost;
mysql>showcreateviewv_shop;
16.index
必須擁有index許可權,才能執行[create|drop]index
mysql>grantindexonpyt.*top1@localhost;
mysql>createindexix_shoponshop(article);
mysql>dropindexix_shoponshop;
17.excute
執行存在的Functions,Procedures
mysql>callpro_shop1(0001,@a);
+———+
|article|
+———+
|0001|
|0001|
+———+
mysql>select@a;
+——+
|@a|
+——+
|2|
+——+
18.locktables
必須擁有locktables許可權,才可以使用locktables
mysql>grantlocktablesonpyt.*top1@localhost;
mysql>locktablesa1read;
mysql>unlocktables;
19.references
有了REFERENCES許可權,使用者就可以將其它表的一個欄位作為某一個表的外來鍵約束。
20.reload
必須擁有reload許可權,才可以執行flush[tables|logs|privileges]
mysql>grantreloadonpyt.*top1@localhost;
ERROR1221(HY000):IncorrectusageofDBGRANTandGLOBALPRIVILEGES
mysql>grantreloadon*.*to‘p1′@’localhost’;
QueryOK,0rowsaffected(0.00sec)
mysql>flushtables;
21.replicationclient
擁有此許可權可以查詢masterserver、slaveserver狀態。
mysql>showmasterstatus;
ERROR1227(42000):Accessdenied;youneedtheSUPER,REPLICATIONCLIENTprivilegeforthisoperation
mysql>grantReplicationclienton*.*top1@localhost;
或:mysql>grantsuperon*.*top1@localhost;
mysql>showmasterstatus;
+——————+———-+————–+——————+
|File|Position|Binlog_Do_DB|Binlog_Ignore_DB|
+——————+———-+————–+——————+
|mysql-bin.000006|2111|||
+——————+———-+————–+——————+
mysql>showslavestatus;
22.replicationslave
擁有此許可權可以檢視從伺服器,從主伺服器讀取二進位制日誌。
mysql>showslavehosts;
ERROR1227(42000):Accessdenied;youneedtheREPLICATIONSLAVEprivilegeforthisoperation
mysql>showbinlogevents;
ERROR1227(42000):Accessdenied;youneedtheREPLICATIONSLAVEprivilegeforthisoperation
mysql>grantreplicationslaveon*.*top1@localhost;
mysql>showslavehosts;
Emptyset(0.00sec)
mysql>showbinlogevents;
+—————+——-+—————-+———–+————-+————–+
|Log_name|Pos|Event_type|Server_id|End_log_pos|Info|
+—————+——-+————–+———–+————-+—————+
|mysql-bin.000005|4|Format_desc|1|98|Serverver:5.0.77-log,Binlogver:4||mysql-bin.000005|98|Query|1|197|use`mysql`;createtablea1(iint)engine=myisam|
……………………………………
23.Shutdown
關閉MySQL:
[mysql@mydev~]$mysqladminshutdown
重新連線:
[mysql@mydev~]$mysql
ERROR2002(HY000):Can’tconnecttolocalMySQLserverthroughsocket‘/tmp/mysql.sock’(2)
[mysql@mydev~]$cd/u01/mysql/bin
[mysql@mydevbin]$./mysqld_safe&
[mysql@mydevbin]$mysql
24.grantoption
擁有grantoption,就可以將自己擁有的許可權授予其他使用者(僅限於自己已經擁有的許可權)
mysql>grantGrantoptiononpyt.*top1@localhost;
mysql>grantselectonpyt.*top2@localhost;
25.file
擁有file許可權才可以執行select..intooutfile和loaddatainfile…操作,但是不要把file,process,super許可權授予管理員以外的賬號,這樣存在嚴重的安全隱患。
mysql>grantfileon*.*top1@localhost;
mysql>loaddatainfile‘/home/mysql/pet.txt’intotablepet;
26.super
這個許可權允許使用者終止任何查詢;修改全域性變數的SET語句;使用CHANGEMASTER,PURGEMASTERLOGS。
mysql>grantsuperon*.*top1@localhost;
mysql>purgemasterlogsbefore‘mysql-bin.000006′;
27.process
通過這個許可權,使用者可以執行SHOWPROCESSLIST和KILL命令。預設情況下,每個使用者都可以執行SHOWPROCESSLIST命令,但是隻能查詢本使用者的程序。
mysql>showprocesslist;
+—-+——+———–+——+———+——+——-+——————+
|Id|User|Host|db|Command|Time|State|Info|
+—-+——+———–+——+———+——+——-+——————+
|12|p1|localhost|pyt|Query|0|NULL|showprocesslist|
+—-+——+———–+——+———+——+——-+——————+
另外,
管理許可權(如super,process,file等)不能夠指定某個資料庫,on後面必須跟*.*
mysql>grantsuperonpyt.*top1@localhost;
ERROR1221(HY000):IncorrectusageofDBGRANTandGLOBALPRIVILEGES
mysql>grantsuperon*.*top1@localhost;
QueryOK,0rowsaffected(0.01sec)
---------------------
作者:anzhen0429
來源:CSDN
原文:https://blog.csdn.net/anzhen0429/article/details/78296814
版權宣告:本文為博主原創文章,轉載請附上博文連結!