Index: provision/db_server/provision_mysql.drush.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/provision/db_server/provision_mysql.drush.inc,v
retrieving revision 1.23
diff -u -p -r1.23 provision_mysql.drush.inc
--- provision/db_server/provision_mysql.drush.inc	28 Aug 2009 13:01:01 -0000	1.23
+++ provision/db_server/provision_mysql.drush.inc	27 Oct 2009 20:18:40 -0000
@@ -231,20 +231,28 @@ function _provision_mysql_suggest_db_nam
     // site name
     //
     // Provision only users will trigger this mostly.
-    $suggest_base = substr(str_replace(array(".", "-"), '' , ereg_replace("^www\.", "", $url)), 0, 14);
-  }
-  $suggest[] = $suggest_base;
-  for ($i = 0; $i < 100; $i++) {
-    $suggest[] = $suggest_base .'_'. $i;
+    $suggest_base = str_replace(array(".", "-"), '' , ereg_replace("^www\.", "", $url));
   }
+  // Mysql has a hard 16 char limit on user name length. Since we are using the same string for
+  // user and database names, we need to limit the number of characters of both to 16.
+  $mysql_username_limit = 16;
 
-  foreach ($suggest as $option) {
+  $max_num = pow(10, $mysql_username_limit);
+  $postfix = '';
+  for ($i = 0; $i < $max_num; $i++) {
+    // Make the $suggested base short enough to add the postfix if needed and 
+    // still respect the mysql char limit.
+    $clean_suggest_base = substr($suggest_base, 0, ($mysql_username_limit - strlen($postfix)));
+    $option =  $clean_suggest_base . $postfix;
     if (!_provision_mysql_database_exists($option)) {
+      drush_log(dt("@option is available", array('@option' => $option)), 'message');
       return $option;
+      
     }
+    $postfix = '_'. $i;
   }
 
-  drush_set_error('PROVISION_CREATE_DB_FAILED', dt("Could not find a free database names after 100 attempts"));
+  drush_set_error('PROVISION_CREATE_DB_FAILED', dt("Could not find a free database name after @i attempts", array('@i' => $i)));
   return false;
 
 }
