Gut dysbiosis and metabolic abnormalities have been implicated in HIV infection. However, the exact causal relationships among the gut microbiota, metabolites, and HIV infection remain poorly understood. Our study involving Mendelian randomization (MR) and mediation analysis aims to unveil these causalities. Genetic instrumental variables for the gut microbiota were retrieved from MiBioGen consortium (n = 18,340). Metabolism-related genetic variants were sourced from the CLSA cohort (n = 8299). GWAS summary statistics for symptomatic HIV infection were derived from the FinnGen study (n = 309,154), and the UK Biobank (n = 208,808). We performed the bidirectional two-sample MR to assess causalities with the inverse-variance weighted (IVW) method as the primary analysis. Moreover, we executed a mediation analysis using two-step MR methods. Compared to the causal effects of HIV infection on gut microbiota (or metabolites), those of gut microbiota (or plasma metabolites) on the risk of HIV infection were more substantial. Phylum Proteobacteria (OR: 2.114, 95% CI 1.042-4.288, P = 0.038), and genus Ruminococcaceae UCG013 (OR: 2.127, 95% CI 1.080-4.191, P = 0.029) exhibited an adverse causal effect on HIV infection, whereas genus Clostridium sensu stricto 1(OR: 0.491, 95% CI 0.252-0.956, P = 0.036) and family Erysipelotrichaceae (OR: 0.399, 95% CI 0.193-0.827, P = 0.013) acted as significant protective factors for HIV. The salicyluric glucuronide level (OR = 2.233, 95% CI 1.120-4.453, P = 0.023) exhibited a considerably adverse causal effect on HIV infection. Conversely, the salicylate-to-citrate ratio (OR: 0.417, 95% CI 0.253-0.688, P = 0.001) was identified as a protective factor for HIV. We identified only one bidirectional causality between 1-palmitoyl-GPI and HIV infection. Mechanistically, genus Haemophilus mediated the causal effects of three phospholipids on HIV infection risk: 1-palmitoyl-GPI (mediation proportion = 33.7%, P = 0.018), 1-palmitoyl-2-arachidonoyl-GPI (mediation proportion = 18.3%, P = 0.019), and 1-linoleoyl-2-linolenoyl-GPC (mediation proportion = 20.3%, P = 0.0216). Additionally, 5-Dodecenoylcarnitine (C12:1) mediated the causal effect of genus Sellimonas on the risk of HIV infection (mediation proportion = 13.7%, P = 0.0348). Our study revealed that gut microbiota and metabolites causally influence HIV infection risk more substantially than the reverse. We identified the bidirectional causality between 1-palmitoyl-GPI (16:0) and HIV infection, and elucidated four mediation relationships. These findings provide genetic insights into prediction, prevention, and personalized medicine of HIV infection.