Mysql设置某个用户的ip访问权限

Mysql设置某个用户的ip访问权限

  • 示例语句
grant all privileges on *.* to 'root'@'10.10.11.12' identified by 'root666' with grant option;
flush privileges;
命令解释
  • '10.10.11.12’是允许远程访问的ip地址(可使用%来表示所有的ip),'root’是账号,'root666’是密码
  • ALL PRIVILEGES ON 后面的 *.*表示所有数据库
  • 第二行是使设置立刻生效
以上语句执行后会影响mysql库的user表的数据记录(添加或者修改表中的数据)

基础语法

grant priv_type on database.table to user[identified by [password] 'password'][,user [identified by [password] 'password']...]
  • priv_type代表允许操作的权限
  • database.table代表数据库名.表名
  • *代表全部,*.* 表示全部数据库的全部表
  • user由用户名(User)和主机名(Host)构成,中间用@隔开,最好加上单引号,不加也可以执行通过

例子

  • 1、授权所有数据库的所有表的所有权限给ip为任意值用户名为test密码为pwd的用户
grant all on *.* to 'test'@'%' identified by 'pwd';
  • 2、授权mydb数据库的所有表的增删改查权限给ip为1.1.1.1用户名为test密码为pwd的用户
grant select,insert,update,delete on mydb.* to 'test'@'1.1.1.1' identified by 'pwd';
  • 3、授权mydb数据库的stu表的修改权限给ip为1.1.1.1用户名为test密码为pwd的用户
grant update(name,age) on mydb.stu to 'test'@'1.1.1.1' identified by 'pwd';

Mysql权限层级相关表

user表:全局层级
  • 存储用户记录的表。关键字段有Host、User、Password。
  • 创建对所有表有SELECT操作权限的用户。
  • GRANT SELECT ON *.* TO 'test'@'1.1.1.1' identified by 'pwd'
db表:数据库层级
  • 存储该用户对一个数据库所有的操作权限。关键字段有Host、User、Db。
  • 授予所有权限
    • GRANT ALL ON mydb.* to 'test'@'1.1.1.1' identified by 'pwd'
tables_priv表:表层级
  • 记录了对一个表的单独授权记录。关键字段有Host、User、Db、Table_name、Table_priv、Column_priv。
  • 当授权all在某张表的时候,Table_priv会有如下授权记录:
    • Select,Insert,Update,Delete,Create,Drop,References,Index,Alter,Create View,Show view,Trigger
  • 单独授权表的某一列,会记录在此表的Column_priv里
    • GRANT UPDATE(age) on databaseName.tableName to 'test'@'1.1.1.1'
    • GRANT Select(address) on databaseName.tableName to 'test'@'1.1.1.1'
    • 此时会在另一张表columns_priv表中留下单独授权记录
columns_priv表:列层级
  • 记录对表的某一列的授权记录。关键字段有Host、User、Db、table_name、Column_name
procs_priv表:子程序层级
  • 可以对存储过程和存储函数进行权限设置。关键字段Host、User、proc_priv

用户相关操作

创建用户
CREATE USER方式
  • 必须要拥有CREATE USER权限
  • crate user username [identified by [password] 'password'],[user [identified by [password] 'password'] ]...
    • create user 'name'@'%' identified by 'pwd';
INSERT方式
  • 必须拥有mysql.user表的insert权限。另外,ssl_cipher、x509_issuer、x509_subject等必须要设置值
  • insert into mysql.user(host,user,password,ssl_cipher,x509_issuer,x509_subject) values ('%','usename',password('pwd'),'','','')
GRANT方式
  • 需要拥有GRANT权限
  • grant priv_type on database.table to user[identified by [password] 'password'][,user [identified by [password] 'password' ]... ];
    • grant all on mydb.* to username@'1.1.1.1' identified by 'pwd';
删除用户
DROP USER方式
  • 需要拥有DROP USER权限
  • DROP USER user[,user]…
    • user是需要删除的用户,有用户名(User)和主机名(Host)构成。
    • DROP USER username@'ip'
DELETE方式
  • delete from mydb.user where host = '%' and user = 'admin'
修改用户
修改用户名称
  • rename username to newname
修改用户密码
mysqladmin方式
  • mysqladmin -u username -p password 'newPassword'
修改user表
  • update user set password = password('newPassword') where user = 'name'
set语句方式
  • 修改当前用户
    • set password = password('newPassword')
  • 修改其它用户
    • set password for 'name'@'%' identified by 'newPassword'
grant方式
  • grant select on *.* to 'name'%'%' identified by 'newPassword'
忘记密码
  • mysqld_saft方式
    • 1.停止mysql:service mysqld stop
    • 2.安全模式启动:mysqld_safe -skip-grant-tables &
    • 3.无密码回车键登录:mysql -uroot -p
    • 4.重置密码: (1) user mysql; (2) update user set password = password('') where user = 'root' and host = 'localhost'; (3) flush privileges;
    • 5.正常启动:service mysql restart;
    • 6.再使用mysqladmin重新设置密码:mysqladmin password '123456'
  • 使用普通账号来找回密码
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  • 25
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值