zoukankan      html  css  js  c++  java
  • mysql 访问控制和用户管理


    mysql 访问控制和用户管理 

    目的:确保数据的安全。用户的访问权限不能多也不能少。

    root对数据库有完全的控制。

    在日常工作中,不要使用root,而是使用有限权限的一系列账户,根据使用者的身份,给予不同的权限。

      

    select user(); 查看当前用户;

    SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user; 

    设定了一个查看所有的用户的格式:简单的写法是select user, host from mysql.user;

    备注: mysql数据库有一个user表,  储存了user的信息。

    创建用户 

    详细见文档:http://www.mysqltutorial.org/mysql-create-user.aspx

    CREATE USER 'username'@'hostname' IDENTIFIED BY 'password'; 
    • username是创建的用户名
    • hostname是可选的。可以localhost, 具体的ip地址,或通配符%代表任意ip地址。 
    • identified by后面的'password'可以是空字符串,代表无密码。

    创建完成后,退出控制台后,再使用mysql -u username -p进入

    后续,

    • create database xxx
    • use xxx
    • create table lists (...);

    再然后是重要的grant,授权:

    详细见文档:http://www.mysqltutorial.org/mysql-grant.aspx

    grant 权限列表 on 数据库.数据表 to 用户名@访问主机;  

    create user创建的用户只能登陆 MySQL Server, 不能做其他事情。只有做了相关授权后,才行。

    *.*表示:数据库名.表名

    例子:

    GRANT ALL PRIVILEGES ON *.* TO 'xxx'@'223.72.101.173' WITH GRANT OPTIon;

    show grants for xxx@xxx

    显示一个用户的特权。

    mysql> show grants for test_user@localhost;
    +-----------------------------------------------+
    | Grants for test_user@localhost                |
    +-----------------------------------------------+
    | GRANT USAGE ON *.* TO `test_user`@`localhost` |
    +-----------------------------------------------+

    usage表示根本没有权限。

    rename user xxx@xxx to yyy@yyy;

    改名。

    revoke xxx on *.* from xxx@xxx

    撤销某个功能的使用权限。

    revoke select on test2.* from test_user@localhost;

    各种权限可以看权限表。

    删除用户

    1. 进入root
    2. select concat(user, host) from mysql.user; 查看mysql数据库中的user表格。
    3. 然后选择要删除的用户, drop user username@host
    4. 同时授权也删除了

    更改用户登陆密码

    改之前要留意:

    • 谁在用这个账户。
    • 什么应用程序被这个账户操作?If you change the password without changing the connection string of the application that is using the user account, the application will not be able to connect to the database server. 

    2种方法:

    • set password for test_user@localhost = "123456"
    • ALTER USER dbadmin@localhost IDENTIFIED BY 'littlewhale';

     http://www.mysqltutorial.org/mysql-changing-password.aspx

  • 相关阅读:
    每天一个linux命令(39):grep 命令
    每天一个linux命令(44):top命令
    每天一个linux命令(42):kill命令
    每天一个linux命令(41):ps命令
    每天一个linux命令(55):traceroute命令
    每天一个linux命令(45):free 命令
    为什么程序员的价值总是被严重的低估?
    每天一个linux命令(54):ping命令
    每天一个linux命令(46):vmstat命令
    每天一个linux命令(48):watch命令
  • 原文地址:https://www.cnblogs.com/chentianwei/p/12106161.html
Copyright © 2011-2022 走看看