1

I trying to follow the steps here to run ruby on rails on linux, everything was fine except when i try to execute this

rake db:create

i got this error,

#<Mysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>"secretpassword", "host"=>"localhost", "database"=>"apps_development"}, {:charset=>"utf8"}
(If you set the charset manually, make sure you have a matching collation)
Created database 'apps_development'
#<Mysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>"secretpassword", "host"=>"localhost", "database"=>"apps_test"}, {:charset=>"utf8"}
(If you set the charset manually, make sure you have a matching collation)
Created database 'apps_test'

what this means?

Arount
  • 9,853
  • 1
  • 30
  • 43
Fil
  • 8,225
  • 14
  • 59
  • 85
  • 1
    Possible duplicate of [rake db:create generated "if you set the charset manually, make sure you have a matching collation" error](http://stackoverflow.com/questions/5952265/rake-dbcreate-generated-if-you-set-the-charset-manually-make-sure-you-have-a) – Deepak Mahakale Nov 25 '16 at 13:49

1 Answers1

2

The error is quite explicit:

#<Mysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)>

Your application can't connect to your MySQL database. Your are trying to reach it through socket (/var/run/mysqld/mysqld.sock).

You have to know where Mysql socket is stored and adapt code OR MySQL config to match eachother.

Before that, you may want to check if Mysql is running, obviously you can not access to socket if Mysql is down. Check it with: sudo service mysql status.

If Mysql is up, check what following.

Find socket and check rights

  1. Find socket path while Mysql running: mysql -e '\s;' | grep 'UNIX socket:'. You may need to add -u<USERNAME> -p<PASSWORD> depending your client configuration. It should result something like: UNIX socket: /var/run/mysqld/mysqld.sock. Here /var/run/mysqld/mysqld.sock is the socket location.
  2. Check socket file: ls -l <SOCKET-PATH> (here: ls -l /var/run/mysqld/mysqld.sock).
    • If you got ls: cannot access /var/run/mysqld/mysqld.sock: No such file or directory then your socket file do not exists. Restarting Mysql may fix it (sudo service mysql restart).
    • If you got srwxrwxrwx 1 mysql mysql 0 nov. 25 10:07 /var/run/mysqld/mysqld.sock check rights and owners on this file (by default should be owned by mysql user & group and be executable at least to mysql user).

Configure your app

Now you know where socket file is and you are sure your app can access it, you may need to adapte your application to what you learn from first part.

According to Rails doc you must edit your config/database.yml file to set socket field to the socket location.

Arount
  • 9,853
  • 1
  • 30
  • 43