Why is MySQL not connecting when reading from mylogin.cnf? On Ubuntu 20.04, I can log into a server by specifying the log credentials on the CLI:
$ mysql -h172.30.0.2 -uroot -p bar -e &quot;SELECT id FROM users LIMIT 1;&quot;
Enter password:
+----+
| id |
+----+
|  1 |
+----+

However, I cannot log in when using ~/.mylogin.cnf:
$ cat ~/.mylogin.cnf
[foo]
user=root
password=&quot;notTheRealPassword&quot;
host=172.30.0.2

$ mysql --login-path=foo bar -e &quot;SELECT id FROM users LIMIT 1;&quot;
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I have confirmed that the file is being read by changing the permissions of the file and noticing MySQL complaining:
$ chmod 660 ~/.mylogin.cnf

$ mysql --login-path=foo bar -e &quot;SELECT id FROM users LIMIT 1;&quot;
mysql: [Warning] /home/dotancohen/.mylogin.cnf should be readable/writable only by current user.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Why might MySQL not connect when using ~/.mylogin.cnf? How should I debug why MySQL is not connecting?
There is a bug here.
You can check your .conf file permissions.
The root cause of crashes or other unexpected behavior will be in the error logs. The default path of the MySQL log file is /var/log/mysql/error.log on Ubuntu.
Also for debugging check this out.