MySQL与PHP的数据控制方法
数据控制语言
数据控制语言 (Data Control Language) 是用来设置或更改数据库用户或角色权限的语句.
MySQL 权限体系
MySQL 的权限信息主要存储在以下几张表中, 当用户连接数据库时, MySQL 会根据这些表对用户进行权限验证.
用户管理
在 MySQL 中, 使用 CREATE USER 来创建用户, 用户创建后没有任何权限.
查看所有用户:
创建用户
MySQL 的用户账号由两部分组成: 用户名和主机名, 即用户名@主机名,主机名可以是 IP 或机器名称, 主机名为%
表示允许任何地方的主机远程登录 MySQL 数据库.
格式:
CREATE USER '用户名' [@'主机名'][IDENTIFIED BY '密码'];
例子:
<?php $conn = mysqli_connect("localhost", "root","admin","mysql"); if ($conn) { echo "数据库连接成功\n"; } else { echo mysqli_connect_error(); } # SQL语句 $SQL = "CREATE USER 'user1'@'%' IDENTIFIED BY '123456'"; # 执行 $result = mysqli_query($conn, $SQL); # 查看是否执行成功 if ($result) { echo "SQL 语句执行成功!\n"; }else { echo mysqli_error($conn); } # 关闭连接 mysqli_close($conn); ?>登录后复制
效果:
删除用户
格式:
DROP USER '用户名‘[@'主机名']
例子:
<?php $conn = mysqli_connect("localhost", "root","admin","mysql"); if ($conn) { echo "数据库连接成功\n"; } else { echo mysqli_connect_error(); } # SQL语句 $SQL = "DROP USER 'user1'@'%'"; # 执行 $result = mysqli_query($conn, $SQL); # 查看是否执行成功 if ($result) { echo "SQL 语句执行成功!\n"; }else { echo mysqli_error($conn); } # 关闭连接 mysqli_close($conn); ?>登录后复制
修改密码
格式:
ALTER USER '用户名'@'主机名' IDENTIFIED BY '新密码';
例子:
<?php $conn = mysqli_connect("localhost", "root","admin","mysql"); if ($conn) { echo "数据库连接成功\n"; } else { echo mysqli_connect_error(); } # SQL语句 $SQL = "ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'"; # 执行 $result = mysqli_query($conn, $SQL); # 查看是否执行成功 if ($result) { echo "SQL 语句执行成功!\n"; }else { echo mysqli_error($conn); } # 关闭连接 mysqli_close($conn); ?>登录后复制
权限管理
MySQL 使用GRANT
和REVOKE
进行授权, 撤销授权. 权限具体分为 3 类, 数据类, 结构类, 和管理类.
INSERT
UPDATE
DELETE
FILECREATE
ALTER
INDEX
DROP
CREATE TEMPORARY TABLES
SHOW VIEW
CREATE ROUTINE
ALTER ROUTINE
EXECUTE
CREATE VIEW
EVENT
TRIGGERUSAGE
GRANT
SUPER
PROCESS
RELOAD
SHUTDOWN
SHOW DATABASES
LOCK TABLES
REFERENCES
REPUCATION CUENT
REPUCATION SLAVE
CREATE USER
查看权限
格式:
SHOW GRANTS FOR '用户名'[@'主机名']
例子:
<?php $conn = mysqli_connect("localhost", "root","admin"); if ($conn) { echo "数据库连接成功\n"; } else { echo mysqli_connect_error(); } # SQL语句 $SQL = "SHOW GRANTS FOR 'root'@'localhost'"; # 执行 $result = mysqli_query($conn, $SQL); # 查看是否执行成功 if ($result) { echo "SQL 语句执行成功!\n"; }else { echo mysqli_error($conn); } # 调试输出 while ($line = mysqli_fetch_assoc($result)) { print_r($line); } # 关闭连接 mysqli_close($conn); ?>登录后复制
输出结果:
数据库链接成功
SQL 语句执行成功!
Array
(
[Grants for root@localhost] => GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `root`@`localhost` WITH GRANT OPTION
)
Array
(
[Grants for root@localhost] => GRANT APPLICATION_PASSWORD_ADMIN,AUDIT_ADMIN,AUTHENTICATION_POLICY_ADMIN,BACKUP_ADMIN,BINLOG_ADMIN,BINLOG_ENCRYPTION_ADMIN,CLONE_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,FLUSH_OPTIMIZER_COSTS,FLUSH_STATUS,FLUSH_TABLES,FLUSH_USER_RESOURCES,GROUP_REPLICATION_ADMIN,GROUP_REPLICATION_STREAM,INNODB_REDO_LOG_ARCHIVE,INNODB_REDO_LOG_ENABLE,PASSWORDLESS_USER_ADMIN,PERSIST_RO_VARIABLES_ADMIN,REPLICATION_APPLIER,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SERVICE_CONNECTION_ADMIN,SESSION_VARIABLES_ADMIN,SET_USER_ID,SHOW_ROUTINE,SYSTEM_USER,SYSTEM_VARIABLES_ADMIN,TABLE_ENCRYPTION_ADMIN,XA_RECOVER_ADMIN ON *.* TO `root`@`localhost` WITH GRANT OPTION
)
Array
(
[Grants for root@localhost] => GRANT PROXY ON ``@`` TO `root`@`localhost` WITH GRANT OPTION
)
用户授权
格式:
GRANT ALL PRIVILEGES ON 数据库名.表名 TO '用户名'[@'主机名']
例子:
<?php $conn = mysqli_connect("localhost", "root","admin"); if ($conn) { echo "数据库连接成功\n"; } else { echo mysqli_connect_error(); } # SQL语句 $SQL = "GRANT ALL PRIVILEGES ON study.table1 TO 'user1'@'%'"; # 执行 $result = mysqli_query($conn, $SQL); # 查看是否执行成功 if ($result) { echo "SQL 语句执行成功!\n"; }else { echo mysqli_error($conn); } # 关闭连接 mysqli_close($conn); ?>登录后复制
撤销授权
REVOKE ALL PRIVILEGES ON 数据库名.表名 from '用户名'[@'主机名']
例子:
<?php $conn = mysqli_connect("localhost", "root","admin"); if ($conn) { echo "数据库连接成功\n"; } else { echo mysqli_connect_error(); } # SQL语句 $SQL = "REVOKE ALL PRIVILEGES ON study.table1 FROM 'user1'@'%'"; # 执行 $result = mysqli_query($conn, $SQL); # 查看是否执行成功 if ($result) { echo "SQL 语句执行成功!\n"; }else { echo mysqli_error($conn); } # 关闭连接 mysqli_close($conn); ?>登录后复制
刷新权限
格式:
FLUSH PRIVILEGES
注意事项
禁止 root 远程登录.
禁止 root 远程登录的原因:
root 是 MySQL 数据库的超级管理员. 几乎拥有所有权限, 一旦泄露后果非常严重
root 是 MySQL 数据库的默认用户. 如果不禁止远程登录, 则某些人可以针对 root 用户暴力破解密码