In this work, to improve its photocatalytic activity, g-C3N4 was doped with different magnesium salts (g-C3N4, MgCl2-g-C3N4, MgSO4-g-C3N4, Mg(CH3COO)2-g-C3N4, Mg(OH)2-g-C3N4 and Mg3(PO4)2-g-C3N4). The sample that provided the best photocatalytic degradation efficiency was MgSO4-g-C3N4, with a photodynamic parameter of 26.36 × 10-3 min−1. During the photocatalytic degradation process, methylene blue is oxidized by O2•−, h+ and •OH. •OH is the key ROS for the degradation of methylene blue. The density functional theory (DFT) calculation results demonstrate that sulfate radicals enhance the adsorption capacity of methylene blue. This work can provide new ideas for the degradation of cationic dyes.