5

I opened my application (in Rails) on Amazon EC2 and got an error - thus I checked the logs and there is following:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)

10 hours ago was everything working yet. What's the problem? A lot of traffic? The app is running on Micro Instance.

How to fix this issue and how to avoid it in the future?

Thank you very much

EDIT:

sudo find / -type s
---
/tmp/.sock
/dev/log
/var/lib/apt-xapian-index/update-socket
/run/mysqld/mysqld.sock
/run/acpid.socket
/run/dbus/system_bus_socket
/run/udev/control
find: `/proc/4739/task/4739/fd/5': No such file or directory
find: `/proc/4739/task/4739/fdinfo/5': No such file or directory
find: `/proc/4739/fd/5': No such file or directory
find: `/proc/4739/fdinfo/5': No such file or directory
user984621
  • 46,344
  • 73
  • 224
  • 412
  • Sounds Similar like [this](http://stackoverflow.com/questions/11990708/error-cant-connect-to-local-mysql-server-through-socket-var-run-mysqld-mysq) – slayedbylucifer Oct 29 '13 at 09:14
  • have you tried to `locate` your `mysqld.sock` file...using locate `mysqld.sock` command. Does the path matches with the `socket=path` in your `myconf` file?? – amit karsale Oct 29 '13 at 09:18
  • Hello guys, thank you for your answers. `locate mysqld.sock` returns empty output, when I ran `sudo find / -type s`, I see there `/run/mysqld/mysqld.sock` - how to deal with that? – user984621 Oct 29 '13 at 09:23

6 Answers6

8

In my case, the server had apparently restarted at some point, and mysql was not started when it did. I simply typed the command to start and it worked:

sudo service mysqld start
Noah Dyer
  • 407
  • 5
  • 10
  • Or `sudo systemctl start mysqld.service`, depending on the OP's system. In which case it can be automated for future reboots, using `sudo systemctl enable mysqld.service`. – ShellFish Jan 13 '16 at 16:47
0

locate your locate my.cnf file see there for line as socket = ../../mysql.sock you need to replace the path with your path as

socket = /run/mysqld/mysqld.sock

if the path matches let me know

amit karsale
  • 740
  • 5
  • 14
  • Thank you for your answer Amit. The output of `locate my.cnf` is `/etc/mysql/my.cnf`, how to update it now? – user984621 Oct 29 '13 at 09:32
  • `sudo vim /etc/mysql/my.cnf` and look for `socket=../../mysql.sock` line and see if the path for socket is same as `/run/mysqld/mysqld.sock` otherwise replace it and save the file by pressing `Esc` + `:`+ `wq` – amit karsale Oct 29 '13 at 09:45
  • I did this update - the previous path was `/var/run/mysqld/mysqld.sock`, so I changed it. However, still the same issue. I tried also to restart `nginx`, but it didn't help me. But - in log is still the old path - `Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111) (Mysql2::Error)` – user984621 Oct 29 '13 at 09:53
  • try `sudo /etc/init.d/mysql restart` – amit karsale Oct 29 '13 at 11:28
0

Cross posting my response from here.

I had this same problem on an ec2 instance. These instructions worked perfectly for me:


Redhat Enterprise Linux - RHEL 5 / 6 MySQL installation

Type the following command as root user:

yum install mysql-server mysql

Redhat Enterprise Linux - RHEL 4/3 MySQL installation

Type the following command as root user:

up2date mysql-server mysql

Start MySQL Service

To start the mysql server type the following command:

chkconfig mysqld on
/etc/init.d/mysqld start

Setup the mysql root password

Type the following command to setup a password for root user:

mysqladmin -u root password NEWPASSWORD

Test the mysql connectivity

Type the following command to connect to MySQL server:

$ mysql -u root -p
Community
  • 1
  • 1
0

In my case I used command below:

project_folder > mysql.server start 

After system return SUCCESS message it worked OK.

sambua
  • 2,274
  • 3
  • 22
  • 20
0

mysql-server is not installed by default on the Amazon Linux AMI. Use these commands to install it:

$ sudo yum install mysql-server
$ sudo chkconfig mysqld on
$ sudo /etc/init.d/mysqld start
$ mysqladmin -u root password NEWPASSWORD
$ mysql -u root -p
Undo
  • 25,519
  • 37
  • 106
  • 129
Raimon
  • 679
  • 6
  • 6
0

To solve this error first find your socket file, run the following commands in terminal

mysqladmin variables | grep socket

For me, this gives:

| socket              | /var/run/mysqld/mysqld.sock

Then, add a line to your Rails application: config/database.yml:

development:
adapter: mysql2
host: localhost
username: root
password: xxxx
database: xxxx
socket: /var/run/mysqld/mysqld.sock

This will solve this problem.

Praveen George
  • 9,237
  • 4
  • 26
  • 53