Solving MySQL Connection Error: Server sent charset in Symfony 3
While trying to connect a MySQL database in a Symfony 3 application, you might encounter the following error when executing the Symfony console command to create the MySQL schema:
PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2044 Database 'my_database' is not defined and no default database for connection is set in the server configuration in /path/to/your/project/app/console.
The error message suggests that the server sent a charset during the connection process. This issue arises because of a mismatch between the client and server charsets, specifically in php.ini and MySQL configuration files.
Understanding the character set and collation
Character sets enable MySQL to support various languages, and collations specify the sorting rules within a character set. When you create a database or a table, you can define its character set and collation. By default, latin1 (ISO 8859-1) and latin1_swedish_ci (respectively) are used.
Symfony 3 and Doctrine configuration
Symfony 3 uses the Doctrine ORM library for database management. The Doctrine configuration specifies the database driver, host, port, database name, user, and password. However, the character set configuration is missing in the doctrine.yaml file by default.
doctrine:
dbal:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
+ charset: UTF8
Adding the 'charset' configuration to the doctrine.yaml file is not enough to solve the issue. You need to modify your php.ini and MySQL configuration files.
Modifying php.ini file
Open your php.ini file (usually located in /etc/php/[version]/apache2/php.ini) and search for the following section:
[MySQLi]
; Allow local Infile
; http://php.net/mysqli.allow-local-infile
;mysqli.allow_local_infile = On
; If mysqlnd is used: Number of cache slots for the internal result set cache
; http://php.net/mysqli.cache_size
;mysqli.cache_size = 2000
; Default charset for connections from this PHP version
; http://php.net/mysqli.default-charset
;mysqli.default_charset =
Uncomment the 'mysqli.default_charset' line and set it to utf8mb4:
myself.default_charset = utf8mb4
Ensure you restart the Apache web server after saving the changes.
Modifying MySQL configuration files
Based on your MySQL installation, you could find the MySQL configuration file in one of the following paths:
- /etc/mysql/my.cnf
- /etc/mysql/mysql.conf.d/mysqld.cnf
Add the following lines under the [mysqld] section to enforce the utf8mb4 character set:
[mysqld]
character_set_server = utf8mb4
collation_server = utf8mb4_unicode_ci
After adding the lines, restart your MySQL service.
Conclusion and additional resources
Properly configuring the character set and collation for Symfony 3, PHP, and MySQL improves database connection and prevents unnecessary errors. Refer to the following resources for further reading:
Unicode Character Sets