mysqlruby-on-railsrubyredmine

Redmine: Error trying to change from cookie sessions to database


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!!!


Solution

  • Rails needs information about the database server that it should connect to. This information can be provided to Rails in a variety of ways:

    That 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:

    1. You use database.yml to provide the configuration
    2. database.yml has a development block that defines a configuration where MySQL is accessed through /var/run/mysqld/mysqld.sock
    3. You are attempting this on a production server, not a development device
    4. You have not defined RAILS_ENV in your shell environment prior to running the command

    I'm guessing this because of the following hints:

    1. Your gems are installed to /var/lib/gems which is typical of a production Ruby server running on Linux and atypical for development devices
    2. You are connecting to a remote MySQL server rather than a local server which is typical of a production Ruby server and atypical for development devices
    3. The default RAILS_ENV is development and when you run the command it picks up that configuration from database.yml

    The solution is likely that you need to manually set RAILS_ENV in your shell to the appropriate environment name defined in database.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:

    export RAILS_ENV=production
    rails generate active_record:session_migration
    

    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 or staging or development). You can determine this by checking database.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 a development block -- then it is likely that your server connects using ENV['DATABASE_URL'] and you must do all of the following:

    1. Determine the correct value for DATABASE_URL
    2. Determine the correct value for RAILS_ENV
    3. Set DATABASE_URL in your shell environment before running the command
    4. Set RAILS_ENV in your shell environment before running the command

    It 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:

    export DATABASE_URL=mysql2://username:password@remote_host_name/database_name
    export RAILS_ENV=production
    rails generate active_record:session_migration
    

    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 running rake 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.