Summary
Encountering a MySQL error like “Failed to set datadir” after moving MySQL data on Windows? This blog post provides step-by-step troubleshooting instructions to resolve the common MySQL error, including checking error logs, validating the my.ini configuration, and testing MySQL without installing the service.
Table of contents
What Causes the MySQL “Failed to Set datadir” Error?
If you’ve changed the datadir location in the my.ini file and restarted the MySQL service on a Windows server, you may encounter this error:
Error:
[ERROR] [MY-013276] [Server] Failed to set datadir to 'C:\Program Files\MySQL\MySQL Server 8.0\data\' (OS errno: 2 - No such file or directory)
Like me, you might be wondering why mysqld is trying to set the datadir in the default location. These are the first steps to finding that out.
Step-by-Step Troubleshooting
1. Check the Error Log
Most critical errors — including permission issues or invalid paths — are logged to the .err file defined in your my.ini.
Open that file and look for:
- File permission issues
- Invalid datadir
- Port conflicts
- Incorrect paths
2. Validate my.ini Configuration
Ensure your my.ini file is clean, valid, and points to the correct directories.
Things to check:
- basedir and datadir exist
- Data directory has been initialized using mysqld –initialize (or –initialize-insecure)
- You’ve used forward slashes / or properly escaped backslashes \\ in paths
- System variables are not listed more than once. The MySQL Server will recognize the last value referenced.
3. Test MySQL Without Installing the Service
Before reinstalling or restarting the Windows service, test MySQL manually to see direct output:
Example uses a generic location for the datadir. Edit the command as necessary.
"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe" --defaults-file="C:\new\path\my.ini"
If your config or directory setup is invalid, this command will output a detailed error in the console — no guesswork required.
Encountering other MySQL issues? Check out our other MySQL blogs or contact us!