Israeli law requires that an armed security guard be on duty at all times in all educational environments. No person can enter a protected school without being physically checked. Most Israelis have had military training, so in a school environment, there are people who have weapons and know how to use them to defeat an attacker. They also have the tools to question any visitor in a way where a simple set of questions will in most cases allow them to determine a good or bad intent.
Weapons alone in the hands of school security officials or teachers will never prevent an attack, but they may certainly delay it from escalating, ending it far more quickly than simply waiting for the nearest police backup to arrive at the scene. But intense training and coordination is required.
Access should be strictly limited, perimeters should be protected with cameras monitored while students are on the property, and as in Israel, no strangers or people without good reason to be on campus should be allowed into any school.
Access control should include searches and the use of technology such as metal detectors, like those used when entering a government building.
Protecting schools and training their personnel to prevent and respond to attacks should be a priority in all educational systems around the world.