The family of layered Mn+1AXn compounds provides a large class of materials with applications ranging from magnets, to high-temperature coatings, to nuclear cladding. In this work, we employ a density-functional theory based discovery approach to identify a large number of thermodynamically stable Mn+1AXn compounds, where n = 1, M = Sc, Ti, V, Cr, Zr, Nb, Mo, Hf, Ta; A = Al, Si, P, S, Ga, Ge, As, Cd, In, Sn, Tl, Pb; and X = C, N. We calculate the formation energy for 216 pure M2AX compounds and 10,314 solid solutions, (MM )2(AA )(XX ), relative to their competing phases. We find that the 49 experimentally known M2AX phases exhibit formation energies less than 30 meV/atom. Among the 10,530 compositions considered, 3,140 exhibit formation energies below 30 meV/atom, most of which have yet to be experimentally synthesized. A significant subset of 301 compositions exhibits strong exothermic stability in excess of 100 meV/atom, indicating favorable synthesis conditions. We identify empirical design rules for stable M2AX compounds. Among the metastable M2AX compounds are two Cr-based compounds with ferromagnetic ordering and expected Curie temperatures around 75K. These results can serve as a map for the experimental design and synthesis of previously unknown M2AX compounds.