A 17-year-old Cincinnati serve-and-volleyer threw a wild Serena scare into the night at Arthur Ashe Stadium.

Caty McNally, the doubles partner of 15-year-old Coco Gauff and a wild-card entry, played with a reckless abandon and the 111th-ranked player looked poised to post one of the great upsets in U.S. Open history. Then reality set in.

Williams, seeking a record 24th Grand Slam, needed all her reserves to fend off McNally and rally from a set down to post a 5-7, 6-3, 6-1 second-round triumph in a Wednesday night match ending at midnight.

“She really came out and played really well and showed no fear,’’ the eighth-seeded Williams said. “She had absolutely nothing to lose and she played like that.’’

The Ashe crowd rooted for McNally early but then cheered Serena during her comeback. McNally presented a different look than what Serena is familiar with. She patterns her game after Roger Federer, using sliced backhands and attempting to serve and volley. She also rattled Williams by coming up to the service line on Serena’s second serve.

“It was unbelievable,’’ said McNally, playing a top-20 player for the first time in her career. “I felt good on the big stage. I wasn’t that nervous.’’

Instead of the Maria Sharapova cakewalk of the first round, Williams looked anguished for a set-and-a-half before she punctured the young American, who may have a bright future.

“I couldn’t play too much worse,’’ Serena said. “At that point, I’m making so many errors off my forehand. I wasn’t practicing like that at all. I was just trying to let Serena come through a bit. I survived tonight, but I’m not too pleased with the way I played at all. I made too many errors, but it’s OK. I’m alive and still here.’’

Asked if she feels a big test is good going forward, Williams sounded mixed. With the match so tight, Serena spent the last hour of the match fist-pumping and shrieking her way to victory.

“I think it’s great because I want to be able to win matches where I’m not playing my best and come through,’’ Williams said. “I need to be tested, I guess it sounds scripted. Actually, I’d rather not be tested in every match. But that doesn’t happen, so it’s important for me to have those really rough, rowdy matches.’’

At 5-5 in the first set, McNally broke Serena’s serve. At 15-40, McNally belted a service return and Serena sprayed it wide for a 6-5 lead. McNally fell behind love-40 but rallied with panache to close it out with two straight service winners.

Serena was still battling herself early in the second set before taking over, especially with a third-set demolition.

In the second set, Serena was on the verge of closing it out, up 5-2, but McNally staved off four set points on her serve. McNally held with beautiful net play — two terrific defensive volleys to eke out the point and make it 5-3.

Serena didn’t get rattled and closed out the set on her serve with a key ace at 30-30. She wound up winning seven of the last eight games.

“It was definitely something to get used to,’’ Williams said of McNally’s style. “You don’t play players like her that have such full games.”