title: How do I create roles that grant access to a database for a new user in mariaDB?
body: <p>What is the proper way to create roles, then assign new users to the roles to grant access to the desired database?</p>
  
  <p>It's not working for me the way I expected.</p>
  
  <p>If I try to make a read/write role and a read only role, then grant the permissions to the roles, and then create a user with a default role, I get a database access denied error for the user:</p>
  
  <pre><code>CREATE DATABASE testDb;
  
  CREATE ROLE readOnly;
  CREATE ROLE readWrite;
  
  GRANT SELECT ON testDB . * TO readOnly;
  GRANT ALL ON testDB . * TO readWrite;
  
  CREATE USER 'testUser'@'%' IDENTIFIED BY 'testPass';
  
  GRANT readOnly TO testUser;
  GRANT readWrite TO testUser;
  
  SET DEFAULT ROLE readOnly FOR testUser;
  \q
  </code></pre>
  
  <p>Then, when I try to connect to the database as testUser:</p>
  
  <pre><code>/mysql -u testUser -p -D testDb
  ERROR 1044 (42000): Access denied for user 'testUser'@'%' to database 'testDb'
  </code></pre>
  
  <p>On the other hand, if I don't use roles and grant the permissions directly to the user, user with no roles, I don't get the database access denied error:</p>
  
  <pre><code>DROP USER testUser;
  DROP ROLE readWrite;
  DROP ROLE readOnly;
  
  GRANT ALL ON testDb . * TO testUser@'%' IDENTIFIED BY 'testPass';
  \q
  </code></pre>
  
  <p>Now, connecting as testUser works:</p>
  
  <pre><code>mysql -u testUser -p -D testDb
  Enter password:
  MariaDB [testDb]&gt; 
  </code></pre>