I like the other answers but one thing I did not find is WHY people say it's a bad thing to stay longer than 2 years in any tech position. I heard of this as well and several co-workers in the past practiced this.
The first problem is that in a lot of tech positions, the job turns redundant/maintenance mode after a while. For example, you might build a website using hot new technologies and frameworks and practices, then you go into bug repair or adding small features or adjustments. Overall you're doing nothing new. With that said, some folks worry they won't have a good resume after a while or they will get bored.
The second problem is that in some cases things yoyo. Take for example Cobol programmers. They were the hottest thing in some early tech era. A lot of folks got retirements and a big career doing it. New generation of people don't know how to maintain/develop on these systems. I know older tech Cobol people who retired and get called back into the job to discuss certain things. With that said, if you stay someone to become ancient, you might become more valuable in the future since you outlasted everyone else and the company wants to maintain a large legacy code base for whatever reason. However, this is highly unlikely, but does happen. I known a situation 5 years ago where only one guy knew how the systems and he quit, and they had to call him. He didn't get paid for it.
The third problem is pay growth. The usual tech companies set a base, then give 2% growth. Eventually new people will get paid near or above you. I had that happen to myself where I got into a company and after my yearly growth, I got stuck getting paid less than my coworkers yet I was the most senior doing most of the work. I think this is a big one most people suggest not to stay more than 2 years unless you make a big jump in pay. You're probably worth 10-20% more if you stay more than 2 years at a company. At year 3, you're probably 20-25% underpaid, etc. Some rare circumstances like point #2 might make you more valuable but otherwise normal people won't see that huge of a pay growth without getting into management.
So consider those three things. I think the biggest is the third and first point. Most interviews ask about what current technologies you use, and if you stay in a company long enough you might not get exposed to those new technologies and on top of that a lot of places ask how much you're making now. If you're underpaid by a lot and want to make a huge jump, that might be a consideration. Overall it's not bad to stay in one place a long time if you are comfortable with it.