ToB企服应用市场:ToB评测及商务社交产业平台

标题: MySQL用户和权限管理 [打印本页]

作者: 莫张周刘王    时间: 2022-9-17 08:41
标题: MySQL用户和权限管理
MySQL的用户账号:

注意:host可以是一个主机名也可以是具体的ip地址、网段等。
  1. 当host为主机名时:
  2. #例如:
  3. user1@'web1.redhat.org'
  4. 当host是ip地址或者网段时:
  5. #例如:
  6. tom@172.16.%.%
  7. nacy@'192.168.1.%'
  8. bob@'10.0.0.0/255.255.0.0'
复制代码
创建用户:
  1. create user 'user_name'@'host'  [IDENTIFIED BY 'password']
  2. 注意:
  3. host必须用引号括起来,user_name部分可以忽略引号
  4. 创建的用户默认只有登录数据库的权限。
复制代码
创建用户的时候指定密码:create user user_name identified by 'password'
修改用户名:
  1. rename user old_name to new_name;
复制代码
删除用户:
  1. drop user 'user_name'@'host'
复制代码
修改用户密码:

注意
修改用户密码

文档:https://dev.mysql.com/doc/refman/5.7/en/set-password.html
5.6版本修改密码

mysql5.6的alter user命令只能用来修改用户密码的过期时间。
  1. (1)update mysql.user set password=password('123456') where User="xxx" and Host = "xxx";
  2. (2)set password for xxx@xxx = password('xxx');
复制代码
5.7及以上版本

MySQL 5.7.6版本起,user表仅使用authentication_string列代替之前版本中的password列来存储密码
  1. SET PASSWORD FOR 'jeffrey'@'localhost' = 'password';
复制代码
  1. SET PASSWORD FOR 'jeffrey'@'localhost' = PASSWORD('password');
复制代码
  1. ALTER USER user IDENTIFIED BY 'auth_string';
复制代码
忘记管理员密码的解决办法:

  1. --skip-grant-tables:不做权限和账号验证。
  2. --skip-networking:禁止远程连接(空密码远程连接上去危险性大),自己能连上因为不走端口号,走的是socket文件
复制代码
范例:Mariadb 和MySQL5.6版之前破解root密码
  1. #修改配置文件
  2. [root@centos8 ~]#vim /etc/my.cnf
  3. [mysqld]
  4. skip-grant-tables
  5. skip-networking
  6. #重启mysql
  7. [root@centos8 ~]#systemctl restart mysqld|mariadb
  8. #进入MySQL修改root密码
  9. [root@centos8 ~]#mysql #连接到MySQL
  10. #方法1
  11. #mariadb 旧版和MySQL5.6版之前
  12. MariaDB [(none)]> update mysql.user set password=password('ubuntu') where user='root';
  13. #mariadb 新版
  14. MariaDB [(none)]> update mysql.user set authentication_string=password('ubuntu') where user='root';
  15. #方法2
  16. MariaDB [(none)]> flush privileges;
  17. MariaDB [(none)]> alter user root@'localhost' identified by 'ubuntu';
  18. #注释掉对应文件
  19. [root@centos8 ~]#vim /etc/my.cnf
  20. [mysqld]
  21. #skip-grant-tables
  22. #skip-networking
  23. #重启mysql
  24. [root@centos8 ~]#systemctl restart mysqld|mariadb
复制代码
范例: MySQL5.7和8.0 破解root密码
  1. #修改配置文件
  2. [root@centos8 ~]#vim /etc/my.cnf
  3. [mysqld]
  4. skip-grant-tables
  5. skip-networking  #MySQL8.0不需要
  6. #重启MySQL服务
  7. [root@centos8 ~]#systemctl restart mysqld
  8. #方法1
  9. mysql> update mysql.user set authentication_string='' where user='root' and
  10. host='localhost';
  11. #方法2
  12. mysql> flush privileges; #再执行下面任意一个命令
  13. mysql> alter user root@'localhost' identified by 'ubuntu';
  14. #进入MySQL后修改root密码为新密码
  15. mysql> set password for root@'localhost'='ubuntu';
  16. #注释掉对应语句
  17. [root@centos8 ~]#vim /etc/my.cnf
  18. [mysqld]
  19. #skip-grant-tables
  20. #skip-networking
  21. #重启mysql
  22. [root@centos8 ~]#systemctl restart mysqld
复制代码
更加简单破解密码的方法

停止数据库以后,删除掉mysql的所有数据。
  1. 重新启动数据库的时候就会重新初始化,相当于重装系统。
  2. 注意:测试环境测试。
复制代码
权限管理和DCL语句

注意
新建用户的默认权限:USAGE,仅仅能登录数据库系统
权限分类

数据库、表、字段、管理类、程序类
授权:grant .. to ..
  1. grant 权限 on db.tb to user [WITH GRANT OPTION]
  2. #*.*:表示所有数据库和所有表
  3. #WITH GRANT OPTION:允许把自己的权限授权给别人
复制代码
例如:
  1. mysql> grant all on *.* to tom@'%';
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> show grants for tom;
  4. +------------------------------------------+
  5. | Grants for tom@%                         |
  6. +------------------------------------------+
  7. | GRANT ALL PRIVILEGES ON *.* TO 'tom'@'%' |
  8. +------------------------------------------+
  9. 1 row in set (0.00 sec)
复制代码
在授权的时候创建用户(8.0已经删除这种语法)
  1. grant privilege on db.tb to user identified by 'password'
复制代码
例如:
  1. mysql> grant all on *.* to bob@'%' identified by 'redhat';
  2. Query OK, 0 rows affected, 1 warning (0.00 sec)
复制代码
取消权限:revoke .. from ..
  1. revoke privilege on db.tb for user
复制代码
例如:
  1. mysql> revoke all on *.* from bob@'%';
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> show grants for bob@'%';
  4. +---------------------------------+
  5. | Grants for bob@%                |
  6. +---------------------------------+
  7. | GRANT USAGE ON *.* TO 'bob'@'%' |
  8. +---------------------------------+
  9. 1 row in set (0.00 sec)
复制代码
查看指定用户的全权限:show grants for ..

例如:
  1. mysql> show grants for tom@'%';
  2. +---------------------------------+
  3. | Grants for tom@%                |
  4. +---------------------------------+
  5. | GRANT USAGE ON *.* TO 'tom'@'%' |
  6. +---------------------------------+
  7. 1 row in set (0.00 sec)
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4