It was by no means one of the gold and silver ports used by the Spanish treasure fleets (those were in what is today Panama), but the city of Campeche, Mexico, was the main port of Yucatán from the 17th through the beginning of the 19th centuries. The city’s wealth came primarily from a plant used for dyeing textiles called palo de Campeche, salt from evaporation, and shipbuilding. The result was that the city was frequently attacked by pirates.
The most prominent of these were the Dutchman Laurens de Graaf, called Lorencillo, and Jean David Nau, called El Olonés. Of the latter, it was said:
He committed innumerable and famous stumbling against the Spanish viceroyalty of the mainland. In a terrible storm, he lost his ship on the coast of Campeche. All the men were saved, but, arriving on land, the Spanish persecuted them by killing most of them, and also hurting the Olonés. Not knowing this how to escape, he thought about saving his life through a ploy: he took several handfuls of sand and mixing it with the blood of his own wounds he smeared his face and other parts of his body. Then, hiding with great skill among the dead, he remained motionless until the Spaniards left the field of struggle. Since they were gone, he retired to the forest, sold his wounds and took care of them until heal and then headed to the City of Campeche perfectly disguised. In the city, he spoke with certain slaves to whom he promised freedom in case they obeyed him. They accepted their promises and stealing a canoe at night, threw themselves into the sea with the Olonés.
Other pirates included Sir Francis Drake, John Hawkins, Cornelius Jol, Portuguese Bartholemew, Jacobo Jackson, Michel de Grandmont, Henry Morgan, and finally Jean Lafitte, who helped Andrew Jackson win the 1815 Battle of New Orleans during the War of 1812.
What the Spanish and the people of Campeche did was to fortify their city, surrounding the center with walls on all sides and putting separate fortifications on the north and south.
With independence from Spain, the pirate menace eventually abated. But many of the walls (baluartes) that surrounded the city still exist and are walkable. You can also visit the two large fortresses that protect Campeche on either side.