Steps to reproduce:

1> Install drupal and get it to step "Set up database"
2> Enter a database name with hyphens like wrong-db-name
3> Click submit

Note: Error appears only once and to reproduce again change the DB-name


Actual result:


Screenshot 2020-07-06 at 5.51.48 PM.png
Error
The website encountered an unexpected error. Please try again later.

Drupal\Core\Database\DatabaseNotFoundException: SQLSTATE[HY000] [1049] Unknown database 'wrong-database' in
Drupal\Core\Database\Driver\mysql\Connection::open() (line 139 of core/lib/Drupal/Core/Database/Driver/mysql/Connection.php).
Drupal\Core\Database\Driver\mysql\Connection::open(Array) (Line: 380)
Drupal\Core\Database\Database::openConnection('default', 'default') (Line: 169)
Drupal\Core\Database\Database::getConnection() (Line: 366)
Drupal\Core\Database\Install\Tasks->getConnection() (Line: 81)
Drupal\Core\Database\Driver\mysql\Install\Tasks->minimumVersion() (Line: 219)
Drupal\Core\Database\Install\Tasks->checkEngineVersion() (Line: 195)
Drupal\Core\Database\Driver\mysql\Install\Tasks->checkEngineVersion()
call_user_func_array(Array, Array) (Line: 142)
Drupal\Core\Database\Install\Tasks->runTasks() (Line: 1207)
install_database_errors(Array, './sites/default/settings.php') (Line: 186)
Drupal\Core\Installer\Form\SiteSettingsForm->getDatabaseErrors(Array, './sites/default/settings.php') (Line: 169)
Drupal\Core\Installer\Form\SiteSettingsForm->validateForm(Array, Object)
call_user_func_array(Array, Array) (Line: 82)
Drupal\Core\Form\FormValidator->executeValidateHandlers(Array, Object) (Line: 273)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object, 'install_settings_form') (Line: 118)
Drupal\Core\Form\FormValidator->validateForm('install_settings_form', Array, Object) (Line: 589)
Drupal\Core\Form\FormBuilder->processForm('install_settings_form', Array, Object) (Line: 321)
Drupal\Core\Form\FormBuilder->buildForm('Drupal\Core\Installer\Form\SiteSettingsForm', Object) (Line: 945)
install_get_form('Drupal\Core\Installer\Form\SiteSettingsForm', Array) (Line: 615)
install_run_task(Array, Array) (Line: 568)
install_run_tasks(Array, NULL) (Line: 118)
install_drupal(Object) (Line: 48)

Expected result:

Screenshot 2020-07-06 at 5.48.11 PM.png
Resolve all issues below to continue the installation. For help configuring your database server, see the installation handbook, or contact your hosting provider.
Database wrong-database not found. The server reports the following message when attempting to create the database: SQLSTATE[HY000]: General error: 1007 Can't create database 'wrong database'; database exists.
Failed to CREATE a test table on your database server with the command CREATE TABLE {drupal_install_test} (id int NOT NULL PRIMARY KEY). The server reports the following message: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected: CREATE TABLE {drupal_install_test} (id int NOT NULL PRIMARY KEY); Array ( ) .
Are you sure the configured username has the necessary permissions to create tables in the database?

Comments

thedrupalkid created an issue. See original summary.

thedrupalkid’s picture

Issue summary: View changes
codersukanta’s picture

Assigned: Unassigned » codersukanta
codersukanta’s picture

Assigned: codersukanta » Unassigned

In this case for mysql, database is getting created escaping the unacceptable characters given in the database name.
After that it tries to match the given database name (Unescaped) in the database server and throws

The website encountered an unexpected error

.

There could be 2 possible solution :

  1. Validate given database name without escaping and throw an error.
  2. Create database after escaping characters and return newly created database name as a success message and update settings.php accordingly.
thedrupalkid’s picture

Hello @codersukanta, Thanks for your valuable inputs on this but what I believe the problem is that it is not throwing an error which it generally shows ie: Screenshot 2020-07-06 at 5.48.11 PM.png we need not to do this

Validate given database name without escaping and throw an error.

or

Create database after escaping characters and return newly created database name as a success message and update settings.php accordingly.

As we already have a solution but in this specific case it is sometimes leads to an error screen ie: Screenshot 2020-07-06 at 5.51.48 PM.png

So as I have already defined in the Issue summary we need to have the expected result instead of the actual result we have.

codersukanta’s picture

Status: Active » Needs review
StatusFileSize
new1.93 KB

Adding patch for the issue.

narendra.rajwar27’s picture

Status: Needs review » Needs work
StatusFileSize
new221.22 KB

@codersukant, Thanks for taking this forward. It seems it is now showing only the error message given and not showing rest of the error details as mentioned in IS expected result. Would be great if error details along with error message could be shown. Except that patch looks good to me.

adding SS of error message after patch:

Thanks

codersukanta’s picture

Status: Needs work » Needs review

Thanks @narendra.rajwar27 for reviewing the patch.

Regarding

not showing rest of the error details as mentioned in IS expected result

This is not as per the expected result from issue summary because the expected message is returned only after the system has created database with the escaped DB name (this is happening @escapeDatabase function at core/lib/Drupal/Core/Database/Connection). The message in question appears only during second attempt to create the DB where in it says that DB already exists.

Ex:

DB Name is abc-abc

The system first creates the database with name "abcabc" after escaping unwanted characters (characters that does not comply with naming convention) here because in system settings we still have "abc-abc" as DB name (actual DB created with name 'abcabc') because of which system throws DB not found error.

On second attempt with same 'abc-abc' DB name system throws/returns DB already exists error because now the same function (escapeDatabase) that previously created database in DB server by escaping the unwanted characters is attempting to do the same i.e escaping DB name and attempting to create the database with escaped name 'abcabc' which already exists.

So in this scenario it is best to prevent DB creation during early validation stage so this unnecessary DB creation by escaping unwanted characters be avoided, and this is exactly what the patch at #6 is doing and for this reason the message returned is "Invalid database name ...".

Kindly share if you have any other point of view or approach to handle this scenario.

thedrupalkid’s picture

StatusFileSize
new92.04 KB

@codersukanta: As per our conversation on slack, Let's make the changes in the error message which is we do not require a list as there is only one error, also we do not require the following message

"Resolve all issues below to continue the installation. For help configuring your database server, see the installation handbook, or contact your hosting provider."

.
I have attached screenshot for the reference:

Good to have: A link reference to database naming conventions in error message just like "Installation Guide".

codersukanta’s picture

Hi @thedrupalkid, Thanks for reviewing the patch.

As per the messsage display is concerned this is being shown as per default template on the page. We have not done anything to disturb that.

Working on message displaying template for this page will be scope creep.

daffie’s picture

Priority: Minor » Normal
Status: Needs review » Needs work
Issue tags: +Needs tests
  1. Could we add the naming convention to the error message.
  2. We also need an automated test for this issue. The right place for the test in in the directory: core/tests/Drupal/FunctionalTests/Installer.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

quietone’s picture

This is a duplicate of an older issue, #2443839: Drupal does not install when auto-creating the MySQL database with special characters. That issue also has a patch with tests and much more discussion. Like here the solution is to inform the user of the naming restrictions.

Therefor closing this as duplicate

quietone’s picture

Issue tags: +Bug Smash Initiative