# Mysql數(shù)據(jù)庫用戶操作
## 1.1創(chuàng)建用戶
```shell
create user ‘nz’ identified by ‘123456’ # hzm:用戶賬號(hào),123456:密碼
create user ‘nz’@’%’ identified by ‘123456’ #所有ip都可用賬號(hào)
create user ‘nz’@’localhost’ identified by ‘123456’ #本地可用賬號(hào)
create user ‘nz’@’192.168.12.1’ identified by ‘123456’ #具體哪個(gè)IP可以使用賬號(hào)
create user ‘nz’@’192.168.12.%’ identified by ‘123456’ #具體哪個(gè)網(wǎng)段可以使用賬號(hào)
```
## 1.2修改密碼
```shell
alter user 用戶名 identified by '密碼' password expirenever; # 修改密碼之后不需要重新登錄
```
## 1.3查看當(dāng)前登錄用戶
```shell
select user() [from dual]; # 查看當(dāng)前登錄的用戶
```
dual 虛擬表,為了讓select語句完整
### 查詢用戶信息
```shell
select user,host from mysql.user;
```
## 1.4刪除用戶
```shell
drop user ‘nz’;
```
## 1.6修改用戶
### 1)修改用戶名
```shell
rename user ‘nz’ to ‘hzm1’ # hzm:原用戶; hzm1:新用戶
```
### 2)修改密碼
```shell
set password for ‘hzm’ =password(‘12321’) #hzm:用戶;12321:新密碼
```
## 1.7授權(quán)
### 基本語法
#### grant 權(quán)限 on 數(shù)據(jù)庫.表 to ‘用戶’@’IP地址’
```shell
grant select on myDB to ‘hzm’@’%’;
```
#### #授予用戶hzm 對(duì)于整個(gè)數(shù)據(jù)庫myDB的查詢(select)權(quán)限
```shell
grant select on myDB.myTable to ‘hzm’@’%’;
```
#### #授予用戶hzm 對(duì)于整個(gè)數(shù)據(jù)庫myDB下的表(mytable)的查詢(select)權(quán)限
```shell
grant select,insert on . to ‘hzm’ @’%’;
```
#### #授予用戶hzm對(duì)于所有數(shù)據(jù)庫的查詢,新增權(quán)限
```shell
grant all privileges on . to ‘hzm’@’%’;
```
#### \# 任意ip可以訪問?
```shell
GRANT ALL PRIVILEGES ON *.* TO '用戶名'@'%';?
update mysql.user set host='%' where user='txsy;
```
#### #授予用戶hzm 對(duì)于所有數(shù)據(jù)庫的所有權(quán)限,除創(chuàng)建用戶權(quán)限外文章來源:http://www.zghlxwxcb.cn/news/detail-602739.html
> 注:
> 1)創(chuàng)建與授權(quán)聯(lián)合使用
>
> ```shell
> grant all privileges on . to “hzm”@”%” identified by ‘123456’ with grant option;
> ```
>
> 2)每次授權(quán)完,刷新授權(quán)
>
> ```shell
> flush privileges;
> ```文章來源地址http://www.zghlxwxcb.cn/news/detail-602739.html
到了這里,關(guān)于Mysql數(shù)據(jù)庫用戶操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!