sql open connection in read only mode

27,487

Solution 1

The best solution here is to create another account on the mysql server with readonly permissions, and connect using that.

Solution 2

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

MVC3 Read-Only MySql Connection String

2. create views or stored procedures with permissions checking logic in them

MS SQL Grant permission to only a view

MySQL Grant a user permission to only view a mysql view

3. Implement permissions layer in your business logic

Good Luck!

Solution 3

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,

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.

Share:
27,487
gtiwari333
Author by

gtiwari333

🇳🇵 🇳🇵 🇳🇵 Blog: https://gtiwari333.com Linkedin: https://www.linkedin.com/in/gtiwari333 GitHub: https://github.com/gtiwari333

Updated on February 24, 2020

Comments

  • gtiwari333
    gtiwari333 over 4 years

    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?

    I want this without changing 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.