A hallmark of addiction is the loss of control over drug intake, which is seen in only a fraction of those exposed to stimulant drugs such as cocaine. The cellular mechanisms underlying vulnerability or resistance to compulsive drug use remain unknown. We found that individual variability in the development of highly motivated and perseverative behavior toward cocaine is associated with synaptic plasticity in medium spiny neurons expressing dopamine D2 receptors (D2-MSNs) in the nucleus accumbens (NAc) of mice. Potentiation of glutamatergic inputs onto indirect pathway D2-MSNs was associated with resilience toward compulsive cocaine seeking. Inhibition of D2-MSNs using a chemicogenetic approach enhanced the motivation to obtain cocaine, whereas optogenetic activation of D2-MSNs suppressed cocaine self-administration. These results indicate that recruitment of D2-MSNs in NAc functions to restrain cocaine self-administration and serves as a natural protective mechanism in drug-exposed individuals.