A one-step electrodeposition method was used to construct a superhydrophobic coating on the surface of magnesium alloy. Using magnesium nitrate and ethanol solution of stearic acid as electrolyte, four groups of electrolytes with different ratios of stearic acid and magnesium nitrate were designed to explore the influence of electrolyte to the process. The contact angles of the four coating samples were 136.4 ± 5.8°, 152.7 ± 2.8°, 156.2 ± 4.9° and 155.1 ± 4.4° The addition of magnesium nitrate in the solution increased the hydrogen evolution reaction in the electrodeposition process, which was not conducive to electrodeposition process. During the deposition process, the sample prepared in a molar ratio of stearic acid to magnesium nitrate of 10:1 had the best corrosion resistance, and its corrosion circuit density was 3.74 × 10−8 A/cm2, far lower than the corrosion current density of the magnesium alloy substrate.