Resolving MySQL Service Starting and Then Stopping Immediately on Windows

Nov 25, 2025 · Programming · 9 views · 7.8

Keywords: MySQL Service | Windows Troubleshooting | Data Directory Initialization

Abstract: This technical article provides an in-depth analysis of the common causes and solutions for MySQL service starting and then stopping immediately on Windows systems. It focuses on the mysqld --initialize command for data directory initialization, while also covering supplementary solutions such as configuration file encoding issues and permission settings. Through detailed step-by-step instructions and code examples, it helps users systematically diagnose and fix MySQL service startup failures.

Problem Phenomenon Analysis

When deploying MySQL service on Windows operating systems, users frequently encounter the issue where the service starts and then stops immediately. System event logs typically show "MySQL service has started" followed by "MySQL service has stopped" messages, but lack specific error details. This problem is particularly common in MySQL 8.0 versions, especially after initial installation or configuration modifications.

Core Solution: Data Directory Initialization

Based on technical community实践经验, the most effective solution is to reinitialize the MySQL data directory using the mysqld --initialize command. This command creates necessary system tables, sets up default user accounts, and generates a temporary root password.

Detailed operational steps:

  1. Open Command Prompt as administrator
  2. Stop current MySQL service: net stop mysql
  3. Backup existing data directory (if important data exists)
  4. Execute initialization command: mysqld --initialize --console
  5. Record the temporary root password from console output
  6. Restart MySQL service: net start mysql

The underlying mechanism of initialization process involves:

// Simulating core logic of initialization process
void initialize_data_directory() {
    create_system_tables();
    generate_temp_root_password();
    set_file_permissions();
    validate_configuration();
}

Configuration File Encoding Issues

In MySQL 8.0 versions, configuration files like my.ini may contain Unicode characters, causing text editors like Notepad to automatically add Byte Order Mark (BOM). These hidden characters interfere with MySQL's configuration parsing, leading to service startup failures.

Detection and repair methods:

# Check file beginning using hex editor
hexdump -C my.ini | head -n 2
# If EF BB BF byte sequence is found, remove BOM
sed -i '1s/^\xef\xbb\xbf//' my.ini

File System Permission Configuration

The MySQL service running account (typically NETWORK SERVICE) requires full control permissions on the data directory. Insufficient permissions prevent the service from accessing necessary database files.

Permission setting code example:

# PowerShell script for permission setting
$acl = Get-Acl "C:\ProgramData\MySQL\Data"
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("NETWORK SERVICE", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
$acl.AddAccessRule($rule)
Set-Acl "C:\ProgramData\MySQL\Data" $acl

Error Diagnosis and Log Analysis

When standard solutions prove ineffective, deep analysis of error logs is necessary. MySQL error logs are typically located in the hostname.err file within the data directory. Key diagnostic steps include:

# Check latest entries in error log
tail -f "C:\ProgramData\MySQL\Data\\hostname.err"

# Start service in verbose mode
mysqld --console --verbose

Preventive Measures and Best Practices

To prevent recurrence of similar issues, follow these best practices:

Through systematic problem analysis and multi-layered solutions, users can effectively resolve MySQL service startup failures and ensure stable database service operation.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.