MySQL sql以只读模式打开连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10240890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sql open connection in read only mode
提问by gtiwari333
This question comes out of curiosity. I searched google for this but didn't get a clue.
这个问题是出于好奇。我在谷歌上搜索了这个,但没有得到任何线索。
Suppose a user has full read/write access to MySQL database. I am wondering, is there any way (some parameter in connection string) to connect to database by the same username and password in read-only mode?
假设用户具有对 MySQL 数据库的完全读/写访问权限。我想知道,有没有办法(连接字符串中的某些参数)以只读模式通过相同的用户名和密码连接到数据库?
I want this withoutchanging this user's permissions because the same user might require write permission too at some other time. This would be useful (if possible) to prevent accidental modification to database.
我希望在 不更改此用户权限的情况下执行此操作,因为同一用户在其他时间也可能需要写入权限。这对于防止意外修改数据库很有用(如果可能)。
采纳答案by Jon Kloske
The best solution here is to create another account on the mysql server with readonly permissions, and connect using that.
这里最好的解决方案是在 mysql 服务器上创建另一个具有只读权限的帐户,并使用它进行连接。
回答by hgulyan
The answer to your question is
你的问题的答案是
No, there's no way to specify read-only access in the connection string.
不,无法在连接字符串中指定只读访问。
Alternatives are
替代方案是
1.Create sql user with read permission
1.创建具有读权限的sql用户
MVC3 Read-Only MySql Connection String
2.create views or stored procedures with permissions checking logic in them
2.创建带有权限检查逻辑的视图或存储过程
MS SQL Grant permission to only a view
MS SQL仅授予视图权限
MySQL Grant a user permission to only view a mysql view
MySQL授予用户仅查看 mysql 视图的权限
3.Implement permissions layer in your business logic
3.在你的业务逻辑中实现权限层
Good Luck!
祝你好运!
回答by Isaac
Depending on your use case and what control you have you could have the code call "set transaction read only" immediately after connecting, or use the --init-command parameter on connect. This worked for a testing use case we have,
根据您的用例和您拥有的控制权,您可以在连接后立即调用“设置事务只读”,或者在连接时使用 --init-command 参数。这适用于我们拥有的测试用例,
Here's the set transaction doc: https://dev.mysql.com/doc/refman/5.7/en/set-transaction.html, similarly you can also set it as a session variable if that makes a difference https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_transaction_read_only.
这是设置的事务文档:https: //dev.mysql.com/doc/refman/5.7/en/set-transaction.html,同样,如果有区别,您也可以将其设置为会话变量https://dev .mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_transaction_read_only。