Well, if you’ve only got one instance of this, and you can explain why you left in a way that frames it as a positive (e.g., you know what you want and are decisive), it won’t be a problem. We all make mistakes (just remember to network, so you have the opportunity to tell your story to employers).
But how long should you stick it out? It depends. Why do you want to leave? Are you seeing some gross ethical violations? Is it crushing your spirit? Are you being unfairly exploited?
If something is fundamentally wrong, then leave. Life is too short to be miserable at work, and you can overcome any negative consequences of this decision.
However, if you are just struggling with the transition into the new job, or only have problems with a few aspects of the work, pause and think things through. Don’t struggle alone: talk it through with mentors, peers, or a careers professional to get some perspective and figure out some ways forward.
And consider raising it with your manager (or their manager if your line manager is part of the problem). They should be invested in your success too but might not realise you are having difficulties.
The world of work isn’t easy: it throws up lots of problems. But if you face up to them, often they can be fixed. And learning the skill of fixing them will help you have a more productive and fulfilling career in the long run.
On a pragmatic note, if you do leave, you’ll ideally get an agreement that you’ll get a positive reference from the employer. And two years is a decent stint in a role. The average tenure in jobs is about 5 years, but there’s huge variance between industries and different demographics (the average tenure in tech is about 3 years for example).
Finally, whatever the outcome, take some time to reflect on lessons learned. What could you do to avoid a similar situation in the future?