MySQL 创建一个指定数据库的用户示例
2022-11-12 09:54:38
内容摘要
这篇文章主要为大家详细介绍了MySQL 创建一个指定数据库的用户示例,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
mysql 创建一个用户 h
文章正文
这篇文章主要为大家详细介绍了MySQL 创建一个指定数据库的用户示例,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!mysql 创建一个用户 hail,密码 hail,指定一个数据库 haildb 给 hail
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <code class = "sql" >-- 512.笔记 512pic.com mysql -u root -p password use mysql; insert into user(host,user,password) values( 'localhost' , 'hail' ,password( 'hail' )); flush privileges; create database haildb; grant all privileges on haildb.* to hail@localhost identified by 'hail' ; flush privileges; -- End www.512pic.com(五一二笔记)</code> |
如果想指定部分权限给用户
1 2 3 4 5 | <code class = "sql" >-- 512.笔记 512pic.com grant select,update on haildb.* to hail@localhost identified by 'hail' ; flush privileges; -- End www.512pic.com(五一二笔记)</code> |
删除用户
1 2 3 4 5 6 | <code class = "sql" >-- 512.笔记 512pic.com delete from user where user= 'hail' and host= 'localhost' ; flush privileges; -- End www.512pic.com(五一二笔记)</code> |
删除用户数据库
1 2 3 4 | <code class = "sql" >-- 512.笔记 512pic.com drop database haildb; -- End www.512pic.com(五一二笔记)</code> |
修改指定用户密码
1 2 3 4 5 6 | <code class = "sql" >-- 512.笔记 512pic.com update user set password=password( 'new_password' ) where user= 'hail' and host= 'localhost' ; flush privileges; -- End www.512pic.com(五一二笔记)</code> |
1.远程登录mysql
1 2 3 4 | <code class = "sql" >-- 512.笔记 512pic.com mysql -h ip -u root -p 密码 -- End www.512pic.com(五一二笔记)</code> |
2.创建用户
格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by “密码”;
例1:增加一个test1用户,密码为123456,可以在任何主机上登录,并对所有数据库有查询,增加,修改和删除的功能。需要在mysql的root用户下进行
1 2 3 4 5 6 | <code class = "sql" >-- 512.笔记 512pic.com mysql>grant select,insert,update, delete on *.* to test1@ "%" identified by "123456" ; mysql> flush privileges; -- End www.512pic.com(五一二笔记)</code> |
例2:增加一个test2用户,密码为123456,只能在192.168.2.12上登录,并对数据库student有查询,增加,修改和删除的功能。需要在mysql的root用户下进行
1 2 3 4 5 6 | <code class = "sql" >-- 512.笔记 512pic.com mysql>grant select,insert,update, delete on student.* to test2@192.168.2.12 identified by "123456" ; mysql> flush privileges; -- End www.512pic.com(五一二笔记)</code> |
例3:授权用户test3拥有数据库student的所有权限
1 2 3 4 5 6 | <code class = "sql" >-- 512.笔记 512pic.com mysql>grant all privileges on student.* to test3@localhost identified by "123456" ; mysql> flush privileges; -- End www.512pic.com(五一二笔记)</code> |
3.修改用户密码
1 2 3 4 5 6 | <code class = "sql" >-- 512.笔记 512pic.com mysql>update mysql.user set password=password( "123456" ) where User= "test1" and Host= "localhost" ; mysql> flush privileges; -- End www.512pic.com(五一二笔记)</code> |
4.删除用户
1 2 3 4 5 6 | <code class = "sql" >-- 512.笔记 512pic.com mysql> delete from user where user= "test2" and host= "localhost" ; mysql> flush privileges; -- End www.512pic.com(五一二笔记)</code> |
5.删除数据库和删除表
1 2 3 4 5 6 | <code class = "sql" >-- 512.笔记 512pic.com mysql>drop database 数据库名; mysql>drop table 表名; -- End www.512pic.com(五一二笔记)</code> |
6.删除账户及权限
1 2 3 4 5 6 7 8 | <code class = "sql" >-- 512.笔记 512pic.com drop user 用户名@ "%" drop user 用户名@localhost flush privileges; -- End www.512pic.com(五一二笔记)</code> |
注:关于MySQL 创建一个指定数据库的用户示例的内容就先介绍到这里,更多相关文章的可以留意
代码注释