Unexpected Default User in MySQL Client: Investigation and Resolution
The article investigates why the MySQL client defaults to a non‑root user (zhenxing) despite configuration files showing root, detailing the discovery of the hidden .mylogin.cnf file, the effect of --no-defaults, and how MySQL reads option files in a specific order.
Scenario Description
During local testing, the author observed that invoking the MySQL client via a Unix socket without specifying a user logged in as zhenxing instead of the expected root user, resulting in an "Access denied" error.
Initial Observations
Running mysql -S /data/mysql/3306/data/mysqld.sock -p displayed the error, and mysql --help|egrep "user|host|port" confirmed that the default user reported by the client was zhenxing .
Configuration File Investigation
The usual suspect, /etc/my.cnf , was examined and correctly set user=root . Subsequent checks of other typical locations ( /etc/mysql/my.cnf , /usr/local/mysql/etc/my.cnf , /data/mysql/3306/base/my.cnf , ~/.my.cnf ) all returned "file not found" or contained no relevant settings.
Determining the Read Order
## 查看mysql客户端读取配置文件的顺序
mysql --verbose --help|grep my.cnf
order of preference, my.cnf, $MYSQL_TCP_PORT, /etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf /data/mysql/3306/base/my.cnf ~/.my.cnfAll listed files were excluded, yet the default user remained zhenxing .
Using --no-defaults
Even with --no-defaults , the client still reported user=zhenxing :
# mysql --no-defaults --help|egrep "user|host|port"
host 127.0.0.1
port 3306
user zhenxingRunning mysql --print-defaults showed that the client would have been started with arguments including --user=zhenxing , indicating an extra source of configuration.
System Call Tracing
Using strace revealed the client attempts to stat several files; the only existing file beyond the standard list was /root/.mylogin.cnf :
stat("/root/.mylogin.cnf", {st_mode=S_IFREG|0600, st_size=336, ...}) = 0Inspecting .mylogin.cnf
The file is generated by mysql_config_editor and stores encrypted login‑path credentials. Displaying its contents:
# mysql_config_editor print --all
[client]
user = "zhenxing"
password = *****
host = "127.0.0.1"
port = 3306This explains why the client defaults to zhenxing even when other option files are absent or when --no-defaults is used.
Conclusion
The MySQL client reads configuration in the order shown by mysql --verbose --help|grep my.cnf , but it also always reads the login‑path file .mylogin.cnf . This file overrides other settings, and its values are applied even with --no-defaults , providing a safer way to store passwords. The unexpected default user in the author's environment was caused by a previous test that populated .mylogin.cnf with the zhenxing credentials.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.