目录
MySQL创建只读用户
应用场景:只要公司有数据团队的,直接给root权限操作,未免有点危险,于是只能给设权限,一般而言,他们只是做读操作,既然做读操作,那么只要有个select权限就可以了。
我们可以通过GRANT来创建用户:
MySQL 5.7
- 创建用户 设置只读权限
GRANT SElECT ON *.* TO 'read_only_user'@'ip' IDENTIFIED BY "password"
- 刷新
flush privileges;
其实这个语句与开通远程连接差不多,read_only_user
就是用户名,随便起,ip
就是允许连接到这个数据库的ip,当然如果不限制ip,那就写成 %
,最后的password
就是连接密码了。
MySQL 8.0
- 创建用户设置只读权限
CREATE USER `read_only_user`@`ip` IDENTIFIED BY 'password';
- 授予权限
GRANT SELECT ON *.* TO `read_only_user`@`ip` WITH GRANT OPTION;
- 删除权限
REVOKE all privileges ON *.* FROM 'read_only_user'@'ip';
- 刷新
flush privileges;
查看一下权限:
mysql> select * from user where User='read_only_user' \G;
*************************** 1. row ***************************
Host: %
User: read_only_user
Select_priv: Y
Insert_priv: N
Update_priv: N
Delete_priv: N
Create_priv: N
Drop_priv: N
Reload_priv: N
Shutdown_priv: N
Process_priv: N
File_priv: N
Grant_priv: N
References_priv: N
Index_priv: N
Alter_priv: N
Show_db_priv: N
Super_priv: N
Create_tmp_table_priv: N
Lock_tables_priv: N
Execute_priv: N
Repl_slave_priv: N
Repl_client_priv: N
Create_view_priv: N
Show_view_priv: N
Create_routine_priv: N
Alter_routine_priv: N
Create_user_priv: N
Event_priv: N
Trigger_priv: N
Create_tablespace_priv: N
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin: mysql_native_password
authentication_string: *088AD884E918FC37145D74D60C3E6ACA3F1D9067
password_expired: N
password_last_changed: 2021-01-04 11:01:19
password_lifetime: NULL
account_locked: N
4 rows in set (0.00 sec)
发现除了Select_priv是Y,其他的全部为N