Translation
(4) The Book On The Mosques And The Congregations
كتاب المساجد والجماعات
(13) Chapter: Supplication When Entering The Mosque
(13) باب الدُّعَاءِ عِنْدَ دُخُولِ الْمَسْجِدِ ‏.‏
Sunan Ibn Majah 772 
It was narrated that Abu Humaid As-Sa'idi said:
"The Messenger of Allah said: 'When anyone of you enters the mosque, let him send peace upon the prophet, then let him say: "Allahummaftah li abwaba rahmatika (O Allah, open to me the gates of Your mercy)." And when he leaves, let him say: "Allahumma inni as'aluka min fadlika. (O Allah, I ask of you from Your bounty)."
حَدَّثَنَا عَمْرُو بْنُ عُثْمَانَ بْنِ سَعِيدِ بْنِ كَثِيرِ بْنِ دِينَارٍ الْحِمْصِيُّ، وَعَبْدُ الْوَهَّابِ بْنُ الضَّحَّاكِ، قَالاَ حَدَّثَنَا إِسْمَاعِيلُ بْنُ عَيَّاشٍ، عَنْ عُمَارَةَ بْنِ غَزِيَّةَ، عَنْ رَبِيعَةَ بْنِ أَبِي عَبْدِ الرَّحْمَنِ، عَنْ عَبْدِ الْمَلِكِ بْنِ سَعِيدِ بْنِ سُوَيْدٍ الأَنْصَارِيِّ، عَنْ أَبِي حُمَيْدٍ السَّاعِدِيِّ، قَالَ قَالَ رَسُولُ اللَّهِ ـ صلى الله عليه وسلم ـ ‏ "‏ إِذَا دَخَلَ أَحَدُكُمُ الْمَسْجِدَ فَلْيُسَلِّمْ عَلَى النَّبِيِّ ـ صلى الله عليه وسلم ـ ثُمَّ لْيَقُلِ اللَّهُمَّ افْتَحْ لِي أَبْوَابَ رَحْمَتِكَ ‏.‏ وَإِذَا خَرَجَ فَلْيَقُلِ اللَّهُمَّ إِنِّي أَسْأَلُكَ مِنْ فَضْلِكَ ‏"‏ ‏.‏
Grade :  Sahih (Darussalam)
Reference : Sunan Ibn Majah 772
In-book reference : Book 4, Hadith 38
English translation : Vol. 1, Book 4, Hadith 772