How alien pests invade new areas has always been a hot topic in invasion biology. The spread of the Bactrocera dorsalis from southern to northern China involved changes in food sources. In this paper, in controlled conditions, we take Bactrocera dorsalis as an example to study how plant host transformation affects gut bacteria by feeding it its favorite host oranges in the south, its favorite host peaches and apples in the north, and feeding it cucumbers as a non-favorite host plant, thereby further affecting their fitness during invasion. The result showed that, after three generations of feeding on cucumbers, Bactrocera dorsalis took longer to develop as a larva while its longevity and fecundity decreased and pre-adult mortality increased. Feeding it cucumbers significantly reduced the overall diversity of gut microbiota of Bactrocera dorsalis. The relative abundance of Enterobacter necessary for survival decreased, while the Empedobacter and Enterococcus increased, resulting in decreased carbohydrate transport and metabolism and increased lipid transport and metabolism. Feeding Bactrocera dorsalis Empedobacter brevis and Enterococcus faecalis resulted in a 26% increase in pre-adult mortality and a 2-3 d increase in adult preoviposition period (APOP). Additionally, Enterococcus faecalis decreased the longevity of female and male adults by 17 and 12 d, respectively, and decreased fecundity by 11%. We inferred that the shifted plant hosts played an important role in posing serious harm to Bactrocera dorsalis invading from the south to the north. Therefore, after an invasion of Bactrocera dorsalis into northern China, it is difficult to colonize cucumbers for a long time, but there is still a risk of short-term harm. The findings of this study have established that the interactions between an insect's food source and gut bacteria may have an important effect on insect invasions.