The same glue I use for frags, the thick stuff from Bulk Reef Supply. The magnets are coated in plastic so there's no risk of erosion. We gouged a magnet shpaed hole into the rock with a dremmel tool, glued it in, and put a few magnets on the outside I glass.

Don't underestimate the weight requirement - remember not just the rock weight, but the coral you'll be putting on it! One of my rock shelves tips annoyingly to the side.