Although emerging evidence shows that gut microbiota-mediated metabolic changes regulate intestinal pathogen invasions, little is known about whether and how gut microbiota-mediated metabolites affect pathogen infection in the distal organs. In this study, untargeted metabolomics was performed to identify the metabolic changes in a subacute ruminal acidosis (SARA)-associated mastitis model, a mastitis model with increased susceptibility to Staphylococcus aureus (S. aureus). The results showed that cows with SARA had reduced cholic acid (CA) and deoxycholic acid (DCA) levels compared to healthy cows. Treatment of mice with DCA, but not CA, alleviated S. aureus-induced mastitis by improving inflammation and the blood-milk barrier integrity in mice. DCA inhibited the activation of NF-κB and NLRP3 signatures caused by S. aureus in the mouse mammary epithelial cells, which was involved in the activation of TGR5. DCA-mediated TGR5 activation inhibited the NF-κB and NLRP3 pathways and mastitis caused by S. aureus via activating cAMP and PKA. Moreover, gut-dysbiotic mice had impaired TGR5 activation and aggravated S. aureus-induced mastitis, while restoring TGR5 activation by spore-forming bacteria reversed these changes. Furthermore, supplementation of mice with secondary bile acids producer Clostridium scindens also activated TGR5 and alleviated S. aureus-induced mastitis in mice. These results suggest that impaired secondary bile acid production by gut dysbiosis facilitates the development of S. aureus-induced mastitis and highlight a potential strategy for the intervention of distal infection by regulating gut microbial metabolism.