• MySQL学习笔记16


    MySQL的用户管理和权限管理:

    在MySQL中创建更多的用户,不可能都使用root用户。root用户的权限太高,一个误操作,容易造成很大的失误。

    注意:MySQL中不能单纯通过用户名来说明用户,必须加上主机,如jack@10.1.1.1

    1、语法:

    创建用户的时候,直接设置密码。

    mysql> create user '用户名'@'被允许连接的主机名称或主机的IP地址' identified by '用户密码';
    mysql> select user,host from mysql.user;

    2、创建MySQL账号,用户名tom,用户密码:123

    1. mysql> create user 'tom'@'localhost' identified by '123';
    2. Query OK, 0 rows affected (0.00 sec)
    3. mysql> select user,host from mysql.user;
    4. +---------------+-----------+
    5. | user | host |
    6. +---------------+-----------+
    7. | mysql.session | localhost |
    8. | mysql.sys | localhost |
    9. | root | localhost |
    10. | tom | localhost |
    11. +---------------+-----------+
    12. 4 rows in set (0.00 sec)

    该tom用户只能本地访问。localhost本机访问。

    我们通过navicat软件进行登录,发现登录不上,这是因为root账号只能本地登录。

    案例:创建一个mysql账号,要求开通远程连接。主机ip地址192.168.17.125,用户名harry,密码:123。

    create user 'harry'@'192.168.17.125' identified by '123';

    然后在192.168.17.125服务器上进行测试:

    1. [root@mysql-test ~]# mysql -h 192.168.17.123 -uharry -p
    2. Enter password:
    3. Welcome to the MariaDB monitor. Commands end with ; or \g.
    4. Your MySQL connection id is 3
    5. Server version: 5.7.43 MySQL Community Server (GPL)
    6. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    7. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    8. MySQL [(none)]>
    9. MySQL [(none)]>
    10. MySQL [(none)]> show databases;
    11. +--------------------+
    12. | Database |
    13. +--------------------+
    14. | information_schema |
    15. +--------------------+
    16. 1 row in set (0.00 sec)

    说明:

    yum install mysql -y

    yum安装mysql:代表安装的是mysql客户端;

    yum安装mysql-server:代表安装的mysql-server服务器端;

    案例:创建一个MySQL账号(要求开通远程连接),主机IP的网段:10.1.1.0,用户名:jack,用户密码:123

    1. mysql> create user 'jack'@'192.168.17.%' identified by '123';
    2. Query OK, 0 rows affected (0.04 sec)
    3. mysql>
    4. mysql> select host, user from mysql.user;
    5. +----------------+---------------+
    6. | host | user |
    7. +----------------+---------------+
    8. | 192.168.17.% | jack |
    9. | 192.168.17.125 | harry |
    10. | localhost | mysql.session |
    11. | localhost | mysql.sys |
    12. | localhost | root |
    13. | localhost | tom |
    14. +----------------+---------------+
    15. 6 rows in set (0.00 sec)
    16. [root@mysql-test ~]# mysql -h 192.168.17.123 -ujack -p
    17. Enter password:
    18. Welcome to the MariaDB monitor. Commands end with ; or \g.
    19. Your MySQL connection id is 4
    20. Server version: 5.7.43 MySQL Community Server (GPL)
    21. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    22. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    23. MySQL [(none)]>
    24. MySQL [(none)]>
    25. MySQL [(none)]> show databases;
    26. +--------------------+
    27. | Database |
    28. +--------------------+
    29. | information_schema |
    30. +--------------------+
    31. 1 row in set (0.00 sec)

    案例:创建一个MySQL账号(要求开通远程连接),要求面向所有主机开放,用户名:root,用户密码:123。

    1. mysql> create user 'root'@'%' identified by '123';
    2. Query OK, 0 rows affected (0.00 sec)
    3. mysql>
    4. mysql> select host, user from mysql.user;
    5. +----------------+---------------+
    6. | host | user |
    7. +----------------+---------------+
    8. | % | root |
    9. | 192.168.17.% | jack |
    10. | 192.168.17.125 | harry |
    11. | localhost | mysql.session |
    12. | localhost | mysql.sys |
    13. | localhost | root |
    14. | localhost | tom |
    15. +----------------+---------------+
    16. 7 rows in set (0.00 sec)

    MySQL里面的账号不能仅仅看账号,也要看IP地址。由这两个决定一个账号。

    删除账号:

    基本语法:

    mysql> drop user '用户名'@'主机名称或主机的IP地址';
    特别说明:
    如果在删除用户时没有指定主机的名称或主机的IP地址,则默认删除这个账号的所有信息。

    案例:删除tom这个账号。

    1. mysql> drop user 'tom'@'localhost';
    2. Query OK, 0 rows affected (0.00 sec)
    3. mysql>
    4. mysql> select host, user from mysql.user;
    5. +----------------+---------------+
    6. | host | user |
    7. +----------------+---------------+
    8. | % | root |
    9. | 192.168.17.% | jack |
    10. | 192.168.17.125 | harry |
    11. | localhost | mysql.session |
    12. | localhost | mysql.sys |
    13. | localhost | root |
    14. +----------------+---------------+
    15. 6 rows in set (0.00 sec)

    案例:删除jack这个账号。

    1. mysql> drop user 'jack'@'192.168.17.%';
    2. Query OK, 0 rows affected (0.00 sec)
    3. mysql>
    4. mysql> select host, user from mysql.user;
    5. +----------------+---------------+
    6. | host | user |
    7. +----------------+---------------+
    8. | % | root |
    9. | 192.168.17.125 | harry |
    10. | localhost | mysql.session |
    11. | localhost | mysql.sys |
    12. | localhost | root |
    13. +----------------+---------------+
    14. 5 rows in set (0.00 sec)

    扩展:删除MySQL账号的另外一种方式。

    1. mysql> delete from mysql.user where user='root' and host='%';
    2. Query OK, 1 row affected (0.00 sec)
    3. mysql> select host, user from mysql.user;
    4. +----------------+---------------+
    5. | host | user |
    6. +----------------+---------------+
    7. | 192.168.17.125 | harry |
    8. | localhost | mysql.session |
    9. | localhost | mysql.sys |
    10. | localhost | root |
    11. +----------------+---------------+
    12. 4 rows in set (0.00 sec)

    修改MySQL账号:

    特别说明:MySQL用户重命名通常可以更改两部分,一部分是用户的名称,一部分是被允许访问的主机名称或主机的IP地址。

    基本语法:

    mysql> rename user 旧用户信息 to 新用户信息;

    案例:把用户'root'@'%'更改为'root'@'10.1.1.%'。

    1. mysql> create user 'root'@'%' identified by '123';
    2. Query OK, 0 rows affected (0.00 sec)
    3. mysql>
    4. mysql> select host, user from mysql.user;
    5. +----------------+---------------+
    6. | host | user |
    7. +----------------+---------------+
    8. | % | root |
    9. | 192.168.17.125 | harry |
    10. | localhost | mysql.session |
    11. | localhost | mysql.sys |
    12. | localhost | root |
    13. +----------------+---------------+
    14. 5 rows in set (0.00 sec)
    15. mysql> rename user 'root'@'%' to 'root'@'192.168.17.%';
    16. Query OK, 0 rows affected (0.00 sec)
    17. mysql> select host, user from mysql.user;
    18. +----------------+---------------+
    19. | host | user |
    20. +----------------+---------------+
    21. | 192.168.17.% | root |
    22. | 192.168.17.125 | harry |
    23. | localhost | mysql.session |
    24. | localhost | mysql.sys |
    25. | localhost | root |
    26. +----------------+---------------+
    27. 5 rows in set (0.00 sec)
    1. mysql> create user 'tom'@'localhost' identified by '123';
    2. Query OK, 0 rows affected (0.00 sec)
    3. mysql> rename user 'tom'@'localhost' to 'harry'@'localhost';
    4. Query OK, 0 rows affected (0.00 sec)
    5. mysql>
    6. mysql> select host, user from mysql.user;
    7. +----------------+---------------+
    8. | host | user |
    9. +----------------+---------------+
    10. | 192.168.17.% | root |
    11. | 192.168.17.125 | harry |
    12. | localhost | harry |
    13. | localhost | mysql.session |
    14. | localhost | mysql.sys |
    15. | localhost | root |
    16. +----------------+---------------+
    17. 6 rows in set (0.00 sec)

    扩展:使用update语句更新用户信息

    1. mysql> select host, user from mysql.user;
    2. +----------------+---------------+
    3. | host | user |
    4. +----------------+---------------+
    5. | 192.168.17.% | root |
    6. | 192.168.17.125 | harry |
    7. | localhost | harry |
    8. | localhost | mysql.session |
    9. | localhost | mysql.sys |
    10. | localhost | root |
    11. +----------------+---------------+
    12. 6 rows in set (0.00 sec)
    13. mysql> update mysql.user set user='tom', host='localhost' where user='harry' and host='localhost';
    14. Query OK, 1 row affected (0.00 sec)
    15. Rows matched: 1 Changed: 1 Warnings: 0
    16. mysql> flush privileges;
    17. Query OK, 0 rows affected (0.00 sec)

  • 相关阅读:
    今日份动态规划学习
    【ROS2原理14】.msg / .srv / .action文件的做法
    腾讯云服务器手动建立WordPress个人站点Windows系统教程-Unirech腾讯云国际版代充
    2.11 流程控制之for循环
    自动驾驶专题介绍 —— 车辆尺寸
    事务(包含ACID)
    Java 异常处理
    2530. 执行 K 次操作后的最大分数
    unity搭建xlua和emmy_lua的debug环境
    关于JVM中的线程整理
  • 原文地址:https://blog.csdn.net/chang_chunhua/article/details/133267837