如何通过 Ruby 的“mysql2” gem 连接到 MySQL?

问题描述

我安装了一个新的 MySQL 实例并尝试通过 MySQL Workbench 连接到该实例(使用 IP 地址、默认端口、用户名/密码)。连接成功。所以这些值是正确的。

然后,我创建了一个新的数据库架构“newDB”,并将所有权限授予同一用户 newDB。

接下来,我使用以下 ruby​​ 代码连接到这个 MySQL 实例。

require 'fileutils'
require 'logger'
require 'sequel'
require 'mysql2'

DATABASE_URL = 'mysql2://The.IP.Address:3306'

DB_OPTIONS = { encoding: 'utf8',sql_log_level: :debug,adapter: "mysql2",database: "newDB",password: 'password123',username: 'someUser',host:'The.IP.Address',port:'3306'  }

database_connection = Sequel.connect(DATABASE_URL,DB_OPTIONS.merge(logger: database_logger))

但是,此连接失败。这是堆栈跟踪:

13: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/sequel-5.44.0/lib/sequel/core.rb:124:in `connect'
        12: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/sequel-5.44.0/lib/sequel/database/connecting.rb:57:in `connect'
        11: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/sequel-5.44.0/lib/sequel/database/connecting.rb:57:in `new'
        10: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/sequel-5.44.0/lib/sequel/database/misc.rb:169:in `initialize'
         9: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/sequel-5.44.0/lib/sequel/database/connecting.rb:278:in `test_connection'
         8: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/sequel-5.44.0/lib/sequel/database/connecting.rb:269:in `synchronize'
         7: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/sequel-5.44.0/lib/sequel/connection_pool/threaded.rb:91:in `hold'
         6: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/sequel-5.44.0/lib/sequel/connection_pool/threaded.rb:139:in `acquire'
         5: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/sequel-5.44.0/lib/sequel/connection_pool/threaded.rb:209:in `assign_connection'
         4: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/sequel-5.44.0/lib/sequel/connection_pool.rb:122:in `make_new'
         3: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/sequel-5.44.0/lib/sequel/adapters/mysql2.rb:43:in `connect'
         2: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/sequel-5.44.0/lib/sequel/adapters/mysql2.rb:43:in `new'
         1: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/mysql2-0.5.3-x64-mingw32/lib/mysql2/client.rb:90:in `initialize'
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/mysql2-0.5.3-x64-mingw32/lib/mysql2/client.rb:90:in `connect': Mysql2::Erro (Sequel::DatabaseConnectionError)sha2_password' cannot be loaded: The specified module could not be found.

我该如何解决这个错误?我似乎为主机名、用户名等提供了正确的值。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)