Search code examples
mysqlruby-on-railsmysql2

How to validate mysql database URI


I'm trying to integrate a Gem named blazer with my Rails application and I have to specify mysql database URL in blazer.yml file so that it can access data in staging and production environments.

I believe the standard format to define MySQL database URL is

mysql2://user:password@hostname:3306/database

I defined my URL in the same format as a string and when I validate the URI I get the below error

URI::InvalidURIError: bad URI(is not URI?): mysql2://f77_oe_85_staging:LcCh%264855c6M;kG9yGhjghjZC?JquGVK@factory97-aurora-staging-cluster.cluster-cmj77682fpy4kjl.us-east-1.rds.amazonaws.com/factory97_oe85_staging

Defined Mysql database URL:

'mysql2://f77_oe_85_staging:LcCh%264855c6M;kG9yGhjghjZC?JquGVK@factory97-aurora-staging-cluster.cluster-cmj77682fpy4kjl.us-east-1.rds.amazonaws.com/factory97_oe85_staging'

Please advice


Solution

  • The URI is invalid.

    The problem is the password contains characters which are not valid in a URI. The username:password is the userinfo part of a URI. From RFC 3986...

      foo://example.com:8042/over/there?name=ferret#nose
      \_/   \______________/\_________/ \_________/ \__/
       |           |            |            |        |
    scheme     authority       path        query   fragment
    
    authority   = [ userinfo "@" ] host [ ":" port ]
    userinfo    = *( unreserved / pct-encoded / sub-delims / ":" )
    
    pct-encoded = "%" HEXDIG HEXDIG
    unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"
    sub-delims  = "!" / "$" / "&" / "'" / "(" / ")"
                / "*" / "+" / "," / ";" / "="
    

    Specifically it's the ? in the password LcCh%264855c6M;kG9yGhjghjZC?JquGVK. It looks like the password is only partially escaped.