Files
nexus/wiki/concepts/用户权限.md
2026-04-22 04:03:04 +08:00

45 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 用户权限
## Concept Information
- **Type**: Concept
- **Status**: Active
- **Source**: [[mysql-mariadb-数据库详细信息]]
## Definition
MariaDB/MySQL 使用 `username@host` 组合作为权限控制的基本单元,同一个用户名在不同主机来源下可以拥有完全不同的权限级别。
## Permission Model
| Host Pattern | Meaning |
|--------------|---------|
| `localhost` | 仅允许本机通过 socket 连接 |
| `127.0.0.1` | 仅允许本机通过 TCP/IP 连接 |
| `%` | 允许任意主机连接 |
| `192.168.1.%` | 允许指定网段连接 |
| `%.example.com` | 允许指定域名后缀连接 |
## Common Example
```sql
-- 本地管理员(仅本机 socket
CREATE USER 'root'@'localhost' IDENTIFIED BY 'password';
-- 远程访问用户(任意主机)
CREATE USER 'shenwei'@'%' IDENTIFIED BY '!Abcde12345';
GRANT ALL PRIVILEGES ON *.* TO 'shenwei'@'%' WITH GRANT OPTION;
-- 限制特定网段
CREATE USER 'app'@'192.168.3.%' IDENTIFIED BY 'password';
GRANT SELECT, INSERT, UPDATE ON mydb.* TO 'app'@'192.168.3.%';
```
## Key Principles
1. **最小权限**:只授予应用程序所需的最小权限
2. **来源隔离**:生产环境避免使用 `%` 通配符
3. **权限分离**:不同用途使用不同账户
## Related Concepts
- [[Socket 登录]] — 本地认证方式
- [[MariaDB]] — 用户权限配置示例
## Related Entities
- [[MariaDB]] — 权限配置实践