Is flagyl safe to take?

I was spotting so I went to the doctors and they said I have a bacterial vaginosis. She prescribed me flagyl. Its 500mg twice a day. I've never taken medication while pregnant but I've heard that having a infection while pregnant could cause premature labor.