Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql open connection in read only mode

Suppose a user has full read/write access to MySQL database. 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.

like image 283
gtiwari333 Avatar asked Apr 20 '12 05:04

gtiwari333


People also ask

How do I turn off read-only mode in SQL Server?

In the Database Properties - DatabaseName window, click on Options in the Select a page pane on the left. Scroll down to the State properties section. Check the property "Database Read-Only". This should be set to False.

What is ApplicationIntent ReadOnly?

When ApplicationIntent=ReadOnly, the client requests a read workload when connecting. The server enforces the intent at connection time, and during a USE database statement.

How do I know if MySQL database is read-only?

SELECT name, is_read_only FROM sys. databases WHERE name = 'MyDBNAme' GO --returns 1 in is_read_only when database is set to read-only mode.


3 Answers

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!

like image 95
hgulyan Avatar answered Nov 18 '22 18:11

hgulyan


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

like image 38
Jon Marnock Avatar answered Nov 18 '22 18:11

Jon Marnock


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.

like image 7
Isaac Avatar answered Nov 18 '22 18:11

Isaac