How to Fix MySQL init-file “File Not Found” Error on Ubuntu Server


2 views

When configuring MySQL to execute initialization scripts through the init-file parameter, many Ubuntu users encounter the frustrating "File not found" error despite the file clearly existing. Let's dissect this common pitfall.

The error message reveals two critical details:

111111 7:41:06 [ERROR] /usr/sbin/mysqld: File '/etc/mysql/mysqlinit.sql' not found (Errcode: 13)
111111 7:41:06 [ERROR] Aborting

Errcode 13 indicates a permission issue, not an actual missing file. MySQL daemon runs as the mysql user, which needs proper access to both the file and its parent directories.

Here's the comprehensive approach to resolve this:

# 1. Verify file exists
sudo ls -la /etc/mysql/mysqlinit.sql

# 2. Set correct ownership (critical!)
sudo chown mysql:mysql /etc/mysql/mysqlinit.sql

# 3. Verify directory permissions
sudo ls -ld /etc/mysql/

# 4. Ensure mysql user has execute permissions on parent directories
sudo chmod +x /etc
sudo chmod +x /etc/mysql

# 5. Set appropriate file permissions
sudo chmod 755 /etc/mysql/mysqlinit.sql

# 6. Verify SELinux/AppArmor isn't blocking access
sudo aa-status  # For AppArmor
getenforce      # For SELinux

For complex initialization scripts, consider this robust setup:

-- Example mysqlinit.sql content
SET GLOBAL log_bin_trust_function_creators = 1;
CREATE DATABASE IF NOT EXISTS app_config;
USE app_config;

DELIMITER //
CREATE PROCEDURE initialize_system()
BEGIN
    -- Your initialization logic here
END //
DELIMITER ;

CALL initialize_system();
  • Check MySQL error log: sudo tail -f /var/log/mysql/error.log
  • Test script manually: mysql -u root -p < /etc/mysql/mysqlinit.sql
  • Verify AppArmor permissions if using Ubuntu

For enterprise deployments:

# Create dedicated init directory
sudo mkdir /var/lib/mysql-init
sudo chown mysql:mysql /var/lib/mysql-init

# Update my.cnf
[mysqld]
init-file=/var/lib/mysql-init/bootstrap.sql

This isolates initialization scripts from system configuration files and follows principle of least privilege.


When trying to automate SQL script execution during MySQL server startup on Ubuntu 11.10, many developers encounter a frustrating "File not found" error despite the file existing with correct permissions. Here's what's happening:

111111  7:41:06 [ERROR] /usr/sbin/mysqld: File '/etc/mysql/mysqlinit.sql' not found (Errcode: 13)
111111  7:41:06 [ERROR] Aborting

The key insight is that MySQL (mysqld) runs as the mysql user, but needs proper access to both the file and its parent directories. Even with -rwxr-xr-x permissions on the file itself, directory permissions might block access.

Check the complete path permissions:

namei -l /etc/mysql/mysqlinit.sql

Here's the step-by-step fix that works on Ubuntu systems:

  1. Ensure proper file ownership:
    sudo chown mysql:mysql /etc/mysql/mysqlinit.sql
  2. Set correct file permissions:
    sudo chmod 644 /etc/mysql/mysqlinit.sql
  3. Verify directory permissions:
    sudo chmod +x /etc
    sudo chmod +x /etc/mysql
  4. Add SELinux context if applicable:
    sudo chcon -R -t mysqld_db_t /etc/mysql/

For complex initialization, your SQL file might look like this:

-- /etc/mysql/mysqlinit.sql
CREATE DATABASE IF NOT EXISTS app_config;
USE app_config;

CREATE TABLE IF NOT EXISTS startup_params (
    param_name VARCHAR(50) PRIMARY KEY,
    param_value TEXT,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

INSERT IGNORE INTO startup_params (param_name, param_value)
VALUES ('init_version', '1.0.2');
  • Test your SQL file manually first: mysql -u root -p < /etc/mysql/mysqlinit.sql
  • Check AppArmor restrictions: sudo aa-status | grep mysql
  • Verify MySQL error log location: sudo grep log-error /etc/mysql/my.cnf

If init-file still doesn't work, consider these alternatives:

1. Using systemd (for newer Ubuntu versions):

[Service]
ExecStartPost=/usr/bin/mysql -u root -pPASSWORD -e "SOURCE /etc/mysql/mysqlinit.sql"

2. Via MySQL event scheduler:

CREATE EVENT run_init_script
ON SCHEDULE AT CURRENT_TIMESTAMP
DO
SOURCE /etc/mysql/mysqlinit.sql;