MySQL最简单的指定数据库进行授权操作

使用场景:

在日常使用中,经常会使用到数据库系统,其中互联网中使用数据库种类频率最多的要属MySQL数据库,但使用该数据库不仅仅是单个数据库对应单个用户(root),一定会涉及权限管理问题,针对某个数据库创建对应的账户进行单独授权管理,控制该用户不可越权操作其他数据库。不可具有root权限。

解决方案:

1、先使用root管理员账户登录数据库。

#mysql -uroot -p

2、查看数据库是否已有,此处以bastion数据库为例。

mysql> show databases;
+--------------------+
| Database     |
+--------------------+
| information_schema|
| assets      |
| auth       |
| auto_test     |
| bastion      |
| different     |
| django      |
| hl7        |
| mysql       |
| performance_schema|
| phpmyadmin    |
| scrap       |
| sys        |
| system_openfalcon |
| syswork      |
+--------------------+
17 rows in set (0.02 sec)

3、如果没有创建数据库。

mysql> create database bastion charset=utf8

4、创建用于管理bastion数据库的用户。

mysql>CREATE USER 'bastion'@'localhost' IDENTIFIED BY '密码';


5、进行对bastion数据库授权。 

mysql>grant all on bastion.* to 'bastion'@'localhost';

6、登录验证。

#mysql -ubastion -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 29298
Server version: 5.7.22-0ubuntu0.16.04.1 (Ubuntu)
 
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
 
mysql> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bastion            |
+--------------------+
2 rows in set (0.02 sec)
 
mysql> 
mysql> use bastion
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
mysql> show tables;
+----------------------------+
| Tables_in_bastion          |
+----------------------------+
| acct                       |
| auth_group                 |
| auth_group_permissions     |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| django_admin_log           |
| django_content_type        |
| django_migrations          |
| django_session             |
| host                       |
| host_grp                   |
| manage_user                |
| order                      |
| privilege                  |
| user                       |
+----------------------------+
17 rows in set (0.01 sec)
 
mysql>

以上可以正常使用该用户进行该数据库专有使用操作。

原文地址:https://www.cnblogs.com/zksfyz/p/9363942.html