To explore the association between magnesium levels and the odds of mild cognitive impairment (MCI). In this cross-sectional study of 1006 participants (≥55 years) from China, whole-blood magnesium concentration was measured using inductively coupled plasma mass spectrometry. MCI was diagnosed according to Petersen criteria using self-reported cognitive decline and a neuropsychological test battery, including the trail-making test-part B (TMT-B), auditory verbal learning test (AVLT), digit symbol substitution test (DSST), and verbal fluency test (VFT), which measured the assessment of executive, memory, attention, and language functioning, respectively. A logistic regression was used to assess the relationship between magnesium levels and MCI, and linear regression analyses were performed for the association between magnesium and cognitive function score. The MCI group had a significantly lower concentration of magnesium compared to the Non-MCI group (34.7 ± 9.8 vs. 36.7 ± 9.7, p = 0.017). After adjusting for covariates, a negative association was observed between magnesium levels and MCI. Compared with the lowest quartile (median: 25.4 mg/L), the odds ratio for MCI was 0.53 (95%CI 0.32-0.90) for the highest quartile (median: 48.4 mg/L), and there was an inverse dose-response relationship (p for trend = 0.009). In addition, higher levels of magnesium were positively correlated with VFT scores (β = 0.37, 95%CI = 0.11-0.62) and DSST scores (β = 0.50, 95%CI = 0.01~0.98) and negatively correlated with TMT scores (β = -1.73, 95%CI = -3.40--0.07) in the middle-aged and older adults. Whole-blood magnesium was inversely associated with the occurrence of MCI and positively associated with performance in neuropsychological tests assessing attention, executive, and language ability in middle-aged and older adults.