Home >Database >Mysql Tutorial >Introduction to the method of creating users and authorization in MySQl (code example)

Introduction to the method of creating users and authorization in MySQl (code example)

不言
不言forward
2019-01-26 11:23:363417browse

This article brings you an introduction to the method of creating users and authorization in MySQl (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Permission management:

  • How to create a user and password

  • Authorize the current user

  • Remove the permissions of the current user

First go to the mysql database:

mysql> use mysql
Database changed

Secondly, add, delete, and modify the new user:

1.创建用户:
# 指定ip:192.118.1.1的chao用户登录
create user 'chao'@'192.118.1.1' identified by '123';
# 指定ip:192.118.1.开头的chao用户登录
create user 'chao'@'192.118.1.%' identified by '123';
# 指定任何ip的chao用户登录
create user 'chao'@'%' identified by '123';
 
2.删除用户
drop user '用户名'@'IP地址';
 
 
3.修改用户
rename user '用户名'@'IP地址' to '新用户名'@'IP地址';
 
4.修改密码
set password for '用户名'@'IP地址'=Password('新密码');

Then, authorize the current user

#查看权限
show grants for '用户'@'IP地址'
 
#授权 chao用户仅对db1.t1文件有查询、插入和更新的操作
grant select ,insert,update on db1.t1 to "chao"@'%';
 
# 表示有所有的权限,除了grant这个命令,这个命令是root才有的。chao用户对db1下的t1文件有任意操作
grant all privileges  on db1.t1 to "chao"@'%';
#chao用户对db1数据库中的文件执行任何操作
grant all privileges  on db1.* to "chao"@'%';
#chao用户对所有数据库中文件有任何操作
grant all privileges  on *.*  to "chao"@'%';
  
#取消权限
  
# 取消chao用户对db1的t1文件的任意操作
revoke all on db1.t1 from 'chao'@"%"; 
 
# 取消来自远程服务器的chao用户对数据库db1的所有表的所有权限
 
revoke all on db1.* from 'chao'@"%"; 
 
取消来自远程服务器的chao用户所有数据库的所有的表的权限
revoke all privileges on *.* from 'chao'@'%';

The above is the detailed content of Introduction to the method of creating users and authorization in MySQl (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete