Cristiano Ronaldo scored after 18 and 70 minutes while Luka Modric (50) and Dani Carvajal (52) also struck to put the 10-times European champions in complete command on a freezing night in western Ukraine.
The temperature then soared quickly, on the field at least, as Shakhtar netted three times in the closing stages.
Brazilian midfielder Alex Teixeira converted a penalty after 77 minutes before Dentinho chested the ball into the net from a corner.
News that makes sense
Your trusted source for staying up-to-date with the world around you. Get free daily news updates and analysis, straight to your inbox.
Teixeira was again on target with an elegant drive in the 88th minute as the hosts gave Real a huge fright.
Real have 13 points from five matches, three ahead of Paris St Germain who also qualified by beating Malmo 5-0 in Sweden.
PSG are sure to finish second because they have an inferior head to head record against Real. Shakhtar and Malmo both have three points.
Ronaldo opened the scoring with a header and grabbed his second goal of the night on the rebound after his initial attempt was blocked by a defender.
The Portugal forward also played a part in the other two goals by the visitors before Shakhtar came storming back as Real conceded their first goals in this year's competition.
(Writing by Igor Nitsak; Editing by Tony Jimenez)
