mysql 建立使用者及授權

2022-07-18 15:57:23 字數 2204 閱讀 4478

參考:

一, 建立使用者:

命令:create user 'username'@'host' identified by 'password';

說明:username - 你將建立的使用者名稱, host - 指定該使用者在哪個主機上可以登陸,如果是本地使用者可用

localhost, 如果想讓該使用者可以從任意遠端主機登陸,可以使用萬用字元%. password - 該使用者的登陸密碼,密碼可以為空,如果為空則

該使用者可以不需要密碼登陸伺服器.

例子: create user 'dog'@'localhost' identified by '123456';

create user 'pig'@'192.168.1.101_' idendified by '123456';

create user 'pig'@'%' identified by '123456';

create user 'pig'@'%' identified by '';

create user 'pig'@'%';

或://建立使用者

insert into mysql.user(host,user,password) values("localhost","phplamp",password("1234"));

二,授權:

命令:grant privileges on databasename.tablename to 'username'@'host'

說明: privileges - 使用者的操作許可權,如select , insert , update 等.如果要授予所的許可權則使用

all.;databasename - 資料庫名,tablename-表名,如果要授予該使用者對所有資料庫和表的相應操作許可權則可用*表

示, 如*.*.

例子: grant select, insert on test.user to 'pig'@'%';

grant all on *.* to 'pig'@'%';

注意:用以上命令授權的使用者不能給其它使用者授權,如果想讓該使用者可以授權,用以下命令:

grant privileges on databasename.tablename to 'username'@'host' with grant option;

重新整理系統許可權表

flush privileges;

三.設定與更改使用者密碼

命令:set password for 'username'@'host' = password('newpassword');如果是當前登陸使用者用set password = password("newpassword");

例子: set password for 'pig'@'%' = password("123456");

或:update mysql.user set password=password('新密碼') where user="phplamp" and host="localhost";

四.撤銷使用者許可權

命令: revoke privilege on databasename.tablename from 'username'@'host';

說明: privilege, databasename, tablename - 同授權部分.

例子: revoke select on *.* from 'pig'@'%';

注意: 假如你在給使用者'pig'@'%'授權的時候是這樣的(或類似

的):grant select on test.user to 'pig'@'%', 則在使用

revoke select on *.* from 'pig'@'%';命令並不能撤銷該使用者對test資料庫中user表的select 操作.相

反,如果授權使用的是grant select on *.* to 'pig'@'%';則

revoke select on test.user from 'pig'@'%';命令也不能撤銷該使用者對test資料庫中user表的

select 許可權.

具體資訊可以用命令show grants for 'pig'@'%'; 檢視.

五.刪除使用者

命令: drop user 'username'@'host';

或:delete from user where user="phplamp" and host="localhost";

//刪除使用者的資料庫

mysql>drop database phplampdb;

MySql建立使用者及授權

grant 語句的語法如下 grant privileges columns on what to user identifiedby password with grant option 對使用者授權 mysql grant rights on database.to user host iden...

mysql建立使用者及授權

grant 語句的語法如下 grant privileges columns on what to user identifiedby password with grant option 對使用者授權 mysql grant rights on database.to user host iden...

mysql 建立使用者及授權

建立使用者 create user username hostidentified by password username 建立的使用者名稱 host 指定主機,localhost代表本地主機,代表所有主機,其他使用ip password 密碼 授權 grantpermissionsondatab...