Rats and mice are indeed attracted to compost piles since they contain food and rats and mice do look for food and can smell it.
You can set out mouse traps or rat traps near the compost bins to trap and kill rats and mice to keep them away from the compost bin.
Keeping the compost bin covered to prevent the smell of the food from the compost bin can help keep mice and rats away from your compost bin too.
Also leave out meat because meat creates a smell that easily attracts rats and mice so by keeping meat out of the compost bin will lessen the attraction of rats.
You should also keep your compost pile or bin in a dry, shady spot with a convenient water source nearby and the rats and mice will usually stay away.