I started this as a comment on another answer, but then realized it would make a better answer than a comment.
I've been in law enforcement, and in the fire service, for coming up on 20 years. Along the way I've done a lot: jail officer, jail supervisor, training officer, deputy sheriff, patrol supervisor, SWAT team member, SWAT sniper, SWAT sniper team leader, SWAT trainer, armorer, firearms instructor, firefighter, fire lieutenant, emergency medical RESPONDER, EMR instructor, and on and on, with no end planned currently.
That being said, the last paragraph of another top answer holds the most weight. Back in the 60’s and 70’s, a push began to end the practice of institutionalizing the severely mentally ill. Instead of placing them in an environment in which they are safer, and society at large is safer, we started keeping the mentally ill in the community, on the premise that it was more humane.
Unfortunately, the result has been less compliance with treatment plans, self-medicating with illicit drugs and alcohol — which makes matters worse, and the public gets exposed to the homicidal outbursts that arise. Currently, it is next to impossible to have even the most extremely mentally ill people institutionalized until they have actually killed someone.
Until then, it's a revolving door of short two- or three-day to maybe a week or two stints of inpatient treatment, at the most, before release with a bottle of pills and an appointment at the local mental health office, which is overloaded with patients, or a 24-hour observation at the local ER and discharge instructions to contact the same overloaded mental health office for an appointment. Other times, the ER releases them after 15 minutes to free up the room.
Case in point: a little over a year ago, we were repeatedly dealing with a man who was showing signs of some advanced mental illness. He was seeing and hearing things that weren't there, patrolling his property with a gun (he lived in a residential area of quarter-acre lots with dozens of neighbors in all directions), threatening neighbors who he believed were spying on him and messing with him, and actually shooting at things that weren't there. He also called several times to report “burglaries,” claiming his neighbors and the government were listening to him and controlling his thoughts through his light sockets, and wanting his house swept for listening devices. We gathered up the evidence, took it to the judge, the judge found probable cause and issued a detention order for involuntary examination. We picked him up, took him to the ER, and we also seized his guns. The ER doctor released him about an hour later. When our admins questioned the doctor, we were basically told that we are just cops, and he is the doctor. Officially, he wrote a letter saying that he examined this man and found no signs of any mental health issues, and no sign of any form of dementia. We had exhausted all our options. The neighbors, knowing we had done everything we could, stopped reporting his behavior, instead living in fear of this man, and his family took us to court and we had to return his guns to them, who returned them to him.
Roughly nine months later, that letter from the doctor was included in a massive case file after he threatened a neighbor with a gun, fired off shots (hitting nothing), then barricaded himself in his home. Another detention order was issued, except this time he tried to murder me and five other deputies, forcing us to return fire to save our own lives. Our evidence tags were still attached to the guns he had prepositioned to use against us.
The next big issue pertinent to the discussion is psychotropic drugs and illegal drugs. I have been unable to find a mass shooter or a school shooter that was not prescribed some form of, or self-medicating with, a psychotropic drug, either a pharmaceutical or illegal variety. It is the common link that ties all of these shootings together.