The classic answer to this problem is to use 127.0.0.1 or the host IP or hostname instead of the "special name" localhost . From the documentation :
[...] Unix connections to localhost are made using the default Unix socket file
And later:
On Unix, MySQL programs handle the localhost host name specifically , which may be different than expected compared to other network programs. To connect to localhost, MySQL programs try to connect to the local server using a Unix socket file. This happens even if the -port or -P option is specified to specify the port number. For the client to establish a TCP / IP connection with the local server, use -host or -h to specify the host name value 127.0.0.1 or the IP address or name of the local server.
However, this simple trick does not work in your case, so you need to somehow force the use of a TCP socket. As you yourself explained, when you call mysql on the command line, you use the --protocol tcp option.
As explained here , from SQLAlchemy you can pass the appropriate parameters (if any) to your driver either as URL parameters or using the connect_args keyword argument.
For example, using PyMySQL, in the test system I installed for this purpose (MariaDB 10.0.12, SQLAlchemy 0.9.8 and PyMySQL 0.6.2), I got the following results:
>>> engine = create_engine( "mysql+pymysql://sylvain: passwd@localhost /db?host=localhost?port=3306")
As you noticed, both will use a TCP connection (I know that due to the port number after the host name). On the other hand:
>>> engine = create_engine( "mysql+pymysql://sylvain: passwd@localhost /db?unix_socket=/path/to/mysql.sock")
There is no port after the host name: this is a UNIX socket.
Sylvain leroux
source share