The Messenger of Allah ﷺ said, "When one of you enters the mosque, let him say, 'O Allah, open for us the gates of Your mercy.' And when he leaves, let him say, 'O Allah, I ask You from Your bounty.'"
قال حدثنا أبو عامر قال حدثنا سليمان بن بلال عن ربيعة بن أبي عبد الرحمن عن عبد الملك بن سعيد بن سويد الأنصاري قال سمعت
قال رسول الله ﷺ إذا دخل أحدكم المسجد فليقل اللهم افتح لنا أبواب رحمتك وإذا خرج فليقل اللهم إني أسألك من فضلك
Its isnad is sahih, its narrators are well known, and the hadith was narrated by Muslim, 1/494, no. 713, in "The Travelers," on what one says upon entering the masjid, by Nasai, 2/53, no. 729, in similar fashion, and by Ibn Majah, 1/254, no. 772.