It was the highest monthly number of foreign tourists to Spain since current records began in 1995 and 7.1 percent higher than August last year, the tourism ministry said in a statement.
The number was swelled by foreign sun seekers avoiding Egypt and Turkey — popular budget holiday destinations hit by civil unrest this year — the ministry said.
In Spain, the northeastern region of Catalonia -- with its beaches and the cultural sites of Barcelona — benefitted most, with 2.3 million foreigners heading there.
Close behind were the nearby Balearic Islands with 2.1 million foreign visitors and the southern region of Andalusia with 1.1 million.
As Spain has struggled through the past two years of recession, with an unemployment rate currently above 26 percent, tourism has been a rare source of positive economic news.
It accounts for 11 percent of the economy, and Spain is the third-most visited country in the world, tied with China and behind only the United States and France, figures from the World Tourism Organisation show.
Spain's surging tourist numbers at the height of the summer holidays in August came most of all from three stronger European economies: two million from Britain, 1.8 million from France and 1.2 million from Germany.
Overall from January to August, 42.3 million foreign tourists came to Spain — 4.5 percent more than the same period last year and another record high, the ministry's data showed.