sql open connection in read only mode sql open connection in read only mode mysql mysql

sql open connection in read only mode


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!


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


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.