mysql示例数据库bank_MySQL 创建一个指定数据库的用户示例

本文介绍了如何在MySQL中创建用户并指定其对特定数据库的权限,包括添加新用户、设置密码、创建数据库、授予权限、修改密码以及删除用户和数据库的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!

MysqL 创建一个用户 hail,密码 hail,指定一个数据库 haildb 给 hail

-- 512.笔记 jb51.cc

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;

如果想指定部分权限给用户

-- 512.笔记 jb51.cc

grant select,update on haildb.* to hail@localhost identified by 'hail';

flush privileges;

-- 512.笔记 jb51.cc

delete from user where user='hail' and host='localhost';

flush privileges;

-- 512.笔记 jb51.cc

drop database haildb;

修改指定用户密码

-- 512.笔记 jb51.cc

update user set password=password('new_password') where user='hail' and host='localhost';

flush privileges;

-- 512.笔记 jb51.cc

MysqL -h ip -u root -p 密码

2.创建用户

格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by “密码”;

例1:增加一个test1用户,密码为123456,可以在任何主机上登录,并对所有数据库有查询,增加,修改和删除的功能。需要在MysqL的root用户下进行

-- 512.笔记 jb51.cc

MysqL>grant select,insert,update,delete on *.* to test1@"%" identified by "123456";

MysqL>flush privileges;

例2:增加一个test2用户,密码为123456,只能在192.168.2.12上登录,并对数据库student有查询,增加,修改和删除的功能。需要在MysqL的root用户下进行

-- 512.笔记 jb51.cc

MysqL>grant select,delete on student.* to test2@192.168.2.12 identified by "123456";

MysqL>flush privileges;

例3:授权用户test3拥有数据库student的所有权限

-- 512.笔记 jb51.cc

MysqL>grant all privileges on student.* to test3@localhost identified by "123456";

MysqL>flush privileges;

3.修改用户密码

-- 512.笔记 jb51.cc

MysqL>update MysqL.user set password=password("123456") where User="test1" and Host="localhost";

MysqL>flush privileges;

-- 512.笔记 jb51.cc

MysqL>delete from user where user="test2" and host="localhost";

MysqL>flush privileges;

-- 512.笔记 jb51.cc

MysqL>drop database 数据库名;

MysqL>drop table 表名;

6.删除账户及权限

-- 512.笔记 jb51.cc

drop user 用户名@"%"

drop user 用户名@localhost

flush privileges;

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值