1. 程式人生 > >MySQL最簡單的指定數據庫進行授權操作

MySQL最簡單的指定數據庫進行授權操作

Owner iat django ble 問題 state str create open

使用場景:

在日常使用中,經常會使用到數據庫系統,其中互聯網中使用數據庫種類頻率最多的要屬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>

以上可以正常使用該用戶進行該數據庫專有使用操作。

MySQL最簡單的指定數據庫進行授權操作