Abu Humayd or Abu Usayd reported,

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.'"

Azami 10989

Sahih

عن أبي حميد، أو أبي أسيد الأنصاري قال

قال رسول الله ﷺ: إذا دخل أحدكم المسجد فليقل: اللهم افتح لي أبواب رحمتك، وإذا خرج فليقل: اللهم إني أسألك من فضلك.

الأعظمي 10989صحيح
Contents
Reference
Commentary
Ayat
Collection
Jami al-Kamil
Book
Supplications, remembrances, and prayer upon the chosen Prophet ﷺ
Chapter
Supplications of adhan and prayer, and what is related to them
Section
What to say when wanting to enter the masjid and exit from it
Numbering
Jami al-Kamil 10989 (2nd edition)
Azami 10989