提示錯誤:“1045-Access denied for user root@...”
解決辦法如下,執行命令:
mysql> use mysql;
mysql> selecthost,user from user;
查看結果是不是root用戶僅允許本地(localhost)登錄,下面這個截圖就是這種情況.
是的話,就要修改它的host為%,表示任意IP地址都可以登錄.
update user set host = '%' where user = 'root';
執行完后可能提示error.再mysql> select host,user from user;查看下吧.
root對應的host成了%,表示可以任意IP地址登錄了.
mysql> flushprivileges;
把緩存flush掉.在使用update語句修改用戶記錄后,需要FLUSH語句告訴服務器重載授權表.
<==============
例子3
增加一個用戶custom,他能從主機localhost、server.domain和whitehouse.gov連接。他 只想要從 localhost存取bankaccount數據庫,從whitehouse.gov存取expenses數據庫和從所有3台主機存取customer 數據庫。他想要從所有3台主機上使用口令stupid。
為了使用GRANT語句設置個用戶的權限,運行這些命令:
shell> mysql --user=root mysql
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
ON bankaccount.* TO custom@localhost IDENTIFIED BY 'stupid';
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
ON expenses.* TO custom@whitehouse.gov IDENTIFIED BY 'stupid';
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
ON customer.* TO custom@'%' IDENTIFIED BY 'stupid';
==============================================
權限信息用user、db、host、tables_priv和columns_priv表被存儲在mysql數據庫中(即在名為mysql的數據庫中)。
權限 列 Context
select Select_priv 表
insert Insert_priv 表
update Update_priv 表
delete Delete_priv 表
index Index_priv 表
alter Alter_priv 表
create Create_priv 數據庫、表或索引
drop Drop_priv 數據庫或表
grant Grant_priv 數據庫或表
references References_priv 數據庫或表
reload Reload_priv 服務器管理
shutdown Shutdown_priv 服務器管理
process Process_priv 服務器管理
file File_priv 在服務器上的文件存取
1.select、insert、update和delete權限 允許你在一個數據庫現有的表上實施操作,是基本權限
2.alter權限允許你使用ALTER TABLE
3.create和drop權限允許你創建新的數據庫和表,或拋棄(刪除)現存的數據庫和表 如果你將mysql數據庫的drop權限授予一個用戶,該用戶能拋棄存儲了MySQL存取權限的數據庫!
4.grant權限允許你把你自己擁有的那些權限授給其他的用戶。
你不能明顯地指定一個給定用戶應該被拒絕存取。即,你不能明顯地匹配一個用戶並且然后拒絕連接。你不能指定一個用戶有權創建立或拋棄一個數據庫中的表,也不能創建或拋棄數據庫本身。 可以同時列出許多被授予的單個權限。
例如,如果想讓用戶能讀取和修改已有表的內容,但又不允許創建新表或刪除表,可按如下授權:
GRANT SELECT,INSERT,DELETE,UPDATE ON samp_db.* TO 'user'@'%' IDENTIFIEDBY "pass"
以上是我從別的地方拷貝過來后稍作修改的文字,下面自己寫一些需要注意的東西。
為什么使用了Grant all on db.* to user identified by "pass"后,在主機上訪問數據庫還會出現ERROR 1045 (28000): Access denied for user 'user'@'localhost' (using password: YES) 的錯誤提示?
解答方法如下:運行命令 Grant all on db.* to 'user'@'localhost' identified by "pass"
原因是:當不加@選項時,效果與加@'%'是一樣的,'%'從名義上包括任何主機,(%必須加上引號,不然與@放在一起可能不會被辨認出。)不過有些時候(有些版本)'%'不包括localhost,要單獨對@'localhost'進行賦值
<==============
mysql 創建一個用戶 hail,密碼 hail,指定一個數據庫 haildb 給 hail
mysql -u root -p
password
use mysql;
insert into user(host,user,password) values('localhost','hail',password('hail'));
flush privileges;
create database haildb;
grant all privileges on haildb.* to hail@localhost identified by 'hail';
flush privileges;
如果想指定部分權限給用戶
grant select,update on haildb.* to hail@localhost identified by 'hail';
flush privileges;
刪除用戶
delete from user where user='hail' and host='localhost';
flush privileges;
刪除用戶數據庫
drop database haildb;
修改指定用戶密碼
update user set password=password('new_password') where user='hail' and host='localhost';
flush privileges;
1.遠程登錄mysql
mysql -h ip -u root -p 密碼
2.創建用戶
格式:grant 權限 on 數據庫.* to 用戶名@登錄主機 identified by “密碼”;
例1:增加一個test1用戶,密碼為123456,可以在任何主機上登錄,並對所有數據庫有查詢,增加,修改和刪除的功能。需要在mysql的root用戶下進行
mysql>grant select,insert,update,delete on *.* to test1@”%” identified by “123456″;
mysql>flush privileges;
例2:增加一個test2用戶,密碼為123456,只能在192.168.2.12上登錄,並對數據庫student有查詢,增加,修改和刪除的功能。需要在mysql的root用戶下進行
mysql>grant select,insert,update,delete on student.* to test2@192.168.2.12 identified by “123456″;
mysql>flush privileges;
例3:授權用戶test3擁有數據庫student的所有權限
mysql>grant all privileges on student.* to test3@localhost identified by ’123456′;
mysql>flush privileges;
3.修改用戶密碼
mysql>update mysql.user set password=password(’123456′) where User=’test1′ and Host=’localhost’;
mysql>flush privileges;
4.刪除用戶
mysql>delete from user where user=’test2′ and host=’localhost’;
mysql>flush privileges;
5.刪除數據庫和刪除表
mysql>drop database 數據庫名;
mysql>drop table 表名;
6.刪除賬戶及權限
drop user 用戶名@’%’
drop user 用戶名@localhost
**************************************************************************************
grant 詳細解析如下:
**************************************************************************************
MySQL 賦予用戶權限命令的簡單格式可概括為:
grant 權限 on 數據庫對象 to 用戶
一、grant 普通數據用戶,查詢、插入、更新、刪除 數據庫中所有表數據的權利。
grant select on testdb.* to common_user@’%’
grant insert on testdb.* to common_user@’%’
grant update on testdb.* to common_user@’%’
grant delete on testdb.* to common_user@’%’
或者,用一條 MySQL 命令來替代:
grant select, insert, update, delete on testdb.* to common_user@’%’
二、grant 數據庫開發人員,創建表、索引、視圖、存儲過程、函數。。。等權限。
grant 創建、修改、刪除 MySQL 數據表結構權限。
grant create on testdb.* to developer@’192.168.0.%’;
grant alter on testdb.* to developer@’192.168.0.%’;
grant drop on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 外鍵權限。
grant references on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 臨時表權限。
grant create temporary tables on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 索引權限。
grant index on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 視圖、查看視圖源代碼 權限。
grant create view on testdb.* to developer@’192.168.0.%’;
grant show view on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 存儲過程、函數 權限。
grant create routine on testdb.* to developer@’192.168.0.%’; — now, can show procedure status
grant alter routine on testdb.* to developer@’192.168.0.%’; — now, you can drop a procedure
grant execute on testdb.* to developer@’192.168.0.%’;
三、grant 普通 DBA 管理某個 MySQL 數據庫的權限。
grant all privileges on testdb to dba@’localhost’
其中,關鍵字 “privileges” 可以省略。
四、grant 高級 DBA 管理 MySQL 中所有數據庫的權限。
grant all on *.* to dba@’localhost’
五、MySQL grant 權限,分別可以作用在多個層次上。
1. grant 作用在整個 MySQL 服務器上:
grant select on *.* to dba@localhost; — dba 可以查詢 MySQL 中所有數據庫中的表。
grant all on *.* to dba@localhost; — dba 可以管理 MySQL 中的所有數據庫
2. grant 作用在單個數據庫上:
grant select on testdb.* to dba@localhost; — dba 可以查詢 testdb 中的表。
3. grant 作用在單個數據表上:
grant select, insert, update, delete on testdb.orders to dba@localhost;
4. grant 作用在表中的列上:
grant select(id, se, rank) on testdb.apache_log to dba@localhost;
5. grant 作用在存儲過程、函數上:
grant execute on procedure testdb.pr_add to ‘dba’@’localhost’
grant execute on function testdb.fn_add to ‘dba’@’localhost’
六、查看 MySQL 用戶權限
查看當前用戶(自己)權限:
show grants;
查看其他 MySQL 用戶權限:
show grants for dba@localhost;
七、撤銷已經賦予給 MySQL 用戶權限的權限。
revoke 跟 grant 的語法差不多,只需要把關鍵字 “to” 換成 “from” 即可:
grant all on *.* to dba@localhost;
revoke all on *.* from dba@localhost;
八、MySQL grant、revoke 用戶權限注意事項
1. grant, revoke 用戶權限后,該用戶只有重新連接 MySQL 數據庫,權限才能生效。
2. 如果想讓授權的用戶,也可以將這些權限 grant 給其他用戶,需要選項 “grant option“
grant select on testdb.* to dba@localhost with grant option;
這個特性一般用不到。實際中,數據庫權限最好由 DBA 來統一管理。
Category: Post
You can follow any responses to this entry via RSS.
Comments are currently closed, but you can trackback from your own site.
=========================================================================
1.創建用戶並授權
grant語句的語法:
grant privileges (columns) on what to user identified by “password” with grant option
要使用該句型,需確定字段有:
privileges 權限指定符權限允許的操作
alter 修改表和索引
create 創建數據庫和表
delete 刪除表中已有的記錄
drop 拋棄(刪除)數據庫和表
index 創建或拋棄索引
insert 向表中插入新行
reference 未用
select 檢索表中的記錄
update 修改現存表記錄
file 讀或寫服務器上的文件
process 查看服務器中執行的線程信息或殺死線程
reload 重載授權表或清空日志、主機緩存或表緩存。
shutdown 關閉服務器
all 所有;
all privileges同義詞
usage 特殊的“無權限”權限
以上權限分三組:
第一組:適用於數據庫、表和列如:alter create delete drop index insert select update
第二組:數管理權限 它們允許用戶影響服務器的操作 需嚴格地授權 如:file process reload shut*
第三組:權限特殊 all意味着“所有權限” uasge意味着無權限,即創建用戶,但不授予權限
columns
權限運用的列(可選)並且你只能設置列特定的權限。如果命令有多於一個列,應該用逗號分開它們。
what
權限運用的級別。權限可以是全局,定數據庫或特定表.
user
權限授予的用戶,由一個用戶名和主機名組成,許兩個同名用戶從不同地方連接.缺省:mysql用戶password
賦予用戶的口令(可選),如果你對用戶沒有指定identified by子句,該用戶口令不變.
用identified by時,口令字符串用改用口令的字面含義,grant將為你編碼口令.
注:set password使用password()函數
with grant option
用戶可以授予權限通過grant語句授權給其它用戶(可選)
實例講解:
grant all on db_book.* to huaying@koowo.com identified by “yeelion” 只能在本地連接
grant all on db_book.* to huaying@vpn.koowo.com identified by “yeeliong” 允許從此域連接
grant all on db_book.* to huaying@% identified by “yeelion” 允許從任何主機連接 注:”%”字符起通配符作用,與like模式匹配的含義相同。
grant all on db_book.* to huaying@%.koowo.com identified by “yeelion”; 允許huaying從koowo.com域的任何主機連接
grant all on db_book.* to huaying@192.168.1.189 identified by “yeelion”
grant all on db_book.* to huaying@192.168.1.% identified by “yeelion”
grant all on db_book.* to huaying@192.168.1.0/17 identified by “yeelion”
允許從單IP 段IP或一子網IP登陸
注:有時 用戶@IP 需用引號 如”huaying@192.168.1.0/17″
grant all on *.* to huaying@localhost identified by “yeelion” with grant option
添加超級用戶huaying 可在本地登陸做任何操作.
grant reload on *.* to huaying@localhost identified by “yeelion” 只賦予reload權限
grant all on db_book to huaying@koowo.com indetified by “yeelion” 所有權限
grant select on db_book to huaying@% indetified by “yeelion” 只讀權限
grant select,insert,delete,update on db_book to huaying@koowo.com indetified by “yeelion”
只有select,insert,delete,update的權限
grant select on db_book.storybook to huaying@localhost indetified by “yeelion” 只對表
grant update (name) on db_book.storybook to huaying@localhost 只對表的name列 密碼不變
grant update (id,name,author) on db_book.storybook to huaying@localhost 只對表的多列
grant all on book.* to “”@koowo.com 允許koowo.com域中的所有用戶使用庫book
grant all on book.* to huaying@%.koowo.com indetified by “yeelion” with grant option
允許huaying對庫book所有表的管理員授權.
2.撤權並刪除用戶
revoke的語法類似於grant語句
to用from取代,沒有indetifed by和with grant option子句. 如下:
revoke privileges (columns) on what from user
user:必須匹配原來grant語句的你想撤權的用戶的user部分。
privileges:不需匹配,可以用grant語句授權,然后用revoke語句只撤銷部分權限。
revoke語句只刪權限不刪用戶,撤銷了所有權限后user表中用戶記錄保留,用戶仍然可以連接服務器.
要完全刪除一個用戶必須用一條delete語句明確從user表中刪除用戶記錄:
delete from user where user=”huaying”
flush privileges; 重載授權表
注:使用grant和revoke語句時,表自動重載,而你直接修改授權表時不是.
實例:
1.創建數據庫
CREATE DATABASE `fypay` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
2.為創建的數據庫增加用戶fypay
grant create,select,insert,update,delete,drop,alter on fypay.* to fypay@”%” identified by “testfpay”;
3.刪除fypay用戶
delete from user where user=”fypay”
drop user fypay@localhost
4.刷新數據庫
flush privileges;