In this study, films with different fluorine contents were prepared on an AZ31 magnesium alloy by using plasma electrolytic oxidation to study the corrosion resistance and cytocompatibility of the alloy. The morphology of the coating surface, phase, and chemical elements were characterized by scanning electron microscopy (SEM), X-ray diffraction (XRD), and energy-dispersive X-ray spectrometry (EDS). The changes in the corrosion resistance with different fluorine contents were investigated by electrochemical experiments, hydrogen evolution, and long-term immersion tests. In addition, murine fibroblast L-929 cells were adopted for in vitro cytotoxicity tests using the cell counting kit (CCK)-8 assay, and the morphology of the cells was observed simultaneously by inverted microscopy. The results showed that the main form of the fluorine ions in the plasma electrolytic oxidation coatings was magnesium fluoride (MgF2). In addition, the corrosion resistance and cytocompatibilities of the coatings were improved by the addition of fluoride ions. When the content of potassium fluoride reached 10 g/L, the cell compatibility and corrosion resistance were the best, a finding which provides a basis for the clinical applications of the AZ31 magnesium alloy in the biomedical field.