title: MySQL Stored Procedure to create user
body: <p>I am trying to create a stored procedure to create user in mysql server 5.6.21 but it throws the following error:</p>
  
  <blockquote>
    <p>Error Code: 1445. Not allowed to set autocommit from a stored function
    or trigger.</p>
  </blockquote>
  
  <p>the code below:</p>
  
  <pre><code>CREATE PROCEDURE `add_User`(IN p_Name VARCHAR(45), IN p_Passw VARCHAR(200))
  BEGIN
  
      DECLARE flag_exist TINYINT(1) DEFAULT 0;
  
      SELECT COUNT(*)
      INTO flag_exist 
      FROM
          `mysql`.`USER`
      WHERE
          `mysql`.`USER`.`User` = p_Name;
  
      IF flag_exist = 0 THEN 
          CREATE USER p_Name;
          GRANT ALL PRIVILEGES ON *.* TO p_Name;
          SET PASSWORD FOR p_Name = PASSWORD (p_Passw);
          FLUSH PRIVILEGES;
  
          /* NO FOUNT
              INSERT INTO user VALUES
              ('localhost',p_Name,PASSWORD(p_Passw),'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
          */
      END IF;
  
  END
  </code></pre>