ToB企服应用市场:ToB评测及商务社交产业平台
标题:
MySQL用户和权限管理
[打印本页]
作者:
莫张周刘王
时间:
2022-9-17 08:41
标题:
MySQL用户和权限管理
MySQL的用户账号:
由两部分组成:用户名和主机名
格式:'user_name'@'host' host必须要用引号括起来
注意
:host可以是一个主机名也可以是具体的ip地址、网段等。
当host为主机名时:
#例如:
user1@'web1.redhat.org'
当host是ip地址或者网段时:
#例如:
tom@172.16.%.%
nacy@'192.168.1.%'
bob@'10.0.0.0/255.255.0.0'
复制代码
创建用户:
create user 'user_name'@'host' [IDENTIFIED BY 'password']
注意:
host必须用引号括起来,user_name部分可以忽略引号
创建的用户默认只有登录数据库的权限。
复制代码
创建用户的时候指定密码:create user user_name identified by 'password'
修改用户名:
rename user old_name to new_name;
复制代码
删除用户:
drop user 'user_name'@'host'
复制代码
修改用户密码:
注意
:
新版mysql中用户密码可以保存在mysql.user表的authentication_string字段中
如果mysql.user表的authentication_string和password字段都保存密码authentication_string优先生效
使用update来操作表修改密码,需要使用FLUSH PRIVILEGES刷新权限才会生效
修改用户密码
文档:
https://dev.mysql.com/doc/refman/5.7/en/set-password.html
5.6版本修改密码
mysql5.6的alter user命令只能用来修改用户密码的过期时间。
(1)update mysql.user set password=password('123456') where User="xxx" and Host = "xxx";
(2)set password for xxx@xxx = password('xxx');
复制代码
5.7及以上版本
MySQL 5.7.6版本起,user表仅使用authentication_string列代替之前版本中的password列来存储密码
使用没有的字符串 PASSWORD()
SET PASSWORD FOR 'jeffrey'@'localhost' = 'password';
复制代码
使用PASSWORD()函数(pawword()在 MySQL 5.7 中已弃用,在8.0删除了这个函数)
SET PASSWORD FOR 'jeffrey'@'localhost' = PASSWORD('password');
复制代码
更改用户密码的首选语句:alter user
ALTER USER user IDENTIFIED BY 'auth_string';
复制代码
忘记管理员密码的解决办法:
启动mysqld进程时,为其使用如下选项:
--skip-grant-tables:不做权限和账号验证。
--skip-networking:禁止远程连接(空密码远程连接上去危险性大),自己能连上因为不走端口号,走的是socket文件
复制代码
使用UPDATE命令修改管理员密码
关闭mysqld进程,移除上述两个选项,重启mysqld
范例:Mariadb 和MySQL5.6版之前破解root密码
#修改配置文件
[root@centos8 ~]#vim /etc/my.cnf
[mysqld]
skip-grant-tables
skip-networking
#重启mysql
[root@centos8 ~]#systemctl restart mysqld|mariadb
#进入MySQL修改root密码
[root@centos8 ~]#mysql #连接到MySQL
#方法1
#mariadb 旧版和MySQL5.6版之前
MariaDB [(none)]> update mysql.user set password=password('ubuntu') where user='root';
#mariadb 新版
MariaDB [(none)]> update mysql.user set authentication_string=password('ubuntu') where user='root';
#方法2
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> alter user root@'localhost' identified by 'ubuntu';
#注释掉对应文件
[root@centos8 ~]#vim /etc/my.cnf
[mysqld]
#skip-grant-tables
#skip-networking
#重启mysql
[root@centos8 ~]#systemctl restart mysqld|mariadb
复制代码
范例: MySQL5.7和8.0 破解root密码
#修改配置文件
[root@centos8 ~]#vim /etc/my.cnf
[mysqld]
skip-grant-tables
skip-networking #MySQL8.0不需要
#重启MySQL服务
[root@centos8 ~]#systemctl restart mysqld
#方法1
mysql> update mysql.user set authentication_string='' where user='root' and
host='localhost';
#方法2
mysql> flush privileges; #再执行下面任意一个命令
mysql> alter user root@'localhost' identified by 'ubuntu';
#进入MySQL后修改root密码为新密码
mysql> set password for root@'localhost'='ubuntu';
#注释掉对应语句
[root@centos8 ~]#vim /etc/my.cnf
[mysqld]
#skip-grant-tables
#skip-networking
#重启mysql
[root@centos8 ~]#systemctl restart mysqld
复制代码
更加简单破解密码的方法
停止数据库以后,删除掉mysql的所有数据。
重新启动数据库的时候就会重新初始化,相当于重装系统。
注意:测试环境测试。
复制代码
权限管理和DCL语句
注意
:
新建用户的默认权限:USAGE,仅仅能登录数据库系统
权限分类
数据库、表、字段、管理类、程序类
数据库:对数据库的相关操作权限
表:针对表的相关操作
管理类:查看数据库、创建用户等操作
程序类:针对函数、存储过程、触发器等的操作
所有权限:ALL PRIVILEGES 或 ALL
授权:grant .. to ..
grant 权限 on db.tb to user [WITH GRANT OPTION]
#*.*:表示所有数据库和所有表
#WITH GRANT OPTION:允许把自己的权限授权给别人
复制代码
例如:
mysql> grant all on *.* to tom@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for tom;
+------------------------------------------+
| Grants for tom@% |
+------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'tom'@'%' |
+------------------------------------------+
1 row in set (0.00 sec)
复制代码
在授权的时候创建用户(8.0已经删除这种语法)
grant privilege on db.tb to user identified by 'password'
复制代码
例如:
mysql> grant all on *.* to bob@'%' identified by 'redhat';
Query OK, 0 rows affected, 1 warning (0.00 sec)
复制代码
取消权限:revoke .. from ..
revoke privilege on db.tb for user
复制代码
例如:
mysql> revoke all on *.* from bob@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for bob@'%';
+---------------------------------+
| Grants for bob@% |
+---------------------------------+
| GRANT USAGE ON *.* TO 'bob'@'%' |
+---------------------------------+
1 row in set (0.00 sec)
复制代码
查看指定用户的全权限:show grants for ..
例如:
mysql> show grants for tom@'%';
+---------------------------------+
| Grants for tom@% |
+---------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'%' |
+---------------------------------+
1 row in set (0.00 sec)
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4