MySQL8.0创建用户及授权

MySQL用户

首先要知道,MySQL用户和系统用户不同,前者用于MySQL数据库的访问登录,而后者则是负责系统的访问登录,互不相关。

MySQL用户的创建,是根据用户名和客户端主机IP来定义账户,MySQL将创建的用户账户存储在mysql库的user表。

MySQL提供的特权

注:只列举了常用

选项 作用
ALL[PRIVILEGES] 所有特权
ALTER 允许使用ALTER TABLE语句更改表的结构
CREATE 允许使用创建新库和表
CREATE USER 允许使用创,删,改用户的语句
DELETE 允许删除表中记录
DROP 允许删除库,表,视图
EVENT 允许使用事务,并且可以在事务中增删改查
GRANT OPTION 允许授予自己拥有的特权或从其他用户撤消特权
INDEX 允许使用创建或删除索引的语句
INSERT 允许在表中插入数据
RELOAD 允许使用FLUSH语句,flush-logs等
REPLICATION CLIENT 允许使用的SHOW MASTER STATUS,SHOW SLAVE STATUS和SHOW BINARY LOGS语句。将此特权授予从属服务器用于将其作为主服务器连接到当前服务器的帐户
REPLICATION SLAVE 启用该帐户已作出对数据库的主服务器上,使用请求更新 SHOW SLAVE HOSTS, SHOW RELAYLOG EVENTS和 SHOW BINLOG EVENTS 语句
LOCK TABLES 允许使用显式LOCK TABLES语句来锁定您具有SELECT特权的表。这包括使用写锁,这可以防止其他会话读取锁定的表
SELECT 允许使用查询语句
TRIGGER 启用触发器功能,具有此的特权才能为该表创建,删除,执行或显示触发器
UPDATE 允许修改表中记录
USAGE 无特权

创建MySQL用户

语法:


create user ‘用户’@‘主机IP’ identified by '密码’


注:identified by '密码’可以省略,表示用户无密码

例如:创建一个名为user的用户,范围是本地,密码为password。

mysql> create user 'user'@'localhost' identified by 'password';

对于主机范围,若不是本地,可以指定具体的IP地址或地址范围。例如:192.168.1.%为192.168.1.0网段,192.168.1.1为具体的主机,不为范围内的主机,即使用户密码正确也无法登录。

使用其他主机,通过user用户登录

[root@linux ~]# mysql -uuser  -h 192.168.1.124 -ppassword
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'user'@'192.168.1.125' (using password: YES)

-h:MySQL服务器IP地址

可以看到拒绝访问,就是因为主机范围设置为了localhost,而在主机即可直接登录。

用户授权

语法:


grant 权限1 权限2… on 库名.表名 to ‘用户’@'主机范围’


为user用户授予查询数据的权限

mysql> grant select on *.* to 'user'@'localhost';

*.*:代表所有库下的所有表

授权完成后,刷新一下权限即可立即生效

mysql> flush privileges;

注:授权或撤权后,都需要刷新权限,才能立即生效

查询权限

通过查询mysql库下的user表,即可查询用户所拥有的权限。

例如:查询user用户权限

mysql> select * from  mysql.user where user='user'\G
*************************** 1. row ***************************
                    Host: localhost
                    User: 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: caching_sha2_password
   authentication_string: $A$005$rXDKtJkm?,=57xd
                                                FrgO8MdSqm.esH01ZmBGmc5/0tU3ibNWo1PLZhc5Q3c4
        password_expired: N
   password_last_changed: 2020-05-29 15:35:38
       password_lifetime: NULL
          account_locked: N
        Create_role_priv: N
          Drop_role_priv: N
  Password_reuse_history: NULL
     Password_reuse_time: NULL
Password_require_current: NULL

其中Select_priv项为Y,表示权限授予成功。

用户撤权

语法:


revoke 权限1 权限2… on 库名.表名 from ‘用户名’@'主机范围’


将user用户的select权限撤销

mysql> revoke select on *.* from 'user'@'localhost';

刷新权限

mysql> flush privileges;

查看用户权限

mysql> select * from mysql.user where user='user'\G
*************************** 1. row ***************************
                    Host: localhost
                    User: user
             Select_priv: N
             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: caching_sha2_password
   authentication_string: $A$005$rXDKtJkm?,=57xd
                                                FrgO8MdSqm.esH01ZmBGmc5/0tU3ibNWo1PLZhc5Q3c4
        password_expired: N
   password_last_changed: 2020-05-29 15:35:38
       password_lifetime: NULL
          account_locked: N
        Create_role_priv: N
          Drop_role_priv: N
  Password_reuse_history: NULL
     Password_reuse_time: NULL
Password_require_current: NULL

其中Select_priv项已经改为了N

修改用户密码

方式1

语法:


alter user ‘用户名’@‘主机范围’ identified by ‘密码’


方式2

语法:


myqladmin -u用户 -p密码 password “密码”


这两种方式都可为用户修改密码,任选一种即可

例如:为user用户修改密码,将密码改为test

mysqladmin -uuser -p123456 password "test"
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

以上密码就修改完成了,通过新密码即可登录

[root@linux ~]# mysql -user -ptest
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'ser'@'localhost' (using password: YES)
[root@linux ~]# mysql -uuser -ptest
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 8.0.13 Source distribution

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

删除用户

语法:


drop user ‘用户’@'主机范围’


例如:将user用户删除

mysql> drop user 'user'@'localhost';

再次查看,用户已经消失

mysql> select * from mysql.user where user='user'\G
Empty set (0.00 sec)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章