Solid base catalysts are widely used in the chemical industry owing to their advantages of environmental friendliness and easy separation. However, their application is limited by basic site aggregation and poor stability. In this study, we report the preparation of magnesium (Mg) single-atom catalysts with high activity and stability by a sublimation-trapping strategy. The Mg net was sublimated as Mg vapor at 620 °C, subsequently transported through argon, and finally trapped on the defects of nitrogen-doped carbon derived from metal-organic framework ZIF-8, producing Mg1/NC. Because of the atomically dispersed Mg sites, the obtained Mg1/NC exhibits high catalytic activity and stability for Knoevenagel condensation of benzaldehyde with malononitrile, which is a typical base-catalyzed reaction. The Mg1/NC catalyst achieves a high efficiency with a turnover frequency of 49.6 h-1, which is much better than that of the traditional counterpart MgO/NC (7.7 h-1). In particular, the activity of Mg1/NC shows no decrease after five catalytic cycles, while that of MgO/NC declines due to the instability of basic sites.