The Magnesium ion is an Alkaline Earth Metal and wants to lose the 2s orbital elections and become a +2 cation. Oxygen(2,6) has six valence electrons and needs two electrons to complete its octet (8) and hence contains a -2 charge. So it is an anion.
In magnesium oxide (MgO)
anion = O2−
cation = Mg2+