Formation of translationally cold MgH + and MgD + molecules in an ion trap