I’m trying to move from cookie-based sessions to a database. I’m running this command. I get the error below. It’s trying to connect to a local MySQL server. I do not have a local MySQL server. I have a dedicated MySQL server that Redmine connects to just fine, but when I try to run this command:
rails generate active_record:session_migration
It tries to look for a local MySQL instance:
/var/lib/gems/3.0.0/gems/activerecord-6.1.7/lib/active_record/connection_adapters/mysql2_adapter.rb:45:in `rescue in new_client': Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) (ActiveRecord::ConnectionNotEstablished)
I cannot figure out how to tell it to connect to the remote MySQL server.
Any help would be most appreciated!!!
2
Answers
You need to edit your database.yml file.
Rails needs information about the database server that it should connect to. This information can be provided to Rails in a variety of ways:
ENV['DATABASE_URL']
is commonly used on deployed servers where it’s easier to substitute a variable than it is to substitute a fileconfig/database.yml
is a very common method and the default in new Rails appsActiveRecord::Base.configurations=
can be used to set a configuration by handThat last option is what is used internally by Rails when it reads the database variable or the file: it sets the configuration based on the values provided to it. And Rails will automatically discover the database configuration by evaluating the first two options and providing a merged result, if one or both are present.
In your case Rails is automatically discovering values that tell it to look for MySQL on the local server and this is not what you intend. I have a hunch that the problem is:
database.yml
to provide the configurationdatabase.yml
has adevelopment
block that defines a configuration where MySQL is accessed through/var/run/mysqld/mysqld.sock
RAILS_ENV
in your shell environment prior to running the commandI’m guessing this because of the following hints:
/var/lib/gems
which is typical of a production Ruby server running on Linux and atypical for development devicesRAILS_ENV
isdevelopment
and when you run the command it picks up that configuration fromdatabase.yml
The solution is likely that you need to manually set
RAILS_ENV
in your shell to the appropriate environment name defined indatabase.yml
before you running your command, however I urge you to exercise great care and I caution you not to run this command without first verifying that it is correct and safe. For example:Before running this command you should make sure that you have determined with certainty what environment name is expected on this server (for example,
production
orstaging
ordevelopment
). You can determine this by checkingdatabase.yml
for the appropriate block that defines the remote server you expect to be connecting to.If you cannot determine the environment name by looking at
database.yml
— for example if it only defines adevelopment
block — then it is likely that your server connects usingENV['DATABASE_URL']
and you must do all of the following:DATABASE_URL
RAILS_ENV
DATABASE_URL
in your shell environment before running the commandRAILS_ENV
in your shell environment before running the commandIt is very important that these two variables are both set properly so that you load the correct Rails environment (and all its settings) and that you connect to the correct database for that environment.
If this is the case then once you have those values you can re-run your command with the following example, however I again urge you to exercise great care and I caution you not to run this command without first verifying that it is correct and safe:
Likewise, prior to running
rake db:migrate
as the next step ensure that these environment variables are set or you will have the same error come up. I especially urge you to exercise caution prior to runningrake db:migrate
without being absolutely certain that you are using the correct environment name and database configuration as this action will write to your remote database server.