微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

mysql 用户管理

1. 实验环境:

1.1 服务器操作系统为centos7

1.2 数据库由yum安装

安装命令:

yum -y install mariadb mariadb-server

1.3 使用MySQL命令直接登录数据库

[root@host-137 ~]# MysqL
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.68-MariaDB MariaDB Server

copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

2. 查看所有的用户

MariaDB [(none)]> select user,host,password from MysqL.user;
+------+-----------+----------+
| user | host      | password |
+------+-----------+----------+
| root | localhost |          |
| root | host-137  |          |
| root | 127.0.0.1 |          |
| root | ::1       |          |
|      | localhost |          |
|      | host-137  |          |
+------+-----------+----------+
6 rows in set (0.00 sec)

3. 创建用户

3.1 语法格式:

create user 用户名@'地址' identified by'密码';

注:
地址的表示方法有:

'%'
'localhost'
'127.0.0.1'
'10.0.0.%'
'10.0.0.5%'
'10.0.0.0/255.255.254.0'
'10.0.%'

3.2 创建用户实例:

MariaDB [(none)]> create user tom@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)

4. 修改用户密码

4.1 方法

4.1.1 sql语句

update user set password=password('新密码') where host='主地址' and user='用户名';
flush privileges;//刷新权限 

4.1.2 实例:

MariaDB [(none)]> use MysqL;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [MysqL]> update user set password=password('456') where user='tom';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

刷新授权表
MariaDB [MysqL]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

4.2 方法

4.2.1 sql语句

set password for 用户名@'主机地址' =password('新密码');  //注这种方法无需刷新权限

4.2.2 实例:

MariaDB [MysqL]> set password for tom@'localhost'=password('123'); 
Query OK, 0 rows affected (0.00 sec)

5. 忘记root密码后的恢复

5.1 先给root设置密码 123

 MysqLadmin -uroot  password 123

5.2 然后将MysqL服务停止

systemctl stop mariadb

5.2 修改MysqL配置文件,跳过授权表

vim /etc/my.cnf

在[MysqLd]下添加一行 skip_grant_tables=1,使其登录时跳过权限检查

在这里插入图片描述

5.3 保存退出之后启动MysqL服务

systemctl start  mariadb

5.4 登录MysqL重置root的密码

[root@host-137 ~]# MysqL
Welcome to the MariaDB monitor.  Commands end with ; or \g.

MariaDB [(none)]> update MysqL.user set password=password('456') where user='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 4  Changed: 3  Warnings: 0

MariaDB [(none)]>  flush privileges;
Query OK, 0 rows affected (0.00 sec)

5.5 使用新密码登录MysqL测试

[root@host-137 ~]# MysqL -uroot -p456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.68-MariaDB MariaDB Server

copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

登录成功!!!

6. 数据库用户的权限设置

6.1 MysqL授权的语句:

grant 权限列表 on  权限的层级   to   '用户名'@'主机IP'

6.2 权限列表有

ALL ,SELECT,INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE

6.3 权限层级

权限可以分为四个层级:

全局级别(*.*)
数据库级别(数据库名.*)
表级别(数据库名.表名)
列级别( 权限(列) 数据库名.表名)。

存放位置

全局级别的权限存放在MysqL.user表中
数据库级别的权限存放在MysqL.db或者MysqL.host
表级别的权限存放在MysqL.tables_priv中
列级别的权限存放在MysqL.columns_priv中。

6.4 为用户授权

6.4 授权的同时创建用户

grant  权限列表 on 数据库名.表名 to 用户@'地址' identified by '密码';
MariaDB [(none)]> grant select on MysqL.* to tom@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)

7. 收回用户的权限

7.1 查看用户的权限:

show grants for 用户@'地址';

实例:

MariaDB [(none)]> show grants for tom@'localhost';
+------------------------------------------------------------------------------------------------------------+
| Grants for tom@localhost                                                                                   |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'localhost' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
| GRANT SELECT ON `MysqL`.* TO 'tom'@'localhost'                                                             |
+------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

7.2 收回用户的权限:

revoke 权限列表 on 数据库名.表名 from 用户@'地址';

实例:

MariaDB [(none)]> revoke select on MysqL.* from tom@'localhost';
Query OK, 0 rows affected (0.00 sec)

在查看tom用户权限已经为空
MariaDB [(none)]> show grants for tom@'localhost';
+------------------------------------------------------------------------------------------------------------+
| Grants for tom@localhost                                                                                   |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'localhost' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
+------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐