How can I connect to a new leader when my database leader switch happens? #11608
Replies: 1 comment 2 replies
-
I would stop right there. you do not want to make a new engine, this would be extremely complicated. All of that When you have a server setup with leader/follower and fallback, you provide your applications a single ip address using one of two tools: haproxy or proxysql. These are extremely robust and mature tools that will perform the switchover task a million times better than any client side python code can. then in your that way, you write zero code and you are using industry standard approaches. anything else is wasting time. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, all, my question and background are as below:
Background:
My backend database is MySQ group replication with group_replication_single_primary_mode = ON, which means only single node(we call it leader or primary node) is allowed to write data at a specific time, but any node can become the new leader as time goes by and the old leader may be in the crashed state for some time.
Question:
Because the leader node may change, I need to create the async engine after I detect the leader switch. However, I don't want the frontend user(here it is FastAPI restful client) to receive the "Lost connection to MySQL server during query" exception. So when leader switch happens I want to:
However, even I add the parameter pool_pre_ping=True to function create_async_engine, I fail to get a connection from the new created async engine in the first request after the leader switch, it seems the connection will be recreated automatically and try to connect the old leader but the old leader is already down. The error is
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on '192.168.0.1'"), the detailed error stack is at the very end after the code.Hope can get some help, thanks very much!
Below is my code:
Beta Was this translation helpful? Give feedback.
All reactions