BACKGROUND: Few studies have explored risk factors for poor mental health in Ugandan primary schools. This study investigated whether individual- and contextual-level school-related factors including violence from school staff and other students, connectedness to school and peers, as well as school size and urban/rural location, were associated with mental health difficulties in Ugandan children. We also examined whether associations between violence exposure at school and mental health were mediated by connectedness as well as whether associations were different for boys and girls. METHODS: The analytic sample consisted of 3,565 students from 42 primary schools participating in the Good Schools Study. Data were collected through individual interviews conducted in June and July 2012. Mental health was measured using the Strengths and Difficulties Questionnaire. Multilevel logistic regression was applied to investigate factors associated with mental health difficulties. RESULTS: Experiences of violence from school staff and other students in the past week were strongly associated with mental health difficulties (OR = 1.58, 95 % CI 1.31 to 1.90 and 1.81, 1.47 to 2.23, respectively). Children with a low school connectedness had 1.43 times (1.11 to 1.83) the odds of mental health difficulties compared to those with a high school connectedness. The OR comparing children never feeling close to other students at their school with those always feeling close was 1.86 (1.18 to 2.93). The effect of violence on mental health was not mediated through the connectedness variables. School size was not related to mental health difficulties, but attending an urban school increased the odds of mental health difficulties after accounting for other factors. We did not find evidence that the effect of one or more of the exposures on the outcome differed between boys and girls. CONCLUSIONS: These findings suggest that violence in school and low connectedness to school and peers are independently associated with mental health difficulties and interventions should address both concurrently. Extra support may be needed for students in urban schools. TRIAL REGISTRATION: Clinicaltrials.gov NCT01678846 . Registered 24 August 2012.