1

Recently I tried to change the database of my Rails application. I was using sqlite3 and I wanted to change it to MySQL. I followed the steps of this tutorial : https://guides.rubyonrails.org/configuring.html#configuring-a-mysql-or-mariadb-database

This is my database.yml :

default: &default
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  adapter: mysql2
  encoding: utf8mb4
  database: blog_development
  pool: 5
  username: root
  password:
  socket: /tmp/mysql.sock

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: db/test.sqlite3

production:
  adapter: mysql2
  advisory_locks: false

What caused this following error : Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) and what I need to do to fix it ?

  • Are you sure MySQL is up and running? Can you connect to it via an admin console, etc.? If so, check its config to ensure that the socket file it is using is ``/tmp/mysql.sock``. – rmlockerd Jun 20 '20 at 19:59
  • Is mysql running on the same machine as your rails server? If not you need to set the host with `host` e.g. like `mysql://:@:/`. – Christian Bruckmayer Jun 20 '20 at 20:18
  • Thanks for answering me . I received this error just now : "#". How can I access it ? – Gabriel Carneiro Jun 21 '20 at 00:58

2 Answers2

0

Reading your latest comment, I've noticed that you've got a new error:

#<Mysql2::Error::ConnectionError: Access denied for user 'root'@'localhost' (using password: NO)>

You've left your password field under the development environment empty. All you need to do is to fill the password with the same password you set while installing mysql2, it should do it.

In case you can't remember your password, check this guide:

Resuming the guide, you should:

sudo service mysql stop
sudo mysqld_safe --skip-grant-tables --skip-networking &
mysql -u root

Once inside MySQL, you shall type:

use mysql;
update user set authentication_string=password('NEWPASSWORD') where user='root';
flush privileges;
exit

This should do it. If you ever want to log into MySQL once again, you shall type:

mysql -u root -p NEWPASSWORD

The -p stands for password.

I'd recommend against using your password directly on database.yml file. It's safer to set an environment variable with your password and then using it on database.yml, like this:

  username: <%= ENV.fetch("username") %>
  password: <%= ENV.fetch("password") %>

If this is the case and you are running Linux, you must update your environment variables under /etc/environment. Under /etc/environment you must have something like this:

username=MYSQL_USERNAME_HERE
password=MYSQL_PASSWORD_HERE
João Fernandes
  • 673
  • 1
  • 5
  • 15
  • Thanks for answering me . I tried to set a new password for the first time as the guide suggests (because i did not remember of mysql asking me during the installation) I got this message : ➜ painel-backend git:(master) ✗ mysqladmin -u root password NEWPASSWORD mysqladmin: connect to server at 'localhost' failed error: 'Access denied for user 'root'@'localhost' (using password: NO)' – Gabriel Carneiro Jun 21 '20 at 15:30
  • I editted my answer. If it makes easier for you to understand using Portuguese, just let me know. – João Fernandes Jun 21 '20 at 20:16
  • I just got another problem. When I typed mysql -u root I got this following error : ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) – Gabriel Carneiro Jun 22 '20 at 01:10
  • Did you run the entire sudo mysqld_safe --skip-grant-tables --skip-networking & ? Including the & at the end? – João Fernandes Jun 25 '20 at 17:37
0

Check your syslog to see where is the socket location. In my case is "run/mysqld/mysqld.sock". Then edit the database.yml file.

From

socket: /tmp/mysql.sock

to

socket: /run/mysqld/mysqld.sock
Cleber Reizen
  • 420
  • 4
  • 11