1·、用户的介绍
Mysql用户格式: 用户名@‘白名单’
白名单 :地址列表 就是允许谁通过ip登录到我们的mysql中
常用的用户格式:
old@’localhost’ : old 用户能够通过本地登录
Old@’10.0.0.10’: old用户能够通过10.0.0.10 远程登录mysql 服务器
Old@‘10.0.0.%’: old 用户能够通过10.0.0.xx/24 远程登录mysql 服务器
Old@‘10.0.0.5%’: old 用户能够通过—50-59-------远程登录mysql 服务器
Old@‘10.0.0.0/255.255.255.254’:old 用户能够通过10.0.0.0/23 远程登录mysql 服务器
-----------@‘%’: 所有用户远程登录mysql 服务器
2.用户管理
查:
use mysql
show tables;
desc user;
select user,host ,authentication_string from mysql.user;
增:
添加用户:Create user oldguo@’localhost’;
添加用户并创建密码:> create user wyq@‘192.168.209.%’ identified by ‘123’;
用户文件在系统里的 cd /data/3306/mysql
改: 修改密码:
alter user oldguo@‘localhost’ identified by ‘123’;
删:
drop user cyz@‘w’;
注意:8.0 版本以前是可以通过greant命令建立用户授权 以后时候先创建后授权
3.权限管理
(1) 作用
用户对数据库对象,有哪些管理能力 。
Linux中权限作用在文件目录上
Mysql的权限是针对用户
(2)权限表现方式
具体命令。
show privileges;
---------------------------------------+
| Alter | Tables | To alter the table |
| Alter routine | Functions,Procedures | To alter or drop stored functions/procedures |
| Create | Databases,Tables,Indexes | To create new databases and tables |
| Create routine | Databases | To use CREATE FUNCTION/PROCEDURE |
| Create temporary tables | Databases | To use CREATE TEMPORARY TABLE |
(3) 授权、回收权限操作
授权:
8.0之前版本 :
grant 权限1,权限3 on 对象 to 用户 identified by ‘密码’; (没用户会创建用户)
8.0之后版本:
Create user 用户 identified by ‘密码’;
grant 权限 on 对象 to 用户;
权限:
show privileges; 查看权限
ALL 权限 —>给管理员
grant 权限1,权限3 on 对象 to 用户 identified by ‘密码’ with grant option
grant option 给别的用户授权
对象:
表示能对那些库和表能操作
*.* :-------> chmod -R 755/ 管理员
Oldguo.* :-------> chmod -R 755 /old 普通用户
Oldguo.ti :-------> chmod -R 755 /old/ti
(2)授权的例子
例一:创建并授权管理员用户old,能够通过10.0.0.% 网段登录并管理用户
创建用户被加权限
mysql> grant all on . to old@‘10.0.0%’ identified by ‘123’ with grant option;
查询创建的用户
Select user,host from mysql.user;
查询用户权限
mysql> show grants for root@‘localhost’;
mysql> select * from mysql.user\G
例子2:创建并授权一个app@ '10.0.0.%'业务用户,能够对app库下的所有对象进行create, select, update, delete,insert操作
grant create,update,select,insert,delete on app.* to app@‘10.0.0.%’ identified by ‘123’;
mysql> show grants for app@‘10.0.0.%’;
mysql> select * form db\G
扩展:
Mysql 授权表
Mysql库下:
User ------> .
db ----------->app.*
Tables_priv --------> app.t1
(3)回收权限
Linux
Chmod -R 644 /old ---------> chmod 755 /old
Mysql
mysql> revoke create on app.* from ‘app’@‘10.0.0.%’;
[rɪˈvəʊk]
(4)超级管理员密码忘记,处理。
(1)关闭数据库
[root@db01 ~]# systemctl stop mysql
(2)使用安全模式启动
[root@db01 ~]# mysqld_safe --skip-grant-tables --skip-networking &
–skip-grant-tables 跳过授权表
–skip-networking 跳过tcp\ip连接
(3)登录数据库并修改密码
Mysql
mysql> alter user root@‘localhost’ identified by ‘123’; 修改密码
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement 报错
mysql> flush privileges; 手工加载授权表
下一章 sql语言