Problem¶
I want to use TurboGears, but my machine is set up with MySQL not bound to a network socket and I get the following errors when I try to connect: _mysql_exceptions.OperationalError?: [2002, “Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ [2]”];
Solution¶
There is a practice of not binding MySQL to an IP address and port following the principle that “if you’re not connected, you are protected.” In this situation, the server has a socket file. By default TurboGears [or SQLObject and the Python MySQL driver/client to be more exact] will look for the socket file in /tmp/mysql.sock. Many installations seem to use /var/lib/mysql/mysql.sock. Therefore, to get TurboGears to work, you’ll need to specify the unix_socket [if on Unix] param in the dburi. You can find out where your mysql socket is by looking at the file my.cnf, which usually resides in /etc/my.cnf or /etc/mysql/my.cnf.
Format¶
sqlobject.dburi="!mysql://username:password@/databasename?unix_socket=path-to-mysql.sock"
Example¶
I have my socket file in /var/lib/mysql/mysql.sock and I want my user foo with password bar to connect to my db test1.
sqlobject.dburi="!mysql:/foo:bar@/test1?unix_socket=/var/lib/mysql/mysql.sock"
I got this error yesterday when trying to connect to my mysql database after getting it running with NodeJS. There is a lot of conflicting information on the web due to how you download mysql, versions, which OS you’re on, etc.
For a quick fix on a unix system I suggest doing the following! The steps below worked for me, and can probably work for you, too! However, you will need to install HomeBrew if you don’t have it already.
$ brew doctor
- Make necessary fixes if you haven’t already [this is where I had many directories with screwed up permissions]
- Reinstall mysql to be safe and get latest version
$ brew remove mysql
$ brew install mysql
3. Now you should be good to go!
To Start Your MySQL Connection
$ mysql.server start
To Close Your MySQL Connection
$ mysqld stop
To Open Bash Terminal for Local Development
$ mysql -u root
If you run into this error message while trying to connect to a local MySQL Server:ERROR 2002 [HY000]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'
It means either the MySQL server is not installed/running, or the file mysql.sock
doesn’t exist in /var/lib/mysql/
.
There are a couple of solutions for this error.
1. First, check to see whether mysqld service is running or not. If not, start it:
Then try to connect again.
2. Try to connect to 127.0.0.1
instead of localhost
If you connect to localhost
, it will use the socket connector, but if you connect to 127.0.0.1
the TCP/IP connector will be used. So when the socket connector is not working, try connecting to 127.0.0.1
instead.
3. Edit file my.cnf
Find the file my.cnf
[usually in /etc/
] to edit and add these following line:
[mysqld]
socket=/var/lib/mysql/mysql.sock
[client]
socket=/var/lib/mysql/mysql.sock
Restart mysql and connect again.
4. Symlink
In some cases, you mysql.sock
might be located in another folder. You have to find and symlink it. You might find it in /tmp/mysql.sock
or /data/mysql_datadir/mysql.sock
For example, your file is located as in /tmp/mysql.sock
Then symlink it:
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
Restart mysql and connect again.
Need a good GUI Tool for MySQL? TablePlus is a modern, native tool with an elegant UI that allows you to simultaneously manage multiple databases such as MySQL, PostgreSQL, SQLite, Microsoft SQL Server and more.
Download TablePlus for Mac.
Not on Mac? Download TablePlus for Windows.
On Linux? Download TablePlus for Linux
Need a quick edit on the go? Download TablePlus for iOS.