Turn Your MySQL to Case Sensitivity on Windows

Süleyman Özgür Özarpacı - Apr 15 - - Dev Community

When working with MySQL on Windows, you might notice that table names are not case-sensitive by default. This is because the Windows file system is case-insensitive, unlike Linux or macOS, which can lead to unexpected behavior if your development or production environment depends on case-sensitive table names. In my case, I am using Windows 11, and I needed to enable case sensitivity for a project where I need to ensure that User and user are treated as distinct table names. Here’s how I achieved that.

Step 1: Locate Your MySQL Configuration File

The first step in making MySQL case-sensitive is to modify the MySQL configuration file (my.ini). Depending on how MySQL is installed on your machine, the path to this file can vary. In my case, I'm using Laragon, so my configuration file is located here:

C:\laragon\bin\mysql\mysql-8.0.13-winx64\my.ini
Enter fullscreen mode Exit fullscreen mode

You will need to open this file with a text editor like Notepad or an IDE of your choice.

Step 2: Modify the my.ini Configuration

Once you've opened the my.ini file, search for the [mysqld] section. This is where you will add a new configuration line to enable case sensitivity. Insert the following line directly under the [mysqld] header:

lower_case_table_names=0
Enter fullscreen mode Exit fullscreen mode

This setting tells MySQL to preserve the case of table names, making the database case-sensitive. By default, on Windows, the value is 1, which means MySQL converts all table names to lowercase, regardless of how they are defined.

Step 3: Adjusting the Windows File System

Although adding the above configuration enables MySQL to respect case sensitivity, there’s one important thing to note: the Windows file system is inherently case-insensitive. That means even with lower_case_table_names=0, your file system will still treat User and user as the same file or folder, leading to potential issues.

To overcome this limitation, you need to configure the Windows file system to support case-sensitive directories. This requires some additional steps.

Step 4: Enable Case Sensitivity in Windows

You will need to enable case sensitivity for the folder where MySQL stores your database files. This folder is typically located under your MySQL installation's data directory. In my setup, it’s located here:

C:\laragon\data
Enter fullscreen mode Exit fullscreen mode

Before proceeding, I recommend making a backup of all your existing database files and folders in case anything goes wrong. Once that’s done, you can enable case sensitivity for this folder by running the following command in an elevated Command Prompt (run as Administrator):

fsutil.exe file SetCaseSensitiveInfo C:\laragon\data enable
Enter fullscreen mode Exit fullscreen mode

This command tells Windows to enable case sensitivity for the specified directory, which is crucial for MySQL to handle table names in a case-sensitive manner.

Step 5: Restart MySQL

After making the above changes, you should restart the MySQL service for the new configuration to take effect. If everything is set up correctly, MySQL should now treat User and user as two separate tables, just like it would on a case-sensitive file system.

Final Thoughts

Enabling case sensitivity on MySQL in Windows can be tricky due to the underlying nature of the Windows file system, but by following the steps outlined above, you should be able to achieve the desired behavior. Remember that while this works well for development environments, it's always a good idea to match your production environment closely to avoid any surprises when deploying your application.

For more details on this topic, check out the following resources:

Case-Sensitive Table Names in MySQL
How to Make Windows Folders Case-Sensitive
Managing MySQL Lower Case Table Names

. . . . . . . . . . . . . .
Terabox Video Player