3

I just manually installed a new plugin on my WordPress site, created a table in MySQL for the plugin's entries, and when I try to open the page that uses the plugin, I receive this error:

DataTables warning (table id = 'to-do_list'): An error occurred while 
connecting to the database 'wordpress_clouse'. The error reported by the 
server was: SQLSTATE[HY000] [2002] Can't connect to local MySQL server 
through socket '/var/lib/mysql/mysql.sock' (2)

This is the MySQL info listed in my config file (which I believe is correct):

"type" => "Mysql",
"user" => "root",
"pass" => "******",
"host" => "localhost",
"port" => "",
"db"   => "******"

This is the main contents of the /etc/my.cnf file:

# The following options will be passed to all MySQL clients
[client]
#port       = 3306
socket      = /var/run/mysqld/mysqld.sock

# The MySQL server
[mysqld]
#port       = 3306
socket      = /var/run/mysqld/mysqld.sock
log_error   = mysqld.err

skip-networking

I've looked at answers to similar errors but none of the solutions worked for me. Any help would be appreciated.

eclipsis
  • 1,541
  • 3
  • 20
  • 56
  • MySQL can listen on either `TCP socket` or `unix domain socket` or both. Your server config is set to listen on unix domain socket `/var/run/mysqld/mysqld.sock` but your client is connecting to unix domain socket `/var/lib/mysql/mysql.sock`. – alvits Jan 13 '14 at 23:19
  • @alvits: Would the solution be to point the client to `/var/run/mysqld/mysqld.sock`? – eclipsis Jan 13 '14 at 23:27
  • Yes, that would be the solution. – alvits Jan 13 '14 at 23:28
  • I assume that would be written somewhere in the plugin? I did a quick search of the plugin and there was no mention of "mysql.sock" – eclipsis Jan 13 '14 at 23:29
  • If it is hard to reconfig wordperss, I'd suggest reconfiguring MySQL. `Stop MySQL`, modify `/etc/my.cnf` to use the socket that worpress expects . Finally `start MySQL`. Only do this if wordpress is the only client of this MySQL, otherwise it will be hard to reconfigure all clients. – alvits Jan 13 '14 at 23:33
  • If everything else fail, use TCP socket. – alvits Jan 13 '14 at 23:35
  • Just an FYI, when I changed host to `127.0.0.1` and port `3306`, I received this message: `Can't connect to MySQL server on '127.0.0.1' (111)`. Don't know if this adds any new info. Also, if I modify my.cnf to `/var/lib/mysql/mysql.sock`, wouldn't the .sock file need to be there? – eclipsis Jan 13 '14 at 23:41
  • You don't need to move the socket file. MySQL creates the socket on startup in the location pointed by `/etc/my.cnf`. If you prefer to use TCP socket, then you will need to enable port in /etc/my.cnf and configure wordpress to connect to localhost on port 3306 (or whatever port /etc/my.cnf specifies). Always shutdown MySQL before editing /etc/my.cnf. – alvits Jan 13 '14 at 23:46
  • Just tried your advice, when I restarted WordPress, it said "Error establishing database connection." I'll give TCP socket a try and report back. – eclipsis Jan 14 '14 at 00:11
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45154/discussion-between-eric-matthew-turano-and-alvits) – eclipsis Jan 14 '14 at 00:11
  • What were the other solutions that you tried but didn't work? – Andy Lester Mar 10 '14 at 20:28
  • @AndyLester See my answer below for what worked for me. Connecting via TCP didn't work for me, and then I tried the below solution. – eclipsis Mar 10 '14 at 21:55

2 Answers2

8

Here's what helped me solve this problem, which was that the actual file directory on my server was /var/lib/mysql/mysql.sock, not /var/lib/mysqld/mysqld.sock:

  1. Open your my.cnf file
  2. Add the following to your [client] section (if you don't have a [client] section, then add it). It should look like this:

[client]

socket=/var/lib/mysql/mysql.sock

The error should be gone. Many thanks to @alvits for pointing me in the right direction.

eclipsis
  • 1,541
  • 3
  • 20
  • 56
  • 2
    Useful answer, though you don't need to restart the mysql server as it's a client, not a server, setting that you're changing. – deadly Sep 10 '14 at 14:14
1

Encountered the same error and found it was because I haven't started the service yet.

Executed the command systemctl start mariadb and then connected to the MariaDB server using the command mysql and it worked.

Hope it helps

Sai Pardhu
  • 289
  • 1
  • 12
  • My site abnormally goes down, today and your answer saved me. but can you please tell me what could cause this. my site is on WordPress i am using amazon instance m4.xlarge. my site starts showing error "Error establishing a database connection". where should I look? thanks anyways. great help. – Usama Dec 03 '17 at 15:53
  • If the DB is getting disconnected, I'm assuming the reason is that mariadb not starting on boot, try doing this "systemctl enable mariadb". hope that helps. – Sai Pardhu Dec 06 '17 at 17:06