Databases 12 min read

MySQL Initialization Failure Caused by Missing Newline in Configuration File

When initializing a MySQL 5.7.44 instance, the server aborts because the configuration file lacks a trailing newline, causing the parser to misinterpret the final '!includedir' line and report a missing '/etc/my.cnf.' directory, which can be resolved by adding a newline at the end of the file.

Aikesheng Open Source Community
Aikesheng Open Source Community
Aikesheng Open Source Community
MySQL Initialization Failure Caused by Missing Newline in Configuration File

Background

During the initialization of a MySQL 5.7.44 instance, the command /opt/mysql/base/5.7.44/bin/mysqld --defaults-file=/u01/my.cnf --datadir=/u01/3306 --basedir=/opt/mysql/base/5.7.44/ --initialize fails with the error "Can't read dir of '/etc/my.cnf.' (Errcode: 2 - No such file or directory)" and aborts.

Steps to Resolve

The error points to a missing /etc/my.cnf. directory. Inspection of my.cnf shows only an !includedir /etc/my.cnf.d directive, and the directory /etc/my.cnf.d does exist.

[root@db1 ~]# cat /u01/my.cnf|grep /etc/my.cnf
!includedir /etc/my.cnf.d

[root@db1 ~]# ls -l /etc/my.cnf.d
total 4
-rw-r--r-- 1 root root 232 May 6 2020 mysql-clients.cnf

Running the server with --help still produces the same error, indicating the problem lies in the configuration file itself. Removing the !includedir /etc/my.cnf.d line allows the server to start.

[root@db1 ~]# cp /u01/my.cnf /u01/my.cnf.bak
[root@db1 ~]# vi /u01/my.cnf.bak
[root@db1 ~]# /opt/mysql/base/5.7.44/bin/mysqld --defaults-file=/u01/my.cnf.bak --help
... (output shows successful start) ...

Adding a newline to the original /u01/my.cnf file resolves the initialization issue:

[root@db1 ~]# echo >> /u01/my.cnf
[root@db1 ~]# tail -1 /u01/my.cnf
!includedir /etc/my.cnf.d
[root@db1 ~]# /opt/mysql/base/5.7.44/bin/mysqld --defaults-file=/u01/my.cnf --datadir=/u01/3306 --basedir=/opt/mysql/base/5.7.44/ --initialize

Root Cause Analysis

Using strace on the failing run shows the server attempts to open "/etc/my.cnf./" (note the trailing dot) and fails, because the last line of the configuration file lacks a newline. The MySQL parser reads the file in 4096‑byte blocks; without a terminating newline, the final !includedir or !include directive is truncated, leading to an incorrect path.

strace -T -tt -s 100 -o /tmp/strace.log /opt/mysql/base/5.7.44/bin/mysqld --defaults-file=/u01/my.cnf ...
... openat(AT_FDCWD, "/etc/my.cnf./", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = -1 ENOENT ...
... write(2, "mysqld: Can't read dir of '/etc/my.cnf.' ...") = 73 ...

When the final line ends with !includedir or !include and lacks a newline, the parser truncates the last character of the included filename, causing a "file not found" situation. Adding an explicit newline prevents this truncation.

Summary

If the last line of my.cnf is an !includedir directive without a trailing newline, MySQL truncates the final character, reports "Can't read dir of '/etc/my.cnf.'" and aborts initialization.

If the last line is an !include directive without a newline, the referenced filename is truncated but the server may still start.

Therefore, always ensure the configuration file ends with a newline character.

Supplement

Two common ways to create a file without a trailing newline are:

Using echo -n "xx" >> my.cnf

Using printf "xx" >> my.cnf

Any other scenarios that lead to a missing newline are welcome for discussion.

debuggingconfigurationMySQLInitializationstracenewline
Aikesheng Open Source Community
Written by

Aikesheng Open Source Community

The Aikesheng Open Source Community provides stable, enterprise‑grade MySQL open‑source tools and services, releases a premium open‑source component each year (1024), and continuously operates and maintains them.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.