mysql 用户管理
1. 实验环境:1.1 服务器操作系统为centos71.2 数据库由yum安装安装命令:yum -y install mariadb mariadb-server1.3 使用mysql命令直接登录数据库[root@host-137 ~]# mysqlWelcome to the MariaDB monitor.Commands end with ; or \g.Your MariaDB conn
·
1. 实验环境:
1.1 服务器操作系统为centos7
1.2 数据库由yum安装
安装命令:
yum -y install mariadb mariadb-server
1.3 启动mysql服务
systemctl start mariadb
1.4 使用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 先使用mysqladmin
命令给root设置密码 123
mysqladmin命令详解>> https://blog.csdn.net/m0_46674735/article/details/112692395
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)
更多推荐
已为社区贡献1条内容
所有评论(0)