我试图在PHP7.0的运行时创建一个MysqL用户帐户,授予对单个数据库的访问权限,该数据库也在运行时创建.
我目前正在使用:
$this->MysqLi = new MysqLi('localhost',$admin_account,$password);
$setup = [
/* create database */
sprintf('CREATE DATABASE IF NOT EXISTS %s;',$dbName),/* grant admin */
sprintf("GRANT ALL PRIVILEGES ON %s.* TO '%s'@'%s' WITH GRANT OPTION;",$dbName,$admins_remote_ip),/* add user */
sprintf("CREATE USER '%s'@'localhost' IDENTIFIED BY '%s';",$dbUsername,$dbPassword),/* THIS WAS ADDED AS A FIX TO NO AVAIL */
sprintf("UPDATE MysqL.user SET password=PASSWORD('%s') WHERE user='%s'",$dbPassword,$dbUsername),/* grant retailer*/
sprintf("GRANT ALL PRIVILEGES ON %s.* TO '%s'@'localhost';",/* flush */
"FLUSH PRIVILEGES;",];
foreach ($setup as $query) {
if (false == $this->MysqLi->query($query)) {
$error = $this->MysqLi->error;
return $error;
}
}
所有语句执行都没有错误,所有内容似乎都在应有的位置,但是非管理员用户会遇到MysqL-> error;
Connect Failed: Access denied for user ‘dbUsername’@’localhost’ (using password: YES)
如果我这样做;
> MysqL -u admin_account -p
update MysqL.user set password=PASSWORD('dbPassword') where user='dbUsername';
然后连接正常,一切都很好.
请帮忙.
最佳答案
通常,事务控制命令与DML命令一起使用,例如-INSERT,UPDATE和DELETE.如果在创建表或删除表时使用它们,则结果可能取决于自动提交模式.默认情况下,mysql自动将更改永久提交到database.
如果您阅读文章“ How Popular Databases Deal with DDL Commands in Transactions”,您会注意到并非所有数据库都能够回滚DDL更改.它们确实支持DDL命令的事务性,但不支持所有命令.
您还将在MySQL Internals Manual中发现带有非事务引擎的DDL语句和操作未在事务列表中“注册”.检查您正在使用的引擎(InnoDB或MyISAM).
我仅在sql中复制了该示例,发现FLUSH一定是原因.这已在手册中确认. The FLUSH statement causes an implicit commit.
START TRANSACTION;
CREATE DATABASE IF NOT EXISTS StackOverflow;
GRANT ALL PRIVILEGES ON StackOverflow.* TO 'admin'@'%' WITH GRANT OPTION;
CREATE USER 'customer'@'localhost' IDENTIFIED BY 'customer';
GRANT ALL PRIVILEGES ON StackOverflow.* TO 'customer'@'localhost';
COMMIT;
FLUSH PRIVILEGES;