[Fixed] Can't connect to local MySQL server through socket '/tmp/mysql.sock'

Hi there!

I successfully installed MariaDB & ERPNext on a new Ubuntu 16.04 LTS machine, but was receiving this error:

"Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)"

when issuing the command:

bench new-site localhost

This happens because MariaDB is (correctly) using /var/run/mysqld/mysqld.sock (as configured in /etc/mysql/my.cnf) instead of /tmp/mysql.sock (the default value).

A simple workaround wold be to create a symbolic link, e.g.:

ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock

But the real problem is that while MariaDB is reading the my.cnf file, ERPNext isn’t.

According to the MariaDB documentation, /tmp/mysql.sock is the default socket file, and while libmariadb doesn’t read my.cnf by default, it can be instructed to do so - see:

To “fix” the problem, I patched the file:

frappe-bench/apps/frappe/frappe/database.py

changing only the connection parameters on lines 52-53, from:

		self._conn = MySQLdb.connect(user=self.user, host=self.host, passwd=self.password,
			use_unicode=True, charset='utf8mb4')

to:

		self._conn = MySQLdb.connect(user=self.user, host=self.host, passwd=self.password,
			use_unicode=True, charset='utf8mb4', read_default_file="/etc/mysql/my.cnf")

This “Works For Me™”, but I guess ERPNext should ideally first search for the my.cnf file in the paths listed in table 5.2 of MySQL manual - Using Option Files.

I hope this might help others.

@FunKing I guess no one has reported this before. If you have a generic solution, send a pull request!

1 Like