is magnesium anode or cathode?
The magnesium corrodes instead of the iron because it is more easily oxidized (it is above iron in the table of reduction potentials). The iron becomes the cathode at which oxygen is reduced to water leaving the iron unattacked. The more easily oxidized metal, in this case magnesium, is called a sacrificial anode.