MySQL -- 用户管理

MySQL -- 用户管理

文章目录


一、用户

1.用户信息

MySQL中的用户,都存储在系统数据库mysql的user表中:

  • host: 表示这个用户可以从哪个主机登陆,如果是localhost,表示只能从本机登陆;
  • user: 用户名;
  • authentication_string: 用户密码通过password函数加密后的;
  • *_priv: 用户拥有的权限;

2.创建用户

sql 复制代码
create user '用户名'@'登陆主机/ip' identified by '密码';


  • 查看当前登录用户:

3.删除用户

sql 复制代码
drop user '用户名'@'主机名'

4.远端登录MySQL

新建允许远端登陆的用户
%表示允许任何ip地址,可以换成固定的ip地址;

使用windows cmd远端登录MySQL,-P后面是MySQL的端口号,这里改为了8080(默认是3306);

5.修改用户密码

自己改自己密码:

sql 复制代码
set password=password('新的密码');

root用户修改指定用户的密码:

sql 复制代码
set password for '用户名'@'主机名'=password('新的密码');

6.数据库的权限

MySQL数据库提供的权限列表:

给用户授权:
刚创建的用户没有任何权限。需要给用户授权。

sql 复制代码
grant 权限列表 on 库.对象名 to '用户名'@'登陆位置' [identified by '密码'];
  • 权限列表,多个权限用逗号分开:
sql 复制代码
grant select on ...
grant select, delete, create on ....
grant all [privileges] on ... -- 表示赋予该用户在该对象上的所有权限
  • *.*: 代表本系统中的所有数据库的所有对象(表,视图,存储过程等);
  • 库.* : 表示某个数据库中的所有数据对象(表,视图,存储过程等);
  • identified by可选。 如果用户存在,赋予权限的同时修改密码,如果该用户不存在,就是创建用户;

案例:

  • 终端A:
sql 复制代码
--使用root账号
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| 57test |
| bit_index |
| ccdata_pro |
| innodb_test |
| musicserver |
| myisam_test |
| mysql |
| order_sys |
| performance_schema |
| scott |
| sys |
| test |
| vod_system |
+--------------------+
14 rows in set (0.00 sec)
mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| account |
| student |
| user |
+----------------+
3 rows in set (0.01 sec)
--给用户whb赋予test数据库下所有文件的select权限
mysql> grant select on test.* to 'whb'@'localhost';
Query OK, 0 rows affected (0.01 sec)
  • 终端B:
sql 复制代码
--使用whb账号
--终端B
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)
--暂停等root用户给whb赋完权之后,在查看
mysql> show databases;
+--------------------+
| Database |
+--------------------+
注意:如果发现赋权限后,没有生效,执行如下指令:
| information_schema |
| test | --赋完权之后,就能看到新的表
+--------------------+
2 rows in set (0.01 sec)
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| account |
| student |
| user |
+----------------+
3 rows in set (0.00 sec)
mysql> select * from account;
+----+--------+---------+
| id | name | blance |
+----+--------+---------+
| 2 | 李四 | 321.00 |
| 3 | 王五 | 5432.00 |
| 4 | 赵六 | 543.90 |
| 5 | 赵六 | 543.90 |
+----+--------+---------+
4 rows in set (0.00 sec)
--没有删除权限
mysql> delete from account;
ERROR 1142 (42000): DELETE command denied to user 'whb'@'localhost' for table
'account'

特定用户现有查看权限:

sql 复制代码
mysql> show grants for 'whb'@'%';
+-----------------------------------------------+
| Grants for whb@% |
+-----------------------------------------------+
| GRANT USAGE ON *.* TO 'whb'@'%' |
| GRANT ALL PRIVILEGES ON `test`.* TO 'whb'@'%' |
+-----------------------------------------------+
2 rows in set (0.00 sec)
mysql> show grants for 'root'@'%';
+-------------------------------------------------------------+
| Grants for root@% |
+-------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION |
+-------------------------------------------------------------+
1 row in set (0.00 sec)

注意:如果发现赋权限后,没有生效,执行如下指令:

sql 复制代码
flush privileges;

回收权限:

sql 复制代码
revoke 权限列表 on 库.对象名 from '用户名'@'登陆位置';

示例:

sql 复制代码
-- 回收whb对test数据库的所有权限
--root身份,终端A
mysql> revoke all on test.* from 'whb'@'localhost';
Query OK, 0 rows affected (0.00 sec)
--whb身份,终端B
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
+--------------------+
2 rows in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)
相关推荐
月光水岸New1 小时前
Ubuntu 中建的mysql数据库使用Navicat for MySQL连接不上
数据库·mysql·ubuntu
我爱松子鱼1 小时前
mysql之规则优化器RBO
数据库·mysql
人间打气筒(Ada)3 小时前
MySQL主从架构
服务器·数据库·mysql
和道一文字yyds3 小时前
MySQL 中的索引数量是否越多越好?为什么?如何使用 MySQL 的 EXPLAIN 语句进行查询分析?MySQL 中如何进行 SQL 调优?
数据库·sql·mysql
哆木6 小时前
排查生产sql查询缓慢
数据库·sql·mysql
simplepeng6 小时前
我的天,我真是和androidx的字体加载杠上了
android
book01217 小时前
MySql数据库运维学习笔记
运维·数据库·mysql
纠结哥_Shrek7 小时前
Oracle和Mysql的区别
数据库·mysql·oracle
极客先躯7 小时前
说说高级java每日一道面试题-2025年2月13日-数据库篇-请说说 MySQL 数据库的锁 ?
java·数据库·mysql·数据库的锁·模式分·粒度分·属性分
小猫猫猫◍˃ᵕ˂◍7 小时前
备忘录模式:快速恢复原始数据
android·java·备忘录模式