TL;DR
After years of using Mariadb without issue, suddenly the program would not work. It gave the "cannot connect via socket" message, but what it really meant was that mysqld was not running, although in some cases it was actually running.
I have recently been developing using a local stack on my Mac including mariadb. Mariadb-10.2 was installed using MacPorts. This configuration is very convenient and has in in daily use for about a year. Suddenly a few days ago it stopped working.
When trying to connect from the mysql client, the error message is as follows:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/local/var/run/mariadb-10.3/mysqld.sock' (2)
The socket file does not exist, and creating a normal file at that location does not help. I have not had to consider that file for a great many years, as I think it is just created by the client or server on demand. However, I do remember many years ago that when the socket file was missing you could just create it as a normal file in the proper location.
Most troubleshooting steps either say "create the socket" or "make sure mysqld is running." Unfortunately there does not appear to be any easy way to create the socket. And mysqld is not running, possibly because of the same error. I have also tried to loosen up the permissions on the directory structure to see if that helps, but it does not.
I have also tried providing a specific location for the socket in my.cnf, but that does not help either.
The way the the mysql server is stopped and started is via the following two commands:
sudo port unload mariadb-10.2-server
sudo port load mariadb-10.2-server
These commands appear to run properly without errors, but yet the server is not running. As I said, for a year there has been no problems with mariadb, or nor with using those two commands to restart the server.
I have tried to reinstall various versions of mariadb: 10.2, 10.3, 10.4, 10.6 and even the current 10.7. For some reason 10.4 and 10.6 will not install properly. However both 10.2 and 10.3 do install, but always give the error message shown above about the socket.
CONTINUATION:
Over the next several days I continued to pound on this problem and it appears to be mostly resolved. Here is what it took:
- Tried to install various versions of mariadb--all would give various errors. Tried each of 10.2, 10.3, 10.4, 10.5, 10.6 and 10.7, include many repetitions of several of the versions.
- The install would fail.
- If the install worked, creation of the db would not work.
- If the db creation worked, the securing the db function failed.
- Mysql client would not run, always with the socket error
- Workarounds required
- Used mariadb-10.5, which seems the least plagued with errors. And amazingly this time, it did cause a socket file to appear in the expected location, which none of the others did. And even 10.5 did not previously. This is probably the core of the issue, where mariadb would not start, and the missing socket is just a symptom of that.
- Since the root user was created without a password, must use sudo to run the client. This solved another aspect mentioned above.
- Since mysql_secure_installation fails due to permissions on several files, just manually perform the steps, which are are documented in various places (mainly involves adding passwords to root users, removing accounts without a password, and removing the test database).
- Must change host from 127.0.0.1 back to localhost for the mysql client to be runnable. The IP address no longer works, but localhost does. This is the reverse of my previous installation.
- Must be careful to tweak the my.cnf for the particular version with the required settings.
- Must add setting in php.ini for socket location. Alternatively, use or use ini_set('mysqli.default_socket','/opt/local/var/run/mariadb-10.5/mysqld.sock');
It is still a mystery as to how this situation came about, and why it took all these workarounds that I have never needed previously.
I am leaving this as a question instead of considering that I have answered my own question, in the hope that someone has some insight into the root cause(s) of the issue.