0

Trying to switch our production Rails 7.1.3 server from mysql2 to trilogy to connect to two dbs:

  • our remote mysql db at DB_IP_ADDR on a separate DO droplet
  • our local solid_cache mysql db on our webserver DO droplet at PRODUCTION_IP_ADDR

Because of the known issue with trilogy not being able to use mysql 8's caching_sha2_password, I've already reverted mysql to use mysql_native_password, on both the remote mysql server and the production (localhost) cache mysql server:

# on DB_IP_ADDR

mysql> ALTER USER 'us'@'PRODUCTION_IP_ADDR' IDENTIFIED WITH mysql_native_password BY 'password';
mysql> FLUSH PRIVILEGES;
# on PRODUCTION_IP_ADDR

mysql> ALTER USER 'us'@'localhost' IDENTIFIED WITH mysql_native_password BY 'cache_password';
mysql> FLUSH PRIVILEGES;

Our config/database.yml

production:
  primary:
    adapter:  trilogy
    database: remote_production_db
    host:     DB_IP_ADDR
    username: us
    password: password
    socket:   /var/run/mysqld/mysqld.sock
    encoding: utf8
  cache:
    adapter:  trilogy
    database: our_solid_cache_db
    host:     127.0.0.1
    username: us
    password: cache_password
    socket:   /var/run/mysqld/mysqld.sock
    endoding: utf8
    migrations_paths: "db/cache/migrate"

The local cache db connects fine with trilogy, but the remote db connection fails with

ActiveRecord::DatabaseConnectionError: There is an issue connecting to your database with your username/password, username: us. (ActiveRecord::DatabaseConnectionError)

Please check your database configuration to ensure the username/password are valid.
...

Caused by:
Trilogy::BaseConnectionError: 1045: Access denied for user 'us'@'localhost' (using password: YES) (Trilogy::BaseConnectionError)

I've double-checked the password and it's right: If i switch the database.yml back to adapter: mysql2 without changing anything else, both db connections are OK.

I notice the error message identifies the attempted user connection as 'us'@'localhost', but the request is not coming from localhost, it's coming from PRODUCTION_IP_ADDR.

Is there something special I need for Trilogy to connect to a remote db, or identify a remote user?

2
  • 1
    If the mysql username has localhost in it, then your code connects to a local mysql instance, not to a remote one. Commented Mar 1, 2024 at 7:47
  • Thanks. That was the mystery - the mysql username did not have localhost in it! But your comment put me on the right track, thanks. :) Commented Mar 1, 2024 at 8:20

1 Answer 1

0

Figured it out.

The remote host needs either a host IP, or a socket, but not both. The presence of a socket param makes it assume it's on localhost.

I deleted the remote socket and it connects OK.

I assume mysql2 makes a different assumption from the same params. It must ignore the socket or something.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.