I think this one would be more appropriate:
SELECT COUNT(p.userid) nrusers FROM ( SELECT l.userid, MONTH(FROM_UNIXTIME(l.time)) FROM mdl_log l WHERE l.action = 'login' AND l.module = 'user' AND YEAR(FROM_UNIXTIME(l.time)) = 2018 GROUP BY l.userid, MONTH(FROM_UNIXTIME(l.time)) ) p
The reason we do not do count in first query is because in recent iterations MySQL insists that all fields in SELECT clause must be present in the GROUP BY clause as well. You can read more about that here: