- 由用户名和主机名组成
- 格式:’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’
例如:
mysql> create user root@'10.0.0.%' identified by 'redhat';
Query OK, 0 rows affected (0.11 sec)
rename user old_name to new_name;
例如:
mysql> create user bob;
Query OK, 0 rows affected (0.01 sec)
mysql> rename user bob@'%' to bob2@'%';
Query OK, 0 rows affected (0.00 sec)
drop user 'user_name'@'host'
例如:
mysql> drop user bob2@'%';
Query OK, 0 rows affected (0.00 sec)
说明:
- 新版mysql中用户密码可以保存在mysql.user表的authentication_string字段中
- 如果mysql.user表的authentication_string和password字段都保存密码authentication_string优先生效
- 使用update来操作表修改密码,需要使用FLUSH PRIVILEGES刷新权限才会生效
不同版本的mysql修改用户的方式有所不同。
mysql5.6不能使用alter user命令来修改密码。可一使用update何set password来修改密码。
mysql5.6的alter user命令只能用来修改用户密码的过期时间。
使用update修改密码:
update mysql.user set password=password('123456') where User="xxx" and Host = "xxx";
使用set password修改密码:
set password for xxx@xxx = password('xxx');
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@'xxx' IDENTIFIED BY 'auth_string';
--skip-grant-tables:不做权限和账号验证。
--skip-networking:禁止远程连接(空密码远程连接上去危险性大),自己能连上因为不走端口号,走的是socket文件
范例: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的所有数据。
重新启动数据库的时候就会重新初始化,相当于重装系统。
注意:测试环境测试。
注意:
新建用户的默认权限:USAGE,仅仅能登录数据库系统
数据库、表、字段、管理类、程序类
- 数据库:对数据库的相关操作权限
- 表:针对表的相关操作
- 管理类:查看数据库、创建用户等操作
- 程序类:针对函数、存储过程、触发器等的操作
- 所有权限:ALL PRIVILEGES 或 ALL
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 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)
例如:
mysql> show grants for tom@'%';
+---------------------------------+
| Grants for tom@% |
+---------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'%' |
+---------------------------------+
1 row in set (0.00 sec)
Original: https://www.cnblogs.com/heyongshen/p/16675885.html
Author: 背对背依靠
Title: MySQL——用户和权限管理
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/578600/
转载文章受原作者版权保护。转载请注明原作者出处!