mysql建立使用者刪除許可權 使用者建立 許可權 刪除

2021-10-19 19:13:12 字數 2296 閱讀 7586

# 使用者建立、許可權、刪除操作

## 連線mysql操作

> **mysql -h 主機位址 -u 使用者名稱 -p 使用者密碼**

> 注:-u與root可以不用加空格,其它引數也一樣。

### demo

開啟電腦cmd,輸入

mysql -h 127.0.0.1 -u root -p

回車,然後輸入密碼。就可以連線到本地的mysql資料庫。

!(## 建立mysql使用者

> **create user 'username'@'host' identified by 'password';**

**說明**

1. username - 你將建立的使用者名稱,

2. host - 指定該使用者在哪個主機上可以登陸,如果是本地使用者可用localhost, 如果想讓該使用者可以從任意遠端主機登陸,可以使用萬用字元%.

3. password - 該使用者的登陸密碼,密碼可以為空,如果為空則該使用者可以不需要密碼登陸伺服器。

### demo

create user 'test'@'localhost' identified by '123456';

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

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

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

create user 'test'@'%';

!(## 授權使用者操作許可權

> **grant privileges on databasename.tablename to 'username'@'host';**

1. privileges - 使用者的操作許可權,如select , insert , update 等(詳細列表見該文最後面).如果要授予所的許可權則使用all.;

2. databasename - 資料庫名;

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

### demo

grant select, insert on school.* to 'test' @'%';

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

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

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

## 撤銷使用者許可權

> **revoke privilege on databasename.tablename from 'username'@'host';**

1. privileges - 使用者的操作許可權,如select , insert , update 等(詳細列表見該文最後面).如果要授予所的許可權則使用all.;

2. databasename - 資料庫名;

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

### demo

revoke select on *.* from 'test'@'%';

> 假如你在給使用者'test'@'%'授權的時候是這樣的(或類似的):`grant select on test.user to 'test'@'%'`, 則在使用`revoke select on *.* from 'test'@'%'`;命令並不能撤銷該使用者對test資料庫中user表的select 操作;

> 相反,如果授權使用的是`grant select on *.* to 'test'@'%'`;則`revoke select on test.user from 'test'@'%'`;命令也不能撤銷該使用者對test資料庫中user表的select許可權。

另外 具體資訊可以用命令`show grants for 'test'@'%';`檢視。

## 設定與更改使用者密碼

> **set password for 'username'@'host' = password('newpassword');**

> **set password = password("newpassword");** # 如果是當前登陸使用者

### demo

set password for 'test'@'%' = password("aaaaaa");

!(## 刪除使用者

>**drop user 'username'@'host';**

mysql 給使用者賦權和刪除許可權

mysql通過 grant 授予許可權 revoke 撤銷許可權 授予使用者許可權 grant all privileges on 層級 to 使用者名稱 主機 identified by 密碼 授予wang使用者全域性級全部許可權 grant all privileges on to wang i...

mysql 建立使用者, 分配許可權, 刪除使用者

通過create user 命令來建立使用者,有兩種方式 只介紹通過 create user 命令,直接往user表中插入資料的方式,這裡就不說了 建立使用者的同時,指定使用者可登入的主機和密碼create user test user identified by 123 create user t...

Mysql建立 刪除使用者,更改使用者許可權命令

mysql中新增使用者,新建資料庫,使用者授權,刪除使用者,修改密碼 注意每行後邊都跟個 表示乙個命令語句結束 1.新建使用者 mysql u root p 密碼 mysql insert into mysql.user host,user,password values localhost tes...