3

I'm new to Rails and I have built an application connecting to MySQL using activerecord. I've setup read replica of master node. The problem is when my slave becomes master, I get following error in the application:

MySQL server is running with the --read-only option so it cannot execute this statement

which I guess is acceptable.

But my application keeps failing forever and doesn't recover until I restart the application. Is there any acitverecord config or MySQL connection property which I can use to recover from this scenario?

I'm using following config in my database.yml.

adapter: mysql2
encoding: utf8
url: <'DATABASE_URL'>
pool: 10
database: <'DATABASE_NAME'>
username: <'DATABASE_USER'>
password: <'DATABASE_PASS'>
reconnect: true 

Is there any configuration which I can add to handle the failover? Any suggestion to handle the scenario?

Thanks in advance.

Aniket Divekar
  • 344
  • 1
  • 13
  • Please take a look at https://github.com/bdurand/seamless_database_pool and if you still have doubt, please do get back with more details. – Surya Sep 25 '18 at 12:49
  • @Surya Thank you for the quick reply. I went through the documentation and what I understand from it is that I can easily direct my read and write queries to appropriate server (If I've misunderstood anything, please correct me) but I still don't understand how is it going to solve my problem wherein when the master/slave switch their roles, how will this library handle it. I've my connection pool created in application and in background roles of master and slave are switched. what exactly library does in this scenario? – Aniket Divekar Sep 25 '18 at 13:24
  • Maybe this can help [mysql failover: how to choose slave as new master?](https://stackoverflow.com/a/44418932/7654691) – R. Sierra Sep 25 '18 at 15:54

1 Answers1

1

I fixed the issue following this blog. Putting it here for reference so that anyone facing the issue can use it.

As per the blog, you'll override the ActiveRecord::ConnectionAdapters::ConnectionManagement adapter and write your own connection handling in case of errors.

Aniket Divekar
  • 344
  • 1
  • 13