目录
如何给MySQL表和列授予权限是MySQL数据操作中非常重要的步骤,也是企业级使用MySQL数据库的起步点,以下分别参照官方教程整理的MySQL数据库的权限操作。
以下的语句可以直接使用MySQL的命令行进行操作(如何连接命令行不在此处说明),也可以使用SQL开发工具如MySQL Workbench或SQLynx等完成。
授予表级别权限
您可以通过执行以下操作在 MySQL 中创建具有表级权限的用户:
- 使用 Create_user_priv 和 Grant_priv 以用户身份连接到 MySQL。通过运行以下查询确定哪些用户具有这些权限。您的用户已经需要 MySQL.user 上的 SELECT 权限才能运行查询。
SELECT` `User`, `Host`, `Super`_priv, `Create`_user_priv, `Grant`_priv `from` mysql.user `WHERE` `Create`_user_priv = `'Y'` `AND` `Grant`_Priv = `'Y'`;
`
- 运行以下查询来为受限用户生成 GRANT 语句。将"mydatabase"、"myuser"和"myhost"替换为您的数据库的特定信息。
请注意,myuser 和 mypassword 周围的引号是两个单引号,而不是双引号。myhost 和 TABLE_NAME 周围的字符是反引号(该键位于键盘上的 Esc 键下方)。
`SELECT CONCAT(`'GRANT SELECT, SHOW VIEW ON mydatabase.`'`, TABLE_NAME, `'` to ''myuser''@`myhost`;'`)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = `'mydatabase'`;`
例如,如果您想使用 chartio_connect 客户端将用户"chartio_read_only"连接到您的"报告"数据库,您可以运行以下命令:
`SELECT CONCAT(`'GRANT SELECT, SHOW VIEW ON Reports.`'`, TABLE_NAME, `'` to ''chartio_read_only''@`localhost`;'`)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = `'Reports'`;`
如果您想使用来自 Chartio 服务器的直接连接将用户"chartio_direct_connect"连接到您的"Analytics"数据库,您可以运行以下命令:
`SELECT CONCAT(`'GRANT SELECT, SHOW VIEW ON Analytics.`'`, TABLE_NAME, `'` to ''chartio_direct_connect''@`52.6.1.1`;'`)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = `'Analytics'`;`
- 查询结果应类似以下内容:
GRANT` `SELECT`, `SHOW` `VIEW` `ON` mydatabase.``Activity`` to `'myuser'`@``myhost``;
`GRANT` `SELECT`, `SHOW` `VIEW` `ON` mydatabase.``Marketing`` to `'myuser'`@``myhost``;
`GRANT` `SELECT`, `SHOW` `VIEW` `ON` mydatabase.``Operations`` to `'myuser'`@``myhost``;
`GRANT` `SELECT`, `SHOW` `VIEW` `ON` mydatabase.``Payments`` to `'myuser'`@``myhost``;
`GRANT` `SELECT`, `SHOW` `VIEW` `ON` mydatabase.``Plans`` to `'myuser'`@``myhost``;
`GRANT` `SELECT`, `SHOW` `VIEW` `ON` mydatabase.``Services`` to `'myuser'`@``myhost``;
`GRANT` `SELECT`, `SHOW` `VIEW` `ON` mydatabase.``Subscriptions`` to `'myuser'`@``myhost``;
`GRANT` `SELECT`, `SHOW` `VIEW` `ON` mydatabase.``Users`` to `'myuser'`@``myhost``;
`GRANT` `SELECT`, `SHOW` `VIEW` `ON` mydatabase.``Visitors`` to `'myuser'`@``myhost``;`
- 仅选择您想要授予访问权限的表的语句并运行这些查询。例如,如果我们只想授予对用户和访问者表的访问权限,我们将运行:
GRANT` `SELECT`, `SHOW` `VIEW` `ON` mydatabase.``Users`` to `'myuser'`@``myhost``;
`GRANT` `SELECT`, `SHOW` `VIEW` `ON` mydatabase.``Visitors`` to `'myuser'`@``myhost``;`
- 为用户提供一个安全的密码。
SET` `PASSWORD` `FOR` `'chartio_read_only'`@``localhost`` = `PASSWORD`(`'top$secret'`);`
或者
SET` `PASSWORD` `FOR` `'chartio_direct_connect'`@``52.6.1.1`` = `PASSWORD`(`'top$secret'`);`
现在您可以安全地使用此用户访问您的数据库,并确保它只对指定的表具有权限。
授予列级别权限
授予特定表的列级权限的过程与授予表级权限的过程非常相似。
- 使用以下查询生成列级权限的 GRANT 语句:
`SELECTCONCAT(`'GRANT SELECT (`'`, COLUMN_NAME, `'`), SHOW VIEW ON mydatabase.`'`, TABLE_NAME, `'` to ''myuser''@`myhost`;'`)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = `'mydatabase'` AND TABLE_NAME = `'mytable'`;`
例如,如果您想使用 chartio_connect 客户端将用户"chartio_read_only"连接到"Reports"数据库的"Users"表中的特定列,则可以运行以下命令:
`SELECTCONCAT(`'GRANT SELECT (`'`, COLUMN_NAME, `'`), SHOW VIEW ON Reports.`'`, TABLE_NAME, `'` to ''chartio_read_only''@`localhost`;'`)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = `'Reports'` AND TABLE_NAME = `'Users'`;`
- 查询结果应类似于以下内容:
GRANT` `SELECT` (``User_ID``), `SHOW` `VIEW` `ON` `Reports`.``Users`` to `'chartio_read_only'`@``localhost``;
`GRANT` `SELECT` (``Campaign_ID``), `SHOW` `VIEW` `ON` `Reports`.``Users`` to `'chartio_read_only'`@``localhost``;
`GRANT` `SELECT` (``Created_Date``), `SHOW` `VIEW` `ON` `Reports`.``Users`` to `'chartio_read_only'`@``localhost``;
`GRANT` `SELECT` (``Company``), `SHOW` `VIEW` `ON` `Reports`.``Users`` to `'chartio_read_only'`@``localhost``;
`GRANT` `SELECT` (``City``), `SHOW` `VIEW` `ON` `Reports`.``Users`` to `'chartio_read_only'`@``localhost``;
`GRANT` `SELECT` (``State``), `SHOW` `VIEW` `ON` `Reports`.``Users`` to `'chartio_read_only'`@``localhost``;
`GRANT` `SELECT` (``Zip``), `SHOW` `VIEW` `ON` `Reports`.``Users`` to `'chartio_read_only'`@``localhost``;
`GRANT` `SELECT` (``Phone_Number``), `SHOW` `VIEW` `ON` `Reports`.``Users`` to `'chartio_read_only'`@``localhost``;
`GRANT` `SELECT` (``Credit_Card``), `SHOW` `VIEW` `ON` `Reports`.``Users`` to `'chartio_read_only'`@``localhost``;`
- 仅选择您想要授予访问权限的列的语句并运行这些查询。例如,如果我们只想授予对"User_ID"和"Company"列的访问权限,我们将运行:
GRANT` `SELECT` (``User_ID``), `SHOW` `VIEW` `ON` `Reports`.``Users`` to `'chartio_read_only'`@``localhost``;
`GRANT` `SELECT` (``Company``), `SHOW` `VIEW` `ON` `Reports`.``Users`` to `'chartio_read_only'`@``localhost``;`
- 为用户提供一个安全的密码。
SET` `PASSWORD` `FOR` `'chartio_read_only'`@``localhost`` = `PASSWORD`(`'top$secret'`);
`
欲了解更多信息,请咨询 MySQL 文档。