MySQL 临时视图可能吗?
这是后续:
我正在做这样的视图(我确信这可以优化):
db.php 被包含:
$conn = mysql_connect("localhost","directory","dghgffhghf") or die(mysql_error());
mysql_select_db("directory", $conn) or die(mysql_error());
mysql_query("CREATE or REPLACE VIEW {$user}_rooms AS SELECT * FROM rooms WHERE palace = '$user'") or die(mysql_error());
mysql_query("CREATE or REPLACE VIEW {$user}_users AS SELECT * FROM users WHERE palace = '$user'") or die(mysql_error());
mysql_query("CREATE or REPLACE VIEW {$user}_servers AS SELECT * FROM servers WHERE palace = '$user'") or die(mysql_error());
mysql_query("CREATE or REPLACE VIEW {$user}_online_servers AS SELECT * FROM online_servers WHERE palace = '$user'") or die(mysql_error());
用户“目录”必须具有的位置SELECT
、CREATE VIEW
和 DROP
权限(由于或 REPLACE
需要 DROP
>)。我不希望他们拥有 DROP 权限,因为目录用户将位于 apache 拥有的 PHP 文件中,并且他们不使用 dir 对其进行限制 - 所以他们可以只需查看用户并通过即可。
另外,我不希望用户在实际上是 joe
时只使用 bob_rooms
。我只想为该特定连接上的该用户创建视图,并在连接消失后立即删除。我不能依赖用户自己DROP
视图。
此外,拥有一个自动删除的临时视图将消除使用或REPLACE
,这意味着我可以为用户获取奇怪的DROP
权限。
我想在 SQLlite 中它会很简单:
CREATE TEMP VIEW ...
SqlLite 可以做临时视图,但 MySQL 不能?
This is following up on:
Allow users only certain information from database
I am doing views as such (I am sure this could be optimized):
db.php
that gets included:
$conn = mysql_connect("localhost","directory","dghgffhghf") or die(mysql_error());
mysql_select_db("directory", $conn) or die(mysql_error());
mysql_query("CREATE or REPLACE VIEW {$user}_rooms AS SELECT * FROM rooms WHERE palace = '$user'") or die(mysql_error());
mysql_query("CREATE or REPLACE VIEW {$user}_users AS SELECT * FROM users WHERE palace = '$user'") or die(mysql_error());
mysql_query("CREATE or REPLACE VIEW {$user}_servers AS SELECT * FROM servers WHERE palace = '$user'") or die(mysql_error());
mysql_query("CREATE or REPLACE VIEW {$user}_online_servers AS SELECT * FROM online_servers WHERE palace = '$user'") or die(mysql_error());
Where the user "directory" has to have SELECT
, CREATE VIEW
and DROP
permissions (DROP
is required because of the or REPLACE
). I do not want them to have DROP
permissions , as the directory user will be in a PHP file owned by apache, and they arent restricted to it using dir
- so they could just view the user and pass.
Also, I dont wan't a user to just use bob_rooms
when he is actually joe
. I only want the view to be created for ONLY that user at that specific connection, and DROP as soon as the connection is gone. I cannot rely on the user to DROP
the view on their own.
Also, having a temporary view that would auto-drop would eliminate the use of or REPLACE
which means I can take odd DROP
permissions for the user.
I guess in SQLlite it would be as easy as:
CREATE TEMP VIEW ...
SqlLite can do temporary views, but MySQL can't?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道临时视图,但支持临时表。因此,您可以创建一个与镜像表具有相同结构的临时表,例如用于房间的 temp_bob_rooms 。然后将实际表中的选择插入到临时表中,并进行适当的限制,例如:
然后,当用户完成会话时,临时表将自动删除,因此目录用户不再需要 DROP 访问权限。您可以在此处阅读有关临时表的更多信息:
http://www.tutorialspoint.com /mysql/mysql-temporary-tables.htm
这种方法的缺点是,在临时表的会话打开期间,临时表不会用插入“真实”表的数据进行更新。
另一种方法可能是简单地编写一个脚本,为每个真实用户生成一个 mysql 用户、适当的视图,并将这些视图的权限授予适当的用户。这种方法的唯一缺点是您的 db.php 文件对任何人都不再有用,因为每个用户都必须使用正确的密码和用户名创建自己的连接。
I don't know about temporary views, but there is support for temporary tables. So you could do something where you create a temporary tables with the same structures as the tables they are mirroring, e.g. temp_bob_rooms for rooms. Then insert into the temp table a selection from the real table, with your appropriate restrictions, e.g.:
Then when the user is done with his session, the temp table will be dropped automatically, so the directory user doesn't need DROP access any longer. You can read some more about temporary tables here:
http://www.tutorialspoint.com/mysql/mysql-temporary-tables.htm
The downside to this approach is that the temporary table won't be updated with data inserted into the 'real' table during the time the session with the temporary table is open.
Another approach might be to simply write a script that would generate a mysql user for each real user, the appropriate views, and grant permissions to those views to the appropriate users. The only downside to that approach is that your db.php file wouldn't be useful to anyone anymore, because each user would have to create his or her own connection with the correct password and user name.
您可以使用以下语法从查询一步创建临时表:
临时表的注意事项:
You can create a temporary table from a query in one step with the syntax:
The caveats to temporary tables: