我试图在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, $admin_account, $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';", $dbName, $dbUsername),
/* 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';
然后连接正常,一切都很好.
请帮忙.
*编辑:我已经删除了交易行为,将它从实时代码中删除并不能解决问题.看来密码字符串在PHP和MySQL之间被更改了吗?
解决方法:
通常,事务控制命令与DML命令一起使用,例如-INSERT,UPDATE和DELETE.如果在创建表或删除表时使用它们,则结果可能取决于自动提交模式.默认情况下,mysql自动将更改永久提交到database.
您还将在MySQL Internals Manual中发现带有非事务引擎的DDL语句和操作未在事务列表中“注册”.检查您正在使用的引擎(InnoDB或MyISAM).
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;
在表mysql.user中,“客户”已创建,没有权限.
在mysql.db表中,已使用权限创建了“客户”和“管理员”.
标签:user-permissions,mysql,php,mysql,database,sql
来源: https://codeday.me/bug/20191211/2106705.html