The Messenger of Allah ﷺ said, "When one of you enters the mosque, let him say, 'O Allah, open for me the gates of Your mercy.' And when he leaves, let him say, 'O Allah, I ask You of Your bounty.'"
قال رسول الله ﷺ: إذا دخل أحدكم المسجد فليقل: اللهم افتح لي أبواب رحمتك، وإذا خرج فليقل: اللهم إني أسألك من فضلك.
Muslim narrated it in the Book of the Prayer of Travelers (713), from Yahya bin Yahya, who said, Sulayman bin Bilal informed us, from Rabia bin Abu Abdul-Rahman, from Abdul-Malik bin Saeed, from Abu Humayd, or Abu Usayd, and he mentioned it.Abu Dawud also narrated it (465) through the hadith of Abdul-Aziz bin Muhammad Darawardi, from Rabia bin Abu Abdul-Rahman, with the same chain, adding at the beginning of the hadith, "When one of you enters the masjid, let him send peace upon the Prophet ﷺ, then let him say..." and he mentioned the rest of it in the same way. This addition is sahih, as is shown in the collections on sending prayers upon the Prophet ﷺ.