【Mysql】常用指令之——使用者操作(建立,授權,修改,刪除),mysql使用者操作

來源:互聯網
上載者:User

【Mysql】常用指令之——使用者操作(建立,授權,修改,刪除),mysql使用者操作

Mysql中的使用者 user 每一個user都對應了不同的使用者地址和許可權


建立Mysql使用者共有三種方式1、create user 2、grant 3、操作mysql.user表

1、CREATE USER 'username'@'host' IDENTIFIED BY 'password';

例子: CREATE USER 'aa'@'localhost' IDENTIFIED BY '123456';

CREATE USER 'aa'@'192.168.1.101_' IDENDIFIED BY '123456';

CREATE USER 'aa'@'%' IDENTIFIED BY '123456';

CREATE USER 'bb'@'%' IDENTIFIED BY '';

CREATE USER 'cc'@'%';


使用者有兩個部分組成 格式:名字@主機

aa@localhost   本機發起連結的aa使用者

bb@152.236.20.10  用戶端地址為152.236.20.10的使用者bb

cc@%              %萬用字元,表示所有


2、使用grant語句(授權方式)

文法:mysql> grant 許可權1,許可權2,...許可權n on 資料庫名稱.表名稱 to 使用者名稱@使用者地址 identified by '串連口令';

許可權1,許可權2,...許可權n代表

select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14個許可權

執行個體:

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分配可對資料庫vtdc的employee表進行select,insert,update,delete,create,drop等操作的許可權,並設定口令為123。

mysql>grant all privileges on vtdc.* to joe@10.163.225.87 identified by '123';

給來自10.163.225.87的使用者joe分配可對資料庫vtdc所有表進行所有操作的許可權,並設定口令為123。

mysql>grant all privileges on *.* to joe@10.163.225.87 identified by '123';

給來自10.163.225.87的使用者joe分配可對所有資料庫的所有表進行所有操作的許可權,並設定口令為123。

mysql>grant all privileges on *.* to joe@localhost identified by '123';

給本機使用者joe分配可對所有資料庫的所有表進行所有操作的許可權,並設定口令為123。


3、直接向mysql.user表插入記錄:

mysql> insert into user (host,user,password) values ('%','jss_insert',password('jss'));

mysql>flush privileges; //重新整理系統許可權表



修改使用者密碼:1、mysqladmin  2、修改mysql.user表  3、set password

1、 使用mysqladmin文法:mysqladmin -u使用者名稱 -p舊密碼 password 新密碼

例如:mysqladmin -u root -p 123 password 456;

2、 直接修改user表的使用者口令:

文法:update mysql.user set password=password('新密碼') where User="phplamp" and Host="localhost";

執行個體:update user set password=password('54netseek') where user='root';

flush privileges;

3、使用SET PASSWORD語句修改密碼:文法:

SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');

如果是當前登陸使用者用SET PASSWORD = PASSWORD("newpassword");

執行個體:

set password for root@localhost=password('');

SET PASSWORD FOR name=PASSWORD('new password');

SET PASSWORD FOR 'pig'@'%' = PASSWORD("123456");



刪除使用者和撤銷許可權:1、drop user  2、取消授權使用者  3、刪除mysql.user表中的記錄

1、 取消一個賬戶和其許可權

Drop USER user;

drop user username@'%'

drop user username@localhost

2、 取消授權使用者:

文法:REVOKE privilege ON databasename.tablename FROM 'username'@'host';

例子: REVOKE SELECT ON *.* FROM 'pig'@'%';

REVOKE SELECT ON test.user FROM 'pig'@'%';

revoke all on *.* from sss@localhost ;

revoke all on user.* from 'admin'@'%';

SHOW GRANTS FOR 'pig'@'%'; //查看授權

3、刪除使用者:

文法: Delete from user where user = "user_name" and host = "host_name" ;

例子:delete from user where user='sss' and host='localhost';



參考博文:http://blog.csdn.net/leili0806/article/details/8573636



MySQL怎授權一個自己的建立的使用者比如daitest建立新資料庫的權利?命令

慢慢看吧
mysql中可以給你一個使用者授予如select,insert,update,delete等其中的一個或者多個許可權,主要使用grant命令,用法格式為:
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......餘下全文>>
 
mysql建立使用者問題

增加新使用者。
格式:grant select on 資料庫.* to 使用者名稱@登入主機 identified by “密碼”
1、增加一個使用者test1密碼為abc,讓他可以在任何主機上登入,並對所有資料庫有查詢、插入、修改、刪除的許可權。首先用root使用者連入MYSQL,然後鍵入以下命令:
grant select,insert,update,delete on *.* to [email=test1@”%]test1@”%[/email]” Identified by “abc”;
但增加的使用者是十分危險的,你想如某個人知道test1的密碼,那麼他就可以在internet上的任何一台電腦上登入你的mysql資料庫並對你的資料可以為所欲為了,解決辦法見2。
2、增加一個使用者test2密碼為abc,讓他只可以在localhost上登入,並可以對資料庫mydb進行查詢、插入、修改、刪除的操作(localhost指本地主機,即MYSQL資料庫所在的那台主機),
這樣使用者即使用知道test2的密碼,他也無法從internet上直接存取資料庫,只能通過MYSQL主機上的web頁來訪問了。
grant select,insert,update,delete on mydb.* to [email=test2@localhost]test2@localhost[/email] identified by “abc”;
如果你不想test2有密碼,可以再打一個命令將密碼消掉。
grant select,insert,update,delete on mydb.* to [email=test2@localhost]test2@localhost[/email] identified by “”;
下篇我是MYSQL中有關資料庫方面的操作。注意:你必須首先登入到MYSQL中,以下操作都是在MYSQL的提示符下進行的,而且每個命令以分號結束。
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.