mysql数据库权限操作

权限操作

作用:

l 给数据库设置密码,保证数据库数据相对安全

可以让指定用户拥有相应权限来管理数据库,保证数据库安全(root权限太大,过度危险)

l 可以让远程主机连接数据库,方便且及时地进行数据库管理。

1. 给数据库设置密码:

基本语法:mysqladmin -u数据库用户名 password”密码”

[root@db51 ~]# mysqladmin -uroot password "root"

[root@db51 ~]# mysql -uroot

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@db51 ~]# mysql -uroot -proot

Welcome to the MariaDB monitor.  Commands end with ; or g.

Your MariaDB connection id is 5

Server version: 5.5.65-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)]>

2. 查看数据库权限设置情况:

l 查看所有相关信息:

MariaDB [(none)]> select * from mysql.userG;

 

 

l 查看相关用户和主机信息

MariaDB [(none)]> select user,host from mysql.user;

 

3. 添加权限方式:

语法格式:grand 权限1,权限2/all on 库名.表名/*  to ‘用户名’@’主机地址’ identified by ‘密码’;   

注释:

all表示所有权限  

*代表数据库内所有表格

10.0.0.%表示10.0.0.0/24这个网段内所有主机地址

MariaDB [dazhu]> grant select,insert on dazhu.* to 'dazhu'@'10.0.0.%' identified by 'dazhu123';

Query OK, 0 rows affected (0.00 sec)

 

 

4. 回收/删除权限;

语法格式:revoke 权限 on .from ‘用户’@’主机地址’ ;

MariaDB [(none)]> revoke insert on dazhu.* from 'dazhu'@'10.0.0.%';

Query OK, 0 rows affected (0.00 sec)

5. 查看用户权限:

l 查看当前用户权限:

语法格式:show grants;

l 查看其他用户权限

语法格式:show grants for ‘用户’@’主机地址’;

l 刷新权限:

语法格式:flush privileges;

原文地址:https://www.cnblogs.com/dazhu-secure/p/13717194.html